PostgreSQL慢查询,按LIMIT排序

时间:2016-12-01 01:40:50

标签: postgresql query-performance postgresql-9.4

  • 列表项

mydbab=# \d+ table1;

                   Table "dbmydb.table1"
     Column     |  Type   | Modifiers | Scol5rage  | Stats target | Description 
----------------+---------+-----------+------------+--------------+-------------
 pk             | bigint  | not null  | plain      |              |   
 col1           | bigint  | not null  | plain      |              |   
 col2           | citext  | not null  | extended   |              |   
 col3           | citext  |           | extended   |              |        
 col4           | citext  | not null  | extended   |              |   
 col5           | citext  |           | extended   |              |   
 col6           | citext  |           | extended   |              |   
 bcol7          | citext  |           | extended   |              |   
 col8           | citext  |           | extended   |              |   
 col9           | citext  |           | extended   |              |  
 col10          | bigint  | not null  | plain      |              |   
 col11          | citext  |           | extended   |              |   
 col12          | bigint  |           | plain      |              |     
 col13          | integer | default 1 | plain      |              |   
 col14          | integer | default 0 | plain      |              |   
 col15          | bigint  |           | plain      |              |   
 col16          | integer | default 0 | plain      |              |   
 col17          | integer | default 0 | plain      |              |     
 col18          | integer | default 0 | plain      |              |   
 col19          | citext  |           | extended   |              |   
 col20          | citext  |           | extended   |              |   
 col21          | citext  |           | extended   |              | 

索引:

"table1_pk" PRIMARY KEY, btree (pk)  
"table1_idx3" btree (col10)  
"table1_idx4" btree ("col4")  
"table1_idx5" btree (col11)  
"table1_idx6" btree (col15)  
"table1_fk_idx" btree (col1)

检查约束:

"table1_col6_c" CHECK (length(col6::text) <= 5000)
"table1_col4_c" CHECK (length("col4"::text) <= 253)
"table1_col5_c” CHECK (length(col5::text) <= 5000)
"table1_col3_c" CHECK (length(col3::text) <= 253)
"table1_col11_c" CHECK (length(col11::text) <= 253)
"table1_col20_c” CHECK (length(col20::text) <= 200)
"table1_col21_c” CHECK (length(col21::text) <= 500)
"table1_col7_c” CHECK (length(col7::text) <= 500)
"table1_col9_c" CHECK (length(col9::text) <= 100)
"table1_col5_c" CHECK (length("col5"::text) <= 5000)
"table1_col19_c” CHECK (length(col19::text) <= 100)

外键约束:

"table1_fk" FOREIGN KEY (col1) REFERENCES table2(col1) ON DELETE CASCADE

参考:

TABLE “table3” CONSTRAINT “table3_fk2" FOREIGN KEY (pk) REFERENCES table1(pk) ON DELETE CASCADE  
TABLE “table4” CONSTRAINT “table4_fk2" FOREIGN KEY (pk) REFERENCES table1(pk) ON DELETE CASCADE  
TABLE “table5” CONSTRAINT “table5_fk1" FOREIGN KEY (pk) REFERENCES table1(pk)  
TABLE “table6” CONSTRAINT “table6_fk" FOREIGN KEY (pk) REFERENCES table1(pk) ON DELETE CASCADE  
TABLE “table7” CONSTRAINT “table7_fk1" FOREIGN KEY (pk) REFERENCES table1(pk) ON DELETE CASCADE  

选项:autovacuum_vacuum_scale_factor=0.05

查询计划

explain SELECT * FROM table1 WHERE ((table1.col1 = 814000000002054) AND (((table1.pk >= 238000000000000) AND (table1.pk <= 238999999999999)) OR  ((table1.pk >= 0) AND (table1.pk <= 999999999999))))   ORDER BY table1.col15 DESC LIMIT 7
(query gets timed out for explain analyze) 

Limit (cost=0.56..607.64 rows=7 width=603)

Index Scan Backward using table1_idx6 on table1 (cost=0.56..12622561.13 rows=145548 width=603) Filter: ((col1 = 814000000002054::bigint) AND (((pk >= 238000000000000::bigint) AND (pk <= 238999999999999::bigint)) OR ((pk >= 0) AND (pk <= 999999999999::bigint))))

问题: 表有50M的记录。 此查询运行时间超过15分钟!我们已经校准到自动真空到0.05。自动分析是PG的默认值。 查询甚至更改为ORDER BY table1.col15 ASC LIMIT 7,即使查询大约在同一时间。 插入/更新操作只需ms。

历史:当数据较少时,结果会更快 - 查询在MySQL中没有问题,几周前就迁移到了Pg。

Worker_mem是956MB。

select version();

 PostgreSQL 9.4.0 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54), 64-bit
(1 row)

1 个答案:

答案 0 :(得分:0)

尝试创建多列索引:

CREATE INDEX ON table1(col15, col1, pk);