pingcap / pingcap/tidb

short-circuit evaluation for cross join with limit 1

Open
#63,872 0 comments 0 reactions 0 assignees View on GitHub
contribution type/feature-request
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Feature Request

**Is your feature request related to a problem? Please describe:**
```sql
CREATE TEMPORARY TABLE digits (d INT);
INSERT INTO digits VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

-- insert into t1 with 10,000,000 rows
INSERT INTO t1
SELECT
d1.d + d2.d*10 + d3.d*100 + d4.d*1000 + d5.d*10000 + d6.d*100000 + d7.d*1000000 AS num
FROM
digits d1, digits d2, digits d3, digits d4, digits d5, digits d6, digits d7
WHERE
d1.d + d2.d*10 + d3.d*100 + d4.d*1000 + d5.d*10000 + d6.d*100000 + d7.d*1000000 <= 10000000;

SELECT * FROM t1 a CROSS JOIN t2 b LIMIT 1;
ERROR 8175 (HY000): Your query has been cancelled due to exceeding the allowed memory limit ...
```

**Describe alternatives you've considered:**
```sql
-- perform a limit scan and return one row randomly
SELECT * FROM t1 a CROSS JOIN t2 b LIMIT 1;
```

Hi, CockroachDB developers,

Oracle, MySQL, and MariaDB have applied such optimization, so I recommend it.

Contributor guide

Open the contributing guide

Research direction

The issue names no files or tests; start by running the provided CROSS JOIN query with LIMIT 1 and tracing the SQL optimizer and execution path it uses. The work is done when this query can return one row without materializing the full cross join or exceeding the memory limit, with regression coverage for the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, sql
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.