Postgres与索引的奇怪行为

时间:2016-02-26 17:38:06

标签: sql postgresql

我有一个datavalue表,行数约为200M左右,site_idparamter_id都有索引。我需要执行诸如“使用数据返回所有站点”和“使用数据返回所有参数”之类的查询。 site表只有200行左右,而parameter表只有100行左右。

site查询速度很快,并使用索引:

EXPLAIN ANALYZE
select *
from site
where exists (
      select 1 from datavalue
      where datavalue.site_id = site.id limit 1
);

Seq Scan on site  (cost=0.00..64.47 rows=64 width=113) (actual time=0.046..1.106 rows=89 loops=1)
  Filter: (SubPlan 1)
  Rows Removed by Filter: 39
  SubPlan 1
    ->  Limit  (cost=0.44..0.47 rows=1 width=0) (actual time=0.008..0.008 rows=1 loops=128)
          ->  Index Only Scan using ix_datavalue_site_id on datavalue  (cost=0.44..8142.71 rows=248930 width=0) (actual time=0.008..0.008 rows=1 loops=128)
                Index Cond: (site_id = site.id)
                Heap Fetches: 0
Planning time: 0.361 ms
Execution time: 1.149 ms

对参数的相同查询相当慢,并且不使用索引:

EXPLAIN ANALYZE
select *
from parameter
where exists (
      select 1 from datavalue
      where datavalue.parameter_id = parameter.id limit 1
);

Seq Scan on parameter  (cost=0.00..20.50 rows=15 width=2648) (actual time=2895.972..21331.701 rows=15 loops=1)
  Filter: (SubPlan 1)
  Rows Removed by Filter: 6
  SubPlan 1
    ->  Limit  (cost=0.00..0.34 rows=1 width=0) (actual time=1015.790..1015.790 rows=1 loops=21)
          ->  Seq Scan on datavalue  (cost=0.00..502127.10 rows=1476987 width=0) (actual time=1015.786..1015.786 rows=1 loops=21)
                Filter: (parameter_id = parameter.id)
                Rows Removed by Filter: 7739355
Planning time: 0.123 ms
Execution time: 21331.736 ms

这里的平局是什么?或者,这是一个很好的方法吗?

一些表格描述:

id BIGINT DEFAULT nextval('datavalue_id_seq'::regclass) NOT NULL,
value DOUBLE PRECISION NOT NULL,
site_id INTEGER NOT NULL,
parameter_id INTEGER NOT NULL,
deployment_id INTEGER,
instrument_id INTEGER,
invalid BOOLEAN,
Indexes:
    "datavalue_pkey" PRIMARY KEY, btree (id)
    "datavalue_datetime_utc_site_id_parameter_id_instrument_id_key" UNIQUE CONSTRAINT, btree (datetime_utc, site_id, parameter_id, instrument_id)
    "ix_datavalue_instrument_id" btree (instrument_id)
    "ix_datavalue_parameter_id" btree (parameter_id)
    "ix_datavalue_site_id" btree (site_id)
    "tmp_idx" btree (site_id, datetime_utc)
Foreign-key constraints:
    "datavalue_instrument_id_fkey" FOREIGN KEY (instrument_id) REFERENCES instrument(id) ON UPDATE CASCADE ON DELETE CASCADE
    "datavalue_parameter_id_fkey" FOREIGN KEY (parameter_id) REFERENCES parameter(id) ON UPDATE CASCADE ON DELETE CASCADE
    "datavalue_site_id_fkey" FOREIGN KEY (site_id) REFERENCES coastal.site(id) ON UPDATE CASCADE ON DELETE CASCADE
    "datavalue_statistic_type_id_fkey"

编辑:这是计数分布

select count(parameter_id), parameter_id from datavalue group by parameter_id

88169   14
2889171 8
15805   17
8570    12
4257262 21
3947049 15
1225902 2
4091090 3
103877  10
633764  11
994442  18
49232   20
14935   4
563638  13
2955919 7

1 个答案:

答案 0 :(得分:3)

更新:如a_horse_with_no_name所述,您可以删除LIMIT 1,查询将使用索引。

显然PostgreSQL错误地认为如果你做一个子查询而忽略了LIMIT 1,它会触及整个数据库。(事实证明这是不必要的。)

我在笔记本电脑上生成了相同的发行版:

create table testtbl (id integer, par_id integer);
insert into testtbl (id, par_id) values (0,0 );
insert into testtbl (id, par_id) select "generate_series", 4 from generate_series((select max(id) from testtbl), (select max(id) from testtbl)+14935);
insert into testtbl (id, par_id) select "generate_series", 12 from generate_series((select max(id) from testtbl), (select max(id) from testtbl)+8570);
insert into testtbl (id, par_id) select "generate_series", 17 from generate_series((select max(id) from testtbl), (select max(id) from testtbl)+15805);
insert into testtbl (id, par_id) select "generate_series", 20 from generate_series((select max(id) from testtbl), (select max(id) from testtbl)+49232);
insert into testtbl (id, par_id) select "generate_series", 14 from generate_series((select max(id) from testtbl), (select max(id) from testtbl)+88169);
insert into testtbl (id, par_id) select "generate_series", 10 from generate_series((select max(id) from testtbl), (select max(id) from testtbl)+103877);
insert into testtbl (id, par_id) select "generate_series", 2 from generate_series((select max(id) from testtbl), (select max(id) from testtbl)+1225902);
insert into testtbl (id, par_id) select "generate_series", 8 from generate_series((select max(id) from testtbl), (select max(id) from testtbl)+2889171);
insert into testtbl (id, par_id) select "generate_series", 7 from generate_series((select max(id) from testtbl), (select max(id) from testtbl)+2955919);
insert into testtbl (id, par_id) select "generate_series", 3 from generate_series((select max(id) from testtbl), (select max(id) from testtbl)+4091090);
insert into testtbl (id, par_id) select "generate_series", 13 from generate_series((select max(id) from testtbl), (select max(id) from testtbl)+563638);
insert into testtbl (id, par_id) select "generate_series", 11 from generate_series((select max(id) from testtbl), (select max(id) from testtbl)+633764);
insert into testtbl (id, par_id) select "generate_series", 18 from generate_series((select max(id) from testtbl), (select max(id) from testtbl)+994442);
insert into testtbl (id, par_id) select "generate_series", 15 from generate_series((select max(id) from testtbl), (select max(id) from testtbl)+3947049);
insert into testtbl (id, par_id) select "generate_series", 21 from generate_series((select max(id) from testtbl), (select max(id) from testtbl)+4257262);
delete from testtbl where id = 0 and par_id = 0;
create index testtbl_paridx on testtbl (par_id);
create table parameter (id integer);
insert into parameter select * from generate_series (1, 28);
analyze testtbl;

然后,如果我运行查询:

testdb=# explain analyze select * from parameter where exists (select 1 from testtbl where testtbl.par_id = parameter.id limit 1);
                                                           QUERY PLAN                                                            
---------------------------------------------------------------------------------------------------------------------------------
 Seq Scan on parameter  (cost=0.00..643.29 rows=1200 width=4) (actual time=4083.514..54216.575 rows=15 loops=1)
   Filter: (SubPlan 1)
   Rows Removed by Filter: 13
   SubPlan 1
     ->  Limit  (cost=0.00..0.25 rows=1 width=0) (actual time=1936.299..1936.299 rows=1 loops=28)
           ->  Seq Scan on testtbl  (cost=0.00..369619.35 rows=1455927 width=0) (actual time=1936.294..1936.294 rows=1 loops=28)
                 Filter: (par_id = parameter.id)
                 Rows Removed by Filter: 14870626
 Planning time: 0.151 ms
 Execution time: 54216.620 ms
(10 rows)

如果我禁用顺序扫描:

testdb=# set local enable_seqscan = off;
SET

testdb=# explain analyze select * from parameter where exists (select 1 from testtbl where testtbl.par_id = parameter.id limit 1);
                                                                      QUERY PLAN                                                                       
-------------------------------------------------------------------------------------------------------------------------------------------------------
 Seq Scan on parameter  (cost=10000000000.00..10000001395.02 rows=1200 width=4) (actual time=0.077..0.563 rows=15 loops=1)
   Filter: (SubPlan 1)
   Rows Removed by Filter: 13
   SubPlan 1
     ->  Limit  (cost=0.44..0.57 rows=1 width=0) (actual time=0.019..0.019 rows=1 loops=28)
           ->  Index Only Scan using ix_testtbl_par on testtbl  (cost=0.44..188678.87 rows=1455927 width=0) (actual time=0.018..0.018 rows=1 loops=28)
                 Index Cond: (par_id = parameter.id)
                 Heap Fetches: 15
 Planning time: 0.169 ms
 Execution time: 0.605 ms
(10 rows)

速度快,但有点黑客。您希望使用SET LOCAL,以便不对所有查询禁用顺序扫描。 SET LOCAL在事务提交之前有效。

更新:更好的选择是按照a_horse_with_no_name的建议完全删除LIMIT 1。

testdb=# explain analyze select * from parameter where exists (select 1 from testtbl where testtbl.par_id = parameter.id );
                                                                  QUERY PLAN                                                                  
----------------------------------------------------------------------------------------------------------------------------------------------
 Nested Loop Semi Join  (cost=0.44..1591.08 rows=1200 width=4) (actual time=0.070..0.492 rows=15 loops=1)
   ->  Seq Scan on parameter  (cost=0.00..34.00 rows=2400 width=4) (actual time=0.010..0.018 rows=28 loops=1)
   ->  Index Only Scan using testtbl_paridx on testtbl  (cost=0.44..29379.76 rows=1455923 width=4) (actual time=0.016..0.016 rows=1 loops=28)
         Index Cond: (par_id = parameter.id)
         Heap Fetches: 15
 Planning time: 0.216 ms
 Execution time: 0.532 ms
(7 rows)