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] Optimizer is choosing different plans for 2 queries which use partition columns in different syntaxes #14836

Open
KavyaShivashankar opened this issue Nov 3, 2022 · 1 comment
Assignees
Labels
area/ysql Yugabyte SQL (YSQL) kind/bug This issue is a bug priority/medium Medium priority issue

Comments

@KavyaShivashankar
Copy link
Contributor

KavyaShivashankar commented Nov 3, 2022

Jira Link: DB-4138

Description

I have 2 queries using all partition keys effectively doing the same thing with different syntaxes. The execution plan for both these are different ( one uses a filter while the other one does not). Why is parsing phase able to parse 2 PK columns in the same way for both the queries, but differs for one PK column between the 2 queries.?

Cluster is a single region multi-AZ cluster

Scenario 1 - default seqscan chosen for query 1

yugabyte=# show enable_seqscan;
 enable_seqscan
----------------
 on
(1 row)

yugabyte=# explain analyse select * from test where (id,type,version) IN (('11111','A','1.1'),('11112','A','1.1'));;
                                                  QUERY PLAN
---------------------------------------------------------------------------------------------------------------
 Seq Scan on test  (cost=0.00..110.00 rows=1000 width=138) (actual time=1.256..5.028 rows=2 loops=1)
   Filter: ((type = 'A'::text) AND (version = '1.1'::text) AND ((id = '11111'::text) OR (id = '11112'::text)))
   Rows Removed by Filter: 3
 Planning Time: 0.114 ms
 Execution Time: 5.086 ms
 Peak Memory Usage: 8 kB
(6 rows)

yugabyte=# explain analyze select * from test where id IN ('11111','11112') and type = 'A' and version = '1.1';
                                                    QUERY PLAN
------------------------------------------------------------------------------------------------------------------
 Index Scan using test_pkey on test  (cost=0.00..4.12 rows=1 width=138) (actual time=2.190..2.193 rows=2 loops=1)
   Index Cond: ((id = ANY ('{11111,11112}'::text[])) AND (type = 'A'::text) AND (version = '1.1'::text))
 Planning Time: 0.104 ms
 Execution Time: 2.247 ms
 Peak Memory Usage: 8 kB
(5 rows)

Scenario 2 - seqcan turned off. Query 1 choses index on columns type and version and adds a filter on column id instead of choosing index for all the 3 columns like query 2

yugabyte=# set enable_seqscan=false;
SET

yugabyte=# explain analyse select * from test where (id,type,version) IN (('11111','A','1.1'),('11112','A','1.1'));
                                                      QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
 Index Scan using test_pkey on test  (cost=0.00..129.00 rows=1000 width=138) (actual time=1.544..5.791 rows=2 loops=1)
   Index Cond: ((type = 'A'::text) AND (version = '1.1'::text))
   Filter: ((id = '11111'::text) OR (id = '11112'::text))
   Rows Removed by Filter: 3
 Planning Time: 0.157 ms
 Execution Time: 5.850 ms
 Peak Memory Usage: 8 kB
(7 rows)

yugabyte=# explain analyze select * from test where id IN ('11111','11112') and type = 'A' and version = '1.1';
                                                    QUERY PLAN
------------------------------------------------------------------------------------------------------------------
 Index Scan using test_pkey on test  (cost=0.00..4.12 rows=1 width=138) (actual time=1.574..1.579 rows=2 loops=1)
   Index Cond: ((id = ANY ('{11111,11112}'::text[])) AND (type = 'A'::text) AND (version = '1.1'::text))
 Planning Time: 0.104 ms
 Execution Time: 1.632 ms
 Peak Memory Usage: 8 kB
(5 rows)

Tried these queries on vanilla Postgres 11.2 docker image and here is the plan

create table test(
  id text,
  type text,
  version text,
  value bytea,
  entity_ts timestamp without time zone,
  cache_version smallint,
primary key(id, type, version));

postgres=# explain analyse select * from test where (id,type,version) IN (('11111','A','1.1'),('11112','A','1.1'));;
                                                                                QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on test  (cost=8.32..12.34 rows=1 width=138) (actual time=0.151..0.152 rows=2 loops=1)
   Recheck Cond: (((id = '11111'::text) AND (type = 'A'::text) AND (version = '1.1'::text)) OR ((id = '11112'::text) AND (type = 'A'::text) AND (version = '1.1'::text)))
   Heap Blocks: exact=1
   ->  BitmapOr  (cost=8.32..8.32 rows=1 width=0) (actual time=0.113..0.114 rows=0 loops=1)
         ->  Bitmap Index Scan on test_pkey  (cost=0.00..4.16 rows=1 width=0) (actual time=0.110..0.110 rows=1 loops=1)
               Index Cond: ((id = '11111'::text) AND (type = 'A'::text) AND (version = '1.1'::text))
         ->  Bitmap Index Scan on test_pkey  (cost=0.00..4.16 rows=1 width=0) (actual time=0.002..0.002 rows=1 loops=1)
               Index Cond: ((id = '11112'::text) AND (type = 'A'::text) AND (version = '1.1'::text))
 Planning Time: 0.575 ms
 Execution Time: 0.341 ms
(10 rows)

postgres=# explain analyze select * from test where id IN ('11111','11112') and type = 'A' and version = '1.1';
                                                    QUERY PLAN
-------------------------------------------------------------------------------------------------------------------
 Index Scan using test_pkey on test  (cost=0.15..12.33 rows=1 width=138) (actual time=0.090..0.097 rows=2 loops=1)
   Index Cond: ((id = ANY ('{11111,11112}'::text[])) AND (type = 'A'::text) AND (version = '1.1'::text))
 Planning Time: 0.446 ms
 Execution Time: 0.135 ms
(4 rows)
@KavyaShivashankar KavyaShivashankar added area/ysql Yugabyte SQL (YSQL) status/awaiting-triage Issue awaiting triage labels Nov 3, 2022
@yugabyte-ci yugabyte-ci added kind/bug This issue is a bug priority/medium Medium priority issue labels Nov 3, 2022
@andrei-mart
Copy link
Contributor

We have been receiving requests to support bitmap scans: #4634, #10848

@yugabyte-ci yugabyte-ci removed the status/awaiting-triage Issue awaiting triage label Jan 16, 2023
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

4 participants