0
下面的查询运行在Postgres的了解缓慢位图扫描堆在LIKE查询
SELECT COUNT(*)
FROM "posts"
WHERE "posts"."author_id" = 20
AND ("posts"."title" ILIKE '123');
显然涉及到一个很慢的位图堆扫描。我如何能够诊断这种缓慢的原因?我能做些什么来让它表现更好?
my-app::DATABASE=> EXPLAIN ANALYZE VERBOSE
my-app::DATABASE-> SELECT COUNT(*)
my-app::DATABASE-> FROM "posts"
my-app::DATABASE-> WHERE "posts"."author_id" = 20
my-app::DATABASE-> AND ("posts"."title" ILIKE '123');
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=25516.64..25516.64 rows=1 width=0) (actual time=35632.267..35632.267 rows=1 loops=1)
Output: count(*)
-> Bitmap Heap Scan on public.posts (cost=307.46..25516.64 rows=1 width=0) (actual time=35632.264..35632.264 rows=0 loops=1)
Recheck Cond: (posts.author_id = 20)
Filter: ((posts.title)::text ~~* '123'::text)
Rows Removed by Filter: 22216
Heap Blocks: exact=15419
-> Bitmap Index Scan on index_posts_on_author_id_and_state (cost=0.00..307.46 rows=23586 width=0) (actual time=54.585..54.585 rows=22235 loops=1)
Index Cond: (posts.author_id = 20)
Planning time: 0.853 ms
Execution time: 35632.405 ms
(11 rows)
为什么在没有通配符时使用'ILIKE'? – dnoeth
它只是探索查询策略,但通常它可能会搜索后缀或前缀。 – sunless
如果标题不是很大,在'(author_id,title)'上创建一个索引 –