高級:我能做到這一點order by
,group by
基於sum
任何更快嗎? (PG 8.4,FWIW,在非小桌子....認爲O(百萬行))算法改進
假設我有一個像這樣的表:
Table "public.summary"
Column | Type | Modifiers
-------------+-------------------+------------------------------------------------------
ts | integer | not null default nextval('summary_ts_seq'::regclass)
field1 | character varying | not null
otherfield | character varying | not null
country | character varying | not null
lookups | integer | not null
Indexes:
"summary_pk" PRIMARY KEY, btree (ts, field1, otherfield, country)
"ix_summary_country" btree (country)
"ix_summary_field1" btree (field1)
"ix_summary_otherfield" btree (otherfield)
"ix_summary_ts" btree (ts)
而且我想查詢:
select summary.field1,
summary.country,
summary.ts,
sum(summary.lookups) as lookups,
from summary
where summary.country = 'za' and
summary.ts = 1275177600
group by summary.field1, summary.country, summary.ts
order by summary.ts, lookups desc, summary.field1
limit 100;
(英文:前100字段1的在特定(TS,國家),其中 'topness' 是查找的任何匹配行的總和 ,不管的otherfield值)
有什麼東西我真的可以加快這個速度嗎?算法 這似乎是一個全表掃描的東西,但我可能會錯過一些東西。
+1:格式良好,並使用序列填充pk! – 2010-07-01 21:40:16
'LIMIT 100'意味着只有100行將被返回,而不是每個ts/country /等的前100名。 – 2010-07-01 21:42:56
格式化提示爲SO,記得把它全部寫成小寫,奇怪的是:) – 2010-07-02 13:46:27