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] Primary Index scan cost higher than Sequential cost in small tables #26235

Open
1 task done
gauravk-in opened this issue Feb 27, 2025 · 0 comments
Open
1 task done
Assignees
Labels
area/ysql Yugabyte SQL (YSQL) kind/bug This issue is a bug priority/medium Medium priority issue

Comments

@gauravk-in
Copy link
Contributor

gauravk-in commented Feb 27, 2025

Jira Link: DB-15582

Description

For small tables, a primary index scan is being assigned higher cost than sequential scan, even though it appears to be slightly faster.

This seems to be caused by the assumption that disk pages will be fetched randomly for primary index scan in the index scan cost model.

yugabyte=# create table test (k1 INT, v1 INT, PRIMARY KEY (k1 ASC));
CREATE TABLE
yugabyte=# INSERT INTO test (SELECT s, s FROM generate_series(1, 10) s);
INSERT 0 10
yugabyte=# ANALYZE test;
ANALYZE
yugabyte=# SET yb_enable_base_scans_cost_model = ON;
SET

yugabyte=# /*+ IndexScan(test test_pkey) */ EXPLAIN ANALYZE SELECT * FROM test WHERE k1 < 5;
                                                     QUERY PLAN                                                     
--------------------------------------------------------------------------------------------------------------------
 Index Scan using test_pkey on test  (cost=180.00..552.88 rows=3 width=8) (actual time=0.467..0.469 rows=4 loops=1)
   Index Cond: (k1 < 5)
 Planning Time: 0.068 ms
 Execution Time: 0.497 ms
 Peak Memory Usage: 24 kB
(5 rows)

yugabyte=# EXPLAIN ANALYZE SELECT * FROM test WHERE k1 < 5;
                                            QUERY PLAN                                            
--------------------------------------------------------------------------------------------------
 Seq Scan on test  (cost=180.00..550.53 rows=3 width=8) (actual time=1.313..1.316 rows=4 loops=1)
   Storage Filter: (k1 < 5)
 Planning Time: 0.139 ms
 Execution Time: 1.393 ms
 Peak Memory Usage: 24 kB
(5 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.
@gauravk-in gauravk-in added area/ysql Yugabyte SQL (YSQL) status/awaiting-triage Issue awaiting triage labels Feb 27, 2025
@gauravk-in gauravk-in self-assigned this Feb 27, 2025
@yugabyte-ci yugabyte-ci added kind/bug This issue is a bug priority/medium Medium priority issue and removed status/awaiting-triage Issue awaiting triage labels Feb 27, 2025
gauravk-in added a commit that referenced this issue Feb 28, 2025
…tched sequentially for primary index scan

Summary:
In the cost model, we try to predict the order in which the disk blocks need
to be fetched. In case of a secondary index scan, we assume that the disk
blocksfor primary table will be fetched in a random order. But for a primary
index scan, we must assume that the disk blocks are fetched in sequential
order.

Before this change, the index scan was being costed higher than a sequential
scan for the following example. With this change, the index scan is cheaper.

```
CREATE TABLE t1 (a INT, PRIMARY KEY (a ASC));
INSERT INTO t1 SELECT s FROM generate_series(1, 10) s;
ANALYZE t1;
SET yb_enable_base_scans_cost_model = ON;

yugabyte=# explain select * from t1 where a < 5;
                             QUERY PLAN
--------------------------------------------------------------------
 Index Scan using t1_pkey on t1  (cost=10.00..31.21 rows=3 width=4)
   Index Cond: (a < 5)
(2 rows)

yugabyte=# /*+ SeqScan(t1) */ explain select * from t1 where a < 5;
                     QUERY PLAN
----------------------------------------------------
 Seq Scan on t1  (cost=10.00..31.61 rows=3 width=4)
   Storage Filter: (a < 5)
(2 rows)
```
Jira: DB-15582

Test Plan: ./yb_build.sh --java-test 'org.yb.pgsql.TestPgRegressPlanner'

Reviewers: mtakahara, amartsinchyk

Reviewed By: mtakahara

Subscribers: yql

Differential Revision: https://phorge.dev.yugabyte.com/D42188
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

2 participants