I have this for query for a table of a size of 2 700 000 rows in Heroku Postgres 12.16:
SELECT 1 AS one
FROM "users"
WHERE (
external_id = 18
AND (email = user@example.com
OR uuid = 779c7963-67b2-43ea-b19b-028759a146dc ))
LIMIT 1
It takes 1497.968 ms to execute.
在<代码>EXPLAIN ANALYZE上填报如下:
"Limit (cost=1000.11..41646.06 rows=1 width=4) (actual time=491.141..1497.948 rows=1 loops=1)"
" -> Gather (cost=1000.11..82292.01 rows=2 width=4) (actual time=491.140..1497.946 rows=1 loops=1)"
" Workers Planned: 1"
" Workers Launched: 1"
" -> Parallel Index Only Scan using index_users_on_whitelabel_id_and_email_and_uuid on users (cost=0.11..81291.81 rows=1 width=4) (actual time=953.762..953.762 rows=0 loops=2)"
" Index Cond: (whitelabel_id = 18)"
" Filter: (((email)::text = user@example.com ::text) OR ((uuid)::text = 779c7963-67b2-43ea-b19b-028759a146dc ::text))"
" Rows Removed by Filter: 1111474"
" Heap Fetches: 3238"
"Planning Time: 0.139 ms"
"Execution Time: 1497.968 ms"
I see the total cost is too high (81291.81) and I suspect is the reason for the query run so slow.
Why is the reason of the total cost and how I can optimize it?
我已经运行了<代码>VACCUUM ANALYZE 用户,预计这将改进询问,但保持了同样的水平。