簡體   English   中英

Postgresql,postgres表中的慢速SELECT查詢

[英]Postgresql, slow SELECT query in postgres table

我們在Postgres數據庫中選擇數據時遇到性能問題。

我有兩個表,Fleet_test和Fleet_inspection_test。
Fleet_test包含94列作為鍵:(fleet_id,usdot,country_code,contacts_count,....),而Fleet_inspection_test包含9列作為鍵:(inspection_id,dot_number,insp_date ...)。
Fleet_test大約有46萬行,而Fleet_inspection_test大約有600萬行。

我需要按以下方式運行查詢,在數據庫中運行大約需要4秒鍾。

select sum(contacts_count) as sum from fleet_test
where country_code = 'US'
and usdot in (select  dot_number from fleet_inspection_test
            where insp_date between '2016-02-15' and '2016-12-15');

如果我只運行主查詢,大約需要600 mesc:

select sum(contacts_count) as sum from fleet_test
where country_code = 'US'

我們為每一列都有索引。 不幸的是結果並不那么好:

CREATE INDEX ix_fleet_test_contacts_count ON fleet_test USING btree (contacts_count);
CREATE INDEX ix_fleet_test_country_code ON fleet_test USING btree (country_code);
CREATE INDEX ix_fleet_test_usdot ON fleet_test USING btree (usdot);
CREATE INDEX ix_fleet_inspection_test_dot_number ON fleet_inspection_test USING btree (dot_number);
CREATE INDEX ix_fleet_inspection_test_insp_date ON fleet_inspection_test USING btree (insp_date);

查詢計划

'Aggregate  (cost=198451.70..198451.71 rows=1 width=4) (actual time=12300.010..12300.011 rows=1 loops=1)'
'  Buffers: shared hit=89350'
'  ->  Hash Join  (cost=152330.84..198276.23 rows=70189 width=4) (actual time=10902.309..12168.892 rows=189740 loops=1)'
'        Hash Cond: (fleet_test.usdot = fleet_inspection_test.dot_number)'
'        Buffers: shared hit=89350'
'        ->  Seq Scan on fleet_test  (cost=0.00..43722.72 rows=444397 width=8) (actual time=0.015..562.385 rows=443616 loops=1)'
'              Filter: ((country_code)::text = 'US'::text)'
'              Rows Removed by Filter: 14726'
'              Buffers: shared hit=37993'
'        ->  Hash  (cost=151817.53..151817.53 rows=41065 width=4) (actual time=10898.319..10898.319 rows=356230 loops=1)'
'              Buckets: 524288 (originally 65536)  Batches: 1 (originally 1)  Memory Usage: 16620kB'
'              Buffers: shared hit=51357'
'              ->  HashAggregate  (cost=151406.88..151817.53 rows=41065 width=4) (actual time=10240.109..10604.871 rows=356231 loops=1)'
'                    Group Key: fleet_inspection_test.dot_number'
'                    Buffers: shared hit=51357'
'                    ->  Seq Scan on fleet_inspection_test  (cost=0.00..138905.67 rows=5000485 width=4) (actual time=126.081..5077.200 rows=5007065 loops=1)'
'                          Filter: ((insp_date >= '2016-02-15'::date) AND (insp_date <= '2016-12-15'::date))'
'                          Rows Removed by Filter: 829513'
'                          Buffers: shared hit=51357'
'Planning time: 1.092 ms'
'Execution time: 12302.407 ms'

這是我的查詢中的一個EXPLAIN(ANALYZE,BUFFERS)示例。 我的期望是使執行時間少於一秒鍾。

這些是postgres db config的一些參數:
操作系統:CentOS
系統內存:16GB

work_mem = 768MB
shared_buffers = 2048MB

任何幫助,評論或想法都將不勝感激!

先感謝您。

試試這個(第3版)

    SELECT SUM(contacts_count) AS sum 
      FROM fleet_test AS t
      JOIN (SELECT DISTINCT dot_number 
              FROM fleet_inspection_test
             WHERE insp_date BETWEEN '2016-02-15' AND '2016-12-15') AS d
        ON (t.usdot = d.dot_number)
     WHERE t.country_code = 'US';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM