Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push LIMIT into scan of outer table in uncorrelated subquery #75301

Closed
msirek opened this issue Jan 21, 2022 · 2 comments
Closed

Push LIMIT into scan of outer table in uncorrelated subquery #75301

msirek opened this issue Jan 21, 2022 · 2 comments
Assignees
Labels
A-sql-optimizer SQL logical planning and optimizations. C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-queries SQL Queries Team

Comments

@msirek
Copy link
Contributor

msirek commented Jan 21, 2022

In the following test case, the LIMIT could be pushed into the scan of t1 because the EXISTS subquery executes as a totally separate query block with no references to outer table columns. If the LIMIT were pushed, it would enable the locality optimized scans in #64862.

CREATE TABLE t1 (a INT);
CREATE TABLE t2 (a INT);
EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t2) LIMIT 1;
                                           info
------------------------------------------------------------------------------------------
  distribution: full
  vectorized: true

  • root
  │
  ├── • limit
  │   │ estimated row count: 0
  │   │ count: 1
  │   │
  │   └── • filter
  │       │ estimated row count: 0
  │       │ filter: @S1
  │       │
  │       └── • scan
  │             estimated row count: 1 (100% of the table; stats collected 1 minute ago)
  │             table: t1@t1_pkey
  │             spans: FULL SCAN
  │
  └── • subquery
      │ id: @S1
      │ original sql: EXISTS (SELECT * FROM t2)
      │ exec mode: exists
      │
      └── • scan
            estimated row count: 1 (100% of the table; stats collected 1 minute ago)
            table: t2@t2_pkey
            spans: LIMITED SCAN
            limit: 1

Jira issue: CRDB-12547

@msirek msirek added C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) A-sql-optimizer SQL logical planning and optimizations. T-sql-queries SQL Queries Team labels Jan 21, 2022
@msirek msirek self-assigned this Jan 21, 2022
@RaduBerinde
Copy link
Member

I think addressing #51820 would fix this case - we would reoptimize the main query with the result of the subquery.

@mgartner
Copy link
Collaborator

Closing as a duplicate of #51820. Something like #93829 could also solve this, if that was deemed to be a good solution for #51820.

@mgartner mgartner moved this to Done in SQL Queries Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-optimizer SQL logical planning and optimizations. C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-queries SQL Queries Team
Projects
Archived in project
Development

No branches or pull requests

3 participants