0
私は様々なpersonidと種類に対してこのクエリを見ましたが、10ミリ秒以下で、16分もかかることがわかりました。何が起こっている?Amazon EC2 m1.largeのPostgres 9.1は時には本当に遅いクエリを持っています
Column | Type | Modifiers
--------------+------------------+--------------------------------------------------------
id | bigint | not null default nextval('record_seq'::regclass)
type | integer | not null
personid | integer |
reporttime | bigint | not null
totalreading | double precision | not null
delta | double precision | not null
Indexes:
"record_pkey" PRIMARY KEY, btree (id)
"record_personid_idx" btree (personid)
"record_type_idx" btree (type)
"record_reporttime_idx" btree (reporttime) CLUSTER
これは、時には本当に遅いクエリの説明分析です。
explain analyze SELECT ID, TYPE, REPORTTIME, TOTALREADING, DELTA, PERSONID FROM RECORD WHERE PERSONID=1103 AND TYPE=405 AND REPORTTIME <= 1332447354000 ORDER BY REPORTTIME DESC LIMIT 1;
Limit (cost=0.00..327.93 rows=1 width=52) (actual time=239749.274..239749.274 rows=0 loops=1)
-> Index Scan Backward using record_reporttime_idx on record (cost=0.00..1196290.82 rows=3648 width=52) (actual time=239749.251..239749.251 rows=0 loops=1)
Index Cond: (reporttime <= 1332447354000::bigint)
Filter: ((personid = 1103) AND (type = 405))
Total runtime: 239749.409 ms
約10-20種類あり、2つが最も頻繁に使用され、405はほとんど使用されません。
select count(*) from record;
count
----------
30420232
SELECT COUNT(*) FROM record WHERE PERSONID=1103 AND TYPE=405;
count
-------
58
問題は、そのクエリに一致するレコードがなかった場合、postgresはテーブル全体をスキャンする必要があるということでした。 比較的新しいタイムスタンプを持つデータがあった場合、クエリはすぐに戻ります。 – xordon