我想创建一个Multicolumn表达式索引,但是当我创建索引时,会输出以下消息:
--detail message
wapgrowth=> create index CONCURRENTLY idx_test on tmp_table using btree (skyid, to_char(create_time, 'YYYY-MM-DD'), actiontype );
ERROR: functions in index expression must be marked IMMUTABLE
--table ddl
wapgrowth=> \d tmp_table
Table "wapgrowth.tmp_table"
Column | Type | Modifiers
-------------+-----------------------------+---------------
id | integer | not null
actiontype | character varying(20) |
apptype | character varying(20) |
score | integer |
create_time | timestamp without time zone | default now()
skyid | integer |
Indexes:
Run Code Online (Sandbox Code Playgroud) 我正在创建一个汇总表,汇总给定日期内的所有事件.
INSERT INTO graph_6(
day,
event_type,
(SELECT COUNT(*) FROM event e
WHERE event_type = e.event_type
AND creation_time::DATE = sq.day)
FROM event_type
CROSS JOIN
(SELECT generate_series(
(SELECT '2014-01-01'::DATE),
(SELECT '2014-01-02'::DATE),
'1 day') as day) sq;
Run Code Online (Sandbox Code Playgroud)
该creation_time列已编入索引:
CREATE INDEX event_creation_time_date_idx ON event USING BTREE(creation_time);
Run Code Online (Sandbox Code Playgroud)
但是,即使仅使用少量事件(2014年1月1日至2日)查询两天的数据,查询也会运行很长时间.
在EXPLAIN上查询是相当严峻-它运行一个顺序扫描的event表,而不是利用指数都:
-> Seq Scan on event e_1 (cost=0.00..12557.39 rows=531 width=38)
Filter: ... AND ((creation_time)::date = (generate_series(($12)::timestamp with time zone, ($13)::timestamp with time zone, '1 day'::interval))))
Run Code Online (Sandbox Code Playgroud)
我认为这是因为我们比较了一个铸造值 - creation_time::DATE而不是creation_time.我试过索引转换: …
我有一个超过450万行的表,我的SELECT查询对我的需求来说太慢了.
该表创建于:
CREATE TABLE all_legs (
carrier TEXT,
dep_hub TEXT,
arr_hub TEXT,
dep_dt TIMESTAMP WITH TIME ZONE,
arr_dt TIMESTAMP WITH TIME ZONE,
price_ct INTEGER,
... 5 more cols ...,
PRIMARY KEY (carrier, dep_hub, arr_hub, dep_dt, arr_dt, ...3 other cols...)
)
Run Code Online (Sandbox Code Playgroud)
当我想要SELECT特定日期的所有行时,查询太慢; 需要12秒到20秒.我的目标是最多需要1秒.我希望查询返回表中包含的行的0.1%和1%之间.
查询非常简单:
SELECT * FROM all_legs WHERE dep_dt::date = '2017-08-15' ORDER BY price_ct ASC
Run Code Online (Sandbox Code Playgroud)
EXPLAIN ANALYZE 收益:
Sort (cost=197154.69..197212.14 rows=22982 width=696) (actual time=14857.300..14890.565 rows=31074 loops=1)
Sort Key: price_ct
Sort Method: external merge Disk: 5256kB
-> …Run Code Online (Sandbox Code Playgroud)