Linux 上的 PostgreSQL 9.6,tags_tmp表大小~ 30 GB(1000 万行),tags是一个text[]并且只有 6 个值。
tags_tmp(id int, tags text[], maker_date timestamp, value text)
Run Code Online (Sandbox Code Playgroud)
tags_tmp(id int, tags text[], maker_date timestamp, value text)
Run Code Online (Sandbox Code Playgroud)
我需要使用 filter ontags和order byon检索数据maker_date desc。我可以在两tags & maker_date desc列上创建索引吗?
如果没有,你能提出其他想法吗?
select id, tags, maker_date, value
from tags_tmp
where tags && array['a','b']
order by maker_date desc
limit 5 offset 0
Run Code Online (Sandbox Code Playgroud)
SQL 代码:
create index idx1 on tags_tmp using gin (tags);
create …Run Code Online (Sandbox Code Playgroud) postgresql performance order-by index-tuning postgresql-performance
我有一个基于日期字段和数字字段的订单查询 Postgres 中的表,该表有 1000000 条记录
表的数据类型为:
fcv_id = serial
fcv_fecha_comprobante = timestamp without time zone
fcv_numero_comprobante = varchar(60)
Run Code Online (Sandbox Code Playgroud)
查询是:
SELECT fcv_id, fcv_fecha_comprobante FROM factura_venta
ORDER BY fcv_fecha_comprobante, fcv_numero_comprobante
Run Code Online (Sandbox Code Playgroud)
这个查询大约需要 5 秒,但如果我取出“order by”,查询只需要 0.499 秒
我遇到的问题是我需要在尽可能短的时间内运行这个查询,所以我在谷歌上搜索我可以做什么并使用以下查询创建一个复合索引
CREATE INDEX factura_venta_orden ON factura_venta
USING btree (fcv_fecha_comprobante ASC NULLS LAST
, fcv_numero_comprobante ASC NULLS LAST);
ALTER TABLE factura_venta CLUSTER ON factura_venta_orden;
Run Code Online (Sandbox Code Playgroud)
但是查询花费的时间相同甚至更多。
我使用的是 Postgres 9.0.13,这里是 73436 行的 EXPLAIN
Sort (cost=11714.03..11897.62 rows=73436 width=27) (actual time=1260.759..1579.853 rows=73436 loops=1)
Sort Key: fcv_fecha_comprobante, fcv_numero_comprobante
Sort Method: external merge …Run Code Online (Sandbox Code Playgroud)