![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have an unusual execution plan below. I am executing a query using the primary key and am getting sequential scan. How can I force the use of the unique index? scott=> explain analyze scott-> select ename,job from emp where empno=8000; QUERY PLAN ---------------------------------------------------------------------------------------------- Seq Scan on emp (cost=0.00..1.18 rows=1 width=74) (actual time=0.053..0.054 rows=1 loops=1) Filter: (empno = 8000) Total runtime: 0.105 ms (3 rows) If I fill the table with a multitude of dummy rows instead of the expected 14, the plan is correct. Is there any method in PostgreSQL which I can use to force a desired execution plan? |
#3
| |||
| |||
|
|
In short: PG is smart enough to decide the right plan. |
#4
| |||
| |||
|
|
On Sat, 14 Nov 2009 13:02:26 +0100, Andreas Kretschmer wrote: In short: PG is smart enough to decide the right plan. Yes, I can see that, but the question is if there is room for the human intervention? In other words, is there a way to influence the plans? I know that PostgreSQL doesn't have hints but there is probably something that can be used instead of them? |
#5
| |||
| |||
|
|
Loki <loki (AT) asgard (DOT) hr.invalid> wrote: On Sat, 14 Nov 2009 13:02:26 +0100, Andreas Kretschmer wrote: In short: PG is smart enough to decide the right plan. Yes, I can see that, but the question is if there is room for the human intervention? In other words, is there a way to influence the plans? I know that PostgreSQL doesn't have hints but there is probably something that can be used instead of them? Sure, you can disable seq-scans: test=# create table test (i int primary key); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_pkey" for table "test" CREATE TABLE Time: 2.388 ms test=*# insert into test values (1); INSERT 0 1 Time: 0.552 ms test=*# analyse test; ANALYZE Time: 0.908 ms test=*# explain select * from test where i=1; QUERY PLAN ---------------------------------------------------- Seq Scan on test (cost=0.00..1.01 rows=1 width=4) Filter: (i = 1) (2 rows) Time: 0.371 ms test=*# set enable_seqscan=false; SET Time: 0.104 ms test=*# explain select * from test where i=1; QUERY PLAN ---------------------------------------------------------------------- Index Scan using test_pkey on test (cost=0.00..8.27 rows=1 width=4) Index Cond: (i = 1) (2 rows) Time: 0.305 ms And please consider the costs! Andreas |
#6
| |||
| |||
|
|
One such parameter is "random_page_cost", with the default value of 4, is basically instructing optimizer that a cost of an index page is approximately 4 times as large as the cost of a page begotten through the full table scan. |
![]() |
| Thread Tools | |
| Display Modes | |
| |