我不明白解释慢查询的结果
I don't understand explain result of a slow query
我的查询很慢(> 1 秒)。这是该查询的 explain analyze
结果:
Nested Loop Left Join (cost=0.42..32275.13 rows=36 width=257) (actual time=549.409..1106.044 rows=2 loops=1)
Join Filter: (answer.lt_surveyee_survey_id = lt_surveyee_survey.id)
-> Index Scan using lt_surveyee_survey_id_key on lt_surveyee_survey (cost=0.42..8.44 rows=1 width=64) (actual time=0.108..0.111 rows=1 loops=1)
Index Cond: (id = 'xxxxx'::citext)
-> Seq Scan on answer (cost=0.00..32266.24 rows=36 width=230) (actual time=549.285..1105.910 rows=2 loops=1)
Filter: (lt_surveyee_survey_id = 'xxxxx'::citext)
Rows Removed by Filter: 825315
Planning time: 0.592 ms
Execution time: 1106.124 ms
结果的 xxxxx
部分类似于 uuid。我没有建立那个数据库,所以我现在不知道。这是查询:
EXPLAIN ANALYZE SELECT
lt_surveyee_survey.id
-- +Some other fields
FROM lt_surveyee_survey
LEFT JOIN answer ON answer.lt_surveyee_survey_id = lt_surveyee_survey.id
WHERE lt_surveyee_survey.id = 'xxxxx';
根据 EXPLAIN ANALYZE
输出,您的 JOIN
导致性能下降。您可以看到有 2 个不同的查找,一个用了几毫秒,另一个用了半秒。
区别由行的开头指示:Index Scan
和 Seq Scan
,其中 Seq
是顺序的缩写,意味着必须检查所有行DBMS 处理 JOIN
。顺序扫描发生的原因是正在检查的列上 缺少索引 (在您的情况下为 answer.lt_surveyee_survey_id
)。
添加索引应该可以解决性能问题。
我的查询很慢(> 1 秒)。这是该查询的 explain analyze
结果:
Nested Loop Left Join (cost=0.42..32275.13 rows=36 width=257) (actual time=549.409..1106.044 rows=2 loops=1)
Join Filter: (answer.lt_surveyee_survey_id = lt_surveyee_survey.id)
-> Index Scan using lt_surveyee_survey_id_key on lt_surveyee_survey (cost=0.42..8.44 rows=1 width=64) (actual time=0.108..0.111 rows=1 loops=1)
Index Cond: (id = 'xxxxx'::citext)
-> Seq Scan on answer (cost=0.00..32266.24 rows=36 width=230) (actual time=549.285..1105.910 rows=2 loops=1)
Filter: (lt_surveyee_survey_id = 'xxxxx'::citext)
Rows Removed by Filter: 825315
Planning time: 0.592 ms
Execution time: 1106.124 ms
结果的 xxxxx
部分类似于 uuid。我没有建立那个数据库,所以我现在不知道。这是查询:
EXPLAIN ANALYZE SELECT
lt_surveyee_survey.id
-- +Some other fields
FROM lt_surveyee_survey
LEFT JOIN answer ON answer.lt_surveyee_survey_id = lt_surveyee_survey.id
WHERE lt_surveyee_survey.id = 'xxxxx';
根据 EXPLAIN ANALYZE
输出,您的 JOIN
导致性能下降。您可以看到有 2 个不同的查找,一个用了几毫秒,另一个用了半秒。
区别由行的开头指示:Index Scan
和 Seq Scan
,其中 Seq
是顺序的缩写,意味着必须检查所有行DBMS 处理 JOIN
。顺序扫描发生的原因是正在检查的列上 缺少索引 (在您的情况下为 answer.lt_surveyee_survey_id
)。
添加索引应该可以解决性能问题。