我在 Postgres 数据库中有一个缓慢的查询。使用
explain analyze
,我可以看到 Postgres 对两个不同的索引进行位图索引扫描,然后对两个结果集进行位图 AND 扫描。
删除其中一个索引会使评估速度提高十倍(第一个索引仍然使用位图索引扫描)。但是,删除的索引在其他查询中很有用。
查询:
select
booking_id
from
booking
where
substitute_confirmation_token is null
and date_trunc('day', from_time) >= cast('01/25/2016 14:23:00.004' as date)
and from_time >= '01/25/2016 14:23:00.004'
and type = 'LESSON_SUBSTITUTE'
and valid
order by
booking_id;
索引:
"idx_booking_lesson_substitute_day" btree (date_trunc('day'::text, from_time)) WHERE valid AND type::text = 'LESSON_SUBSTITUTE'::text
"booking_substitute_confirmation_token_key" UNIQUE CONSTRAINT, btree (substitute_confirmation_token)
查询计划:
Sort (cost=287.26..287.26 rows=1 width=8) (actual time=711.371..711.377 rows=44 loops=1)
Sort Key: booking_id
Sort Method: quicksort Memory: 27kB
Buffers: shared hit=8 read=7437 written=1
-> Bitmap Heap Scan on booking (cost=275.25..287.25 rows=1 width=8) (actual time=711.255..711.294 rows=44 loops=1)
Recheck Cond: ((date_trunc('day'::text, from_time) >= '2016-01-25'::date) AND valid AND ((type)::text = 'LESSON_SUBSTITUTE'::text) AND (substitute_confirmation_token IS NULL))
Filter: (from_time >= '2016-01-25 14:23:00.004'::timestamp without time zone)
Buffers: shared hit=5 read=7437 written=1
-> BitmapAnd (cost=275.25..275.25 rows=3 width=0) (actual time=711.224..711.224 rows=0 loops=1)
Buffers: shared hit=5 read=7433 written=1
-> Bitmap Index Scan on idx_booking_lesson_substitute_day (cost=0.00..20.50 rows=594 width=0) (actual time=0.080..0.080 rows=72 loops=1)
Index Cond: (date_trunc('day'::text, from_time) >= '2016-01-25'::date)
Buffers: shared hit=5 read=1
-> Bitmap Index Scan on booking_substitute_confirmation_token_key (cost=0.00..254.50 rows=13594 width=0) (actual time=711.102..711.102 rows=2718734 loops=1)
Index Cond: (substitute_confirmation_token IS NULL)
Buffers: shared read=7432 written=1
Total runtime: 711.436 ms
我可以阻止在 Postgres 中对特定查询使用特定索引吗?
对于您的特定情况,部分唯一索引仅涵盖稀有值,因此 Postgres 不会(不能)使用常见
NULL
值的索引。
CREATE UNIQUE INDEX booking_substitute_confirmation_uni
ON booking (substitute_confirmation_token)
WHERE substitute_confirmation_token IS NOT NULL;
这是部分索引的教科书用例。 字面意思!该手册有一个类似的示例以及与之完美匹配的建议:
最后,部分索引也可以用来覆盖系统的 查询计划选择。此外,具有特殊分布的数据集可能 导致系统在不应该使用索引的情况下使用索引。在那里面 在这种情况下,可以设置索引,使其不可用于 违规查询。通常,PostgreSQL 会做出合理的选择 索引的使用(例如,它在检索通用值时避免使用它们,所以 前面的例子实际上只节省了索引大小,这不是必需的 以避免索引使用),并且严重错误的计划选择是原因 获取错误报告。
请记住,设置部分索引表明您知道 至少与查询计划者知道的一样多,特别是你知道的 当指数可能有利可图时。形成这些知识需要 体验并了解 PostgreSQL 中的索引如何工作。在 大多数情况下,部分索引相对于常规索引的优势 将是最小的。在某些情况下,它们会适得其反[...]
您评论:
该表有几百万行,只有几千行不为空值。
所以这是一个完美的用例。它甚至会加速对
substitute_confirmation_token
的非空值的查询,因为索引现在小得多。
回答您原来的问题:不可能“禁用”特定查询的现有索引。你必须放弃它,但这太贵了。
您可以在事务中删除索引,运行您的
SELECT
,然后使用ROLLBACK
而不是提交。这快,但请注意(引用手册):
正常的
获取表上的独占锁,阻塞 其他访问,直到索引删除完成。DROP INDEX
因此这不适合在多用户环境中常规使用。
BEGIN;
DROP INDEX big_user_id_created_at_idx;
SELECT ...;
ROLLBACK; -- so the index is preserved after all
参见:
不过,通常情况下,提高列的
STATISTICS
目标就足够了,因此 Postgres 可以更可靠地识别常见值并避免为这些值建立索引。尝试:
ALTER TABLE booking ALTER COLUMN substitute_confirmation_token SET STATISTICS 1000;
然后:
ANALYZE booking;
,然后再次尝试查询。 1000 是一个示例值。相关: