我有这样的查询:
SELECT t0.id
FROM platform_conversations t0
LEFT OUTER JOIN contacts t1 ON t1.id = t0.contact_id
WHERE t0.user_id = 5340
AND (
t0.participant ILIKE '%baa%' -- (1)
OR t1.first_name ILIKE '%baa%' -- (2)
)
LIMIT 50;
Run Code Online (Sandbox Code Playgroud)
和
CREATE INDEX ix_conversations_participant
ON platform_conversations USING GIN (participant gin_trgm_ops);
CREATE INDEX ix_trgm_contacts_search
ON contacts USING GIN (first_name gin_trgm_ops);
Run Code Online (Sandbox Code Playgroud)
并且无法弄清楚为什么索引不与OR条件一起使用。如果我只使用 (1),或者只使用 (2),或者使用AND,它们都被使用。
这是计划:
仅适用于 (1)
Limit (cost=12.43..203.68 rows=50 width=37) (actual time=0.037..0.037 rows=0 loops=1)
-> Bitmap Heap Scan on platform_conversations t0 (cost=12.43..222.80 …Run Code Online (Sandbox Code Playgroud)