我有一个1200万行表,所以不是很大,但我想尽可能地优化它的读取。
例如当前正在运行
SELECT *
FROM hp.historicalposition
WHERE instrumentid = 1167 AND fundid = 'XXX'
ORDER BY date;
返回4200行,第一次运行时大约需要4秒,第二次运行需要1秒。
哪些指数可能会有所帮助,还有其他建议吗?
CREATE TABLE hp.historicalposition ( date date NOT NULL, fundid character(3) NOT NULL, instrumentid integer NOT NULL, quantityt0 double precision, quantity double precision, valuation character varying, fxid character varying, localt0 double precision, localt double precision, CONSTRAINT attrib_fund_fk FOREIGN KEY (fundid) REFERENCES funds (fundid) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT attrib_instr_fk FOREIGN KEY (instrumentid) REFERENCES instruments (instrumentid) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION )
答案 0 :(得分:1)
以下是您的查询:
SELECT *
FROM hp.historicalposition
WHERE instrumentid = 1167 AND fundid = 'XXX'
ORDER BY date;
最佳指数是综合指数:
create index idx_historicalposition_instrumentid_fundid_date) on historicalposition(instrumentid, fundid, date);
这符合where
条款,也可以用于order by
。
答案 1 :(得分:1)
create index hp.historicalposition_instrumentid_fundid_idx on hp.historicalposition(instrumentid,fundid);
cluster hp.historicalposition using hp.historicalposition_instrumentid_fundid_idx;
答案 2 :(得分:0)
一般的想法,不一定都适用于postgresql(事实上,它们来自Oracle世界):