A performance bug about LIMIT 0 query
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
Please answer these questions before submitting your issue. Thanks!
### 1. Minimal reproduce step (Required)
If you have a query of the form:
Q1 INTERSECT Q2 ... INTERSECT Qn,
and you know that query Qn always returns an empty set(e.g., a query with LIMIT 0), then the entire intersection will always be empty.
I think that the query should be eliminated during optimization, as it will always return an empty set and should never consume execution time.
```
mysql> use INFORMATION_SCHEMA;
mysql> SELECT * FROM TABLES CROSS JOIN COLUMNS ON TABLES.TABLE_NAME = COLUMNS.TABLE_NAME
INTERSECT
SELECT * FROM TABLES CROSS JOIN COLUMNS ON TABLES.TABLE_NAME != COLUMNS.TABLE_NAME
INTERSECT
SELECT * FROM TABLES CROSS JOIN COLUMNS ON TABLES.TABLE_NAME > COLUMNS.TABLE_NAME
INTERSECT
SELECT * FROM TABLES CROSS JOIN COLUMNS ON TABLES.TABLE_NAME < COLUMNS.TABLE_NAME LIMIT 0;
Empty set (0.00 sec)
mysql> SELECT * FROM TABLES CROSS JOIN COLUMNS ON TABLES.TABLE_NAME = COLUMNS.TABLE_NAME
INTERSECT
SELECT * FROM TABLES CROSS JOIN COLUMNS ON TABLES.TABLE_NAME != COLUMNS.TABLE_NAME
INTERSECT
SELECT * FROM TABLES CROSS JOIN COLUMNS ON TABLES.TABLE_NAME > COLUMNS.TABLE_NAME
INTERSECT
(SELECT * FROM TABLES CROSS JOIN COLUMNS ON TABLES.TABLE_NAME < COLUMNS.TABLE_NAME LIMIT 0);
Empty set (1 min 17.27 sec)
```
### 2. What did you expect to see? (Required)
```
mysql> SELECT * FROM TABLES CROSS JOIN COLUMNS ON TABLES.TABLE_NAME = COLUMNS.TABLE_NAME
INTERSECT
SELECT * FROM TABLES CROSS JOIN COLUMNS ON TABLES.TABLE_NAME != COLUMNS.TABLE_NAME
INTERSECT
SELECT * FROM TABLES CROSS JOIN COLUMNS ON TABLES.TABLE_NAME > COLUMNS.TABLE_NAME
INTERSECT
(SELECT * FROM TABLES CROSS JOIN COLUMNS ON TABLES.TABLE_NAME < COLUMNS.TABLE_NAME LIMIT 0);
Empty set (0.00 sec)
```
### 3. What did you see instead (Required)
```
mysql> SELECT * FROM TABLES CROSS JOIN COLUMNS ON TABLES.TABLE_NAME = COLUMNS.TABLE_NAME
INTERSECT
SELECT * FROM TABLES CROSS JOIN COLUMNS ON TABLES.TABLE_NAME != COLUMNS.TABLE_NAME
INTERSECT
SELECT * FROM TABLES CROSS JOIN COLUMNS ON TABLES.TABLE_NAME > COLUMNS.TABLE_NAME
INTERSECT
(SELECT * FROM TABLES CROSS JOIN COLUMNS ON TABLES.TABLE_NAME < COLUMNS.TABLE_NAME LIMIT 0);
Empty set (1 min 17.27 sec)
```
### 4. What is your TiDB version? (Required)
| Release Version: v8.5.1
Edition: Community
Git Commit Hash: fea86c8e35ad4a86a5e1160701f99493c2ee547c
Git Branch: HEAD
UTC Build Time: 2025-01-16 07:38:34
GoVersion: go1.23.4
Race Enabled: false
Check Table Before Drop: false
Store: tikv |
Contributor guide
Assessment
This issue has not been assessed yet.