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

[YSQL] Support Distinct Bitmap Scans #21039

Open
1 task done
timothy-e opened this issue Feb 12, 2024 · 0 comments
Open
1 task done

[YSQL] Support Distinct Bitmap Scans #21039

timothy-e opened this issue Feb 12, 2024 · 0 comments
Assignees
Labels
area/ysql Yugabyte SQL (YSQL) kind/bug This issue is a bug priority/medium Medium priority issue

Comments

@timothy-e
Copy link
Contributor

timothy-e commented Feb 12, 2024

Jira Link: DB-10008

Description

Schema:

CREATE TABLE uniq(r1 INT, r2 INT, r3 INT, PRIMARY KEY(r1 ASC, r2 ASC));
CREATE INDEX idx on uniq(r3 ASC);
INSERT INTO uniq (SELECT 1, i FROM GENERATE_SERIES(1, 10) AS i);
INSERT INTO uniq (SELECT 2, i FROM GENERATE_SERIES(1, 10) AS i);
INSERT INTO uniq (SELECT 3, i FROM GENERATE_SERIES(1, 10) AS i);

To answer this query, we use a bitmap scan:

EXPLAIN (COSTS OFF) /*+Set(enable_hashagg false)*/ SELECT DISTINCT r1 FROM uniq WHERE r1 <= 5 OR r2 <= 5;
                       QUERY PLAN
--------------------------------------------------------
 Unique
   ->  Sort
         Sort Key: r1
         ->  YB Bitmap Table Scan on uniq
             Storage Table Rows Scanned: 30
               ->  BitmapOr
                     ->  Bitmap Index Scan on uniq_pkey
                           Index Cond: (r1 <= 5)
                           Storage Table Rows Scanned: 30
                     ->  Bitmap Index Scan on uniq_pkey
                           Index Cond: (r2 <= 5)
                           Storage Table Rows Scanned: 15
(9 rows)

If we break out the conditions, we only have to select 1 row per tablet with SELECT DISTINCT:

EXPLAIN ANALYZE /*+Set(enable_hashagg false)*/ SELECT DISTINCT r1 FROM uniq WHERE r1 <= 5;
                                                           QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------
 Unique  (cost=0.00..12.51 rows=82 width=4) (actual time=3.730..3.755 rows=3 loops=1)
   ->  Distinct Index Scan using uniq_pkey on uniq  (cost=0.00..12.51 rows=82 width=4) (actual time=3.722..3.736 rows=3 loops=1)
         Index Cond: (r1 <= 5)
         Distinct Prefix: 1
 Planning Time: 0.344 ms
 Execution Time: 3.883 ms
 Peak Memory Usage: 24 kB
(7 rows)

EXPLAIN ANALYZE /*+Set(enable_hashagg false)*/ SELECT DISTINCT r1 FROM uniq WHERE r2 <= 5;
                                                           QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------
 Unique  (cost=0.00..12.51 rows=82 width=4) (actual time=5.809..5.837 rows=3 loops=1)
   ->  Distinct Index Scan using uniq_pkey on uniq  (cost=0.00..12.51 rows=82 width=4) (actual time=5.802..5.818 rows=3 loops=1)
         Index Cond: (r2 <= 5)
         Distinct Prefix: 1
 Planning Time: 0.877 ms
 Execution Time: 6.046 ms
 Peak Memory Usage: 24 kB
(7 rows)

If we could support Distinct Index Scans inside Bitmap Operations, we could do this really efficiently:

EXPLAIN (COSTS OFF) /*+Set(enable_hashagg false)*/ SELECT DISTINCT r1 FROM uniq WHERE r1 <= 5 OR r2 <= 5;
                       QUERY PLAN
--------------------------------------------------------
 Unique
   ->  YB Bitmap Table Scan on uniq
       Storage Table Rows Scanned: 3
         ->  BitmapOr
              ->  Bitmap Distinct Index Scan on uniq_pkey
                    Index Cond: (r1 <= 5)
                    Distinct Prefix: 1
                    Storage Table Rows Scanned: 3
              ->  Bitmap Distinct Index Scan on uniq_pkey
                    Index Cond: (r2 <= 5) 
                    Distinct Prefix: 1
                    Storage Table Rows Scanned: 3
(9 rows)

Issue Type

kind/bug

Warning: Please confirm that this issue does not contain any sensitive information

  • I confirm this issue does not contain any sensitive information.
@timothy-e timothy-e added area/ysql Yugabyte SQL (YSQL) status/awaiting-triage Issue awaiting triage labels Feb 12, 2024
@yugabyte-ci yugabyte-ci added kind/bug This issue is a bug priority/medium Medium priority issue labels Feb 12, 2024
@timothy-e timothy-e self-assigned this Feb 22, 2024
@sushantrmishra sushantrmishra removed the status/awaiting-triage Issue awaiting triage label Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/ysql Yugabyte SQL (YSQL) kind/bug This issue is a bug priority/medium Medium priority issue
Projects
None yet
Development

No branches or pull requests

3 participants