2
我在PostgreSQL 9.0.3數據庫中擁有一個簡單的表,該數據庫包含來自風力渦輪機控制器的輪詢數據。每行表示特定時間的特定傳感器的值。目前該表有大約90M行:緩慢postgresql查詢是否where子句匹配沒有行
wtdata=> \d integer_data
Table "public.integer_data"
Column | Type | Modifiers
--------+--------------------------+-----------
date | timestamp with time zone | not null
name | character varying(64) | not null
value | integer | not null
Indexes:
"integer_data_pkey" PRIMARY KEY, btree (date, name)
"integer_data_date_idx" btree (date)
"integer_data_name_idx" btree (name)
一個查詢,我需要的是找到最後一次的變量被更新:
select max(date) from integer_data where name = '<name of variable>';
此查詢工作正常存在的變量進行搜索時在表:
wtdata=> select max(date) from integer_data where name = 'STATUS_OF_OUTPUTS_UINT16';
max
------------------------
2011-04-11 02:01:40-05
(1 row)
但是,如果我嘗試尋找不存在於表中的變量,查詢掛起(或需要更長的時間比我有耐心):
select max(date) from integer_data where name = 'Message';
我已經讓查詢運行了幾個小時,有時幾天沒有結束的跡象。有與NAME =「消息」的表中沒有行:
wtdata=> select count(*) from integer_data where name = 'Message';
count
-------
0
(1 row)
我不明白爲什麼一個查詢快,其他的需要永遠。查詢以某種方式被強制掃描整個表?
wtdata=> explain select max(date) from integer_data where name = 'Message';
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
Result (cost=13.67..13.68 rows=1 width=0)
InitPlan 1 (returns $0)
-> Limit (cost=0.00..13.67 rows=1 width=8)
-> Index Scan Backward using integer_data_pkey on integer_data (cost=0.00..6362849.53 rows=465452 width=8)
Index Cond: ((date IS NOT NULL) AND ((name)::text = 'Message'::text))
(5 rows)
下面是一個快速查詢的查詢計劃:
wtdata=> explain select max(date) from integer_data where name = 'STATUS_OF_OUTPUTS_UINT16';
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------
Result (cost=4.64..4.65 rows=1 width=0)
InitPlan 1 (returns $0)
-> Limit (cost=0.00..4.64 rows=1 width=8)
-> Index Scan Backward using integer_data_pkey on integer_data (cost=0.00..16988170.38 rows=3659570 width=8)
Index Cond: ((date IS NOT NULL) AND ((name)::text = 'STATUS_OF_OUTPUTS_UINT16'::text))
(5 rows)
你在這張桌子上分析過嗎? – Kuberchaun 2011-04-11 16:15:18
是的,我手動運行分析,加上autovacuum正在運行。 – jcollie 2011-04-11 16:17:40
你有沒有機會在壓縮文件中共享你的數據庫?看起來像一個很好的學習機會? – Kuberchaun 2011-04-11 16:22:49