2013-03-20 692 views
1

该查询运行速度非常慢。为什么?其他人都很好。索引是好的,我想。Postgresql查询速度很慢

explain analyze 
select "e_inst"."si_id" as "c0" 
from "e_inst" as "e_inst" 
group by "e_inst"."si_id" 
order by "e_inst"."si_id" ASC NULLS LAST 

查询计划:

Sort (cost=12221.87..12221.90 rows=68 width=4) (actual time=1115.377..1115.433 rows=81 loops=1) 
    Sort Key: si_id 
    Sort Method: quicksort Memory: 28kB 
    -> HashAggregate (cost=12221.25..12221.45 rows=68 width=4) (actual time=1115.198..1115.261 rows=81 loops=1) 
     -> Seq Scan on e_inst (cost=0.00..11920.07 rows=602357 width=4) (actual time=0.021..611.570 rows=602357 loops=1) 
Total runtime: 1115.538 ms 

创建表和索引:

CREATE TABLE e_inst (
    id integer NOT NULL, 
    ip numeric, 
    gu character varying, 
    referrer character varying, 
    proc integer, 
    loke_id integer, 
    top_id integer, 
    si_id integer, 
    kop integer, 
    count integer, 
    created integer, 
    modified integer, 
    timepop integer, 
    count_active character varying, 
    country character(3), 
    info character varying 
); 

CREATE INDEX "topEnhance" ON e_inst USING btree (created, top_id); 
CREATE INDEX "procEnhance" ON e_inst USING btree (created, proc); 
CREATE INDEX "countryEnhance" ON e_install USING btree (created, country); 
CREATE INDEX "createdE" ON e_inst USING btree (created); 
ALTER TABLE e_inst CLUSTER ON "createdE"; 
CREATE INDEX "lokeE" ON e_inst USING btree (loke_id); 
CREATE INDEX "lokeEnhance" ON e_inst USING btree (created, loke_id); 
CREATE INDEX "siE" ON e_inst USING btree (si_id); 
CREATE INDEX "siEnhance" ON e_inst USING btree (created, si_id); 
CREATE INDEX "kopEnhance" ON e_inst USING btree (created, kop); 
+0

另外:如果索引数据没有高基数,索引不会有帮助。 – bernie 2013-03-20 22:23:02

+0

对不起,作为PGSQL noob,什么是高基数? – 2013-03-20 22:27:01

+1

因为人群可以比我更好地解释:http://en.wikipedia.org/wiki/Cardinality_(SQL_statements) – bernie 2013-03-20 22:27:53

回答

0

升级到PostgreSQL 9.2。现在这只是一个索引只扫描! 工程很好,感谢a_horse_with_no_name谁建议我升级。

3

指标都不会通过处理整个表的查询使用。

事实是,您正在检索和处理 600k条记录。它在一秒钟之内完成,实际上令人印象深刻。

现在在这种情况下,您正试图从600k条记录中提取81个不同的值。你可能想要做的是构造一个递归查询,使其获取81行一行。这可能会更快,但没有保证。通常我使用返回的行数少得多的地方。然而,这里是一个例子:

WITH RECURSIVE sparse_scan AS (
    SELECT min(si_id) as si_id FROM e_inst 
    UNION ALL 
    SELECT min(si_id) as si_id 
     FROM e_inst 
     JOIN (select max(si_id) as last FROM sparse_scan) s 
    WHERE s.last < si_id 
) 
SELECT si_id as c0 FROM sparse_scan; 

请注意,这取代了81索引扫描顺序扫描。

+0

我想这样做,但我无法更改查询,因为Mondrian构建了我的查询。 – 2013-03-21 13:17:13