我有一个包含worklogs
的主表和一些相关的表,例如users
,teams
等。我必须提供导出功能,该功能最多可以获取10万行并与3-4张桌子。当前,此任务在30-40秒内运行,这实在太多了。有什么方法可以改善此查询的性能?
查询本身
SELECT "trapping_snaring_trapwork"."id",
"trapping_snaring_trapwork"."added",
"trapping_snaring_trapwork"."added_by_id",
"trapping_snaring_trapwork"."import_id",
"trapping_snaring_trapwork"."team_id",
"trapping_snaring_trapwork"."date",
"contacts_contact"."id",
"contacts_contact"."added",
"contacts_contact"."added_by_id",
"contacts_contact"."team_id",
"contacts_contact"."first_name",
"contacts_contact"."last_name",
"trapping_snaring_trapfeature"."id",
"trapping_snaring_trapfeature"."added",
"trapping_snaring_trapfeature"."added_by_id",
"trapping_snaring_trapfeature"."team_id",
"trapping_snaring_trapfeature"."name",
"trapping_snaring_trapfeature"."description",
"lists_snaringtraptype"."id",
"lists_snaringtraptype"."added",
"lists_snaringtraptype"."added_by_id",
"lists_snaringtraptype"."team_id",
"lists_snaringtraptype"."name",
"lists_snaringtraptype"."description",
"trapping_snaring_groupfeature"."id",
"trapping_snaring_groupfeature"."added",
"trapping_snaring_groupfeature"."added_by_id",
"trapping_snaring_groupfeature"."team_id",
"trapping_snaring_groupfeature"."name",
"trapping_snaring_groupfeature"."description",
"trapping_snaring_groupfeature"."end_date",
"trapping_session"."id",
"trapping_session"."added",
FROM "trapping_snaring_trapwork"
INNER JOIN "contacts_contact" ON ("trapping_snaring_trapwork"."checker_id" = "contacts_contact"."id")
INNER JOIN "trapping_snaring_trapfeature" ON ("trapping_snaring_trapwork"."trap_id" = "trapping_snaring_trapfeature"."id")
INNER JOIN "lists_snaringtraptype" ON ("trapping_snaring_trapfeature"."trap_type_id" = "lists_snaringtraptype"."id")
LEFT OUTER JOIN "trapping_snaring_groupfeature" ON ("trapping_snaring_trapfeature"."group_id" = "trapping_snaring_groupfeature"."id")
LEFT OUTER JOIN "trapping_session" ON ("trapping_snaring_trapwork"."session_id" = "trapping_session"."id")
WHERE "trapping_snaring_trapwork"."team_id" = 11;
执行计划:
Merge Join (cost=2358.83..2473.45 rows=11334 width=998) (actual time=22.810..140.677 rows=11336 loops=1)
Merge Cond: (trapping_snaring_trapwork.checker_id = contacts_contact.id)
-> Nested Loop Left Join (cost=1.00..12439.20 rows=11334 width=820) (actual time=0.033..103.113 rows=11336 loops=1)
-> Nested Loop Left Join (cost=0.86..10436.08 rows=11334 width=513) (actual time=0.029..89.731 rows=11336 loops=1)
-> Nested Loop (cost=0.71..7170.02 rows=11334 width=401) (actual time=0.024..63.432 rows=11336 loops=1)
-> Nested Loop (cost=0.57..5033.83 rows=11334 width=231) (actual time=0.020..39.480 rows=11336 loops=1)
-> Index Scan using trapping_snaring_trapwork_checker_id_ae914a8a on trapping_snaring_trapwork (cost=0.29..796.63 rows=11334 width=74) (actual time=0.012..6.637 rows=11336 loops=1)
Filter: (team_id = 11)
Rows Removed by Filter: 1
-> Index Scan using trapping_snaring_trapfeature_pkey on trapping_snaring_trapfeature (cost=0.28..0.36 rows=1 width=157) (actual time=0.002..0.002 rows=1 loops=11336)
Index Cond: (id = trapping_snaring_trapwork.trap_id)
-> Index Scan using lists_snaringtraptype_pkey on lists_snaringtraptype (cost=0.15..0.18 rows=1 width=170) (actual time=0.001..0.001 rows=1 loops=11336)
Index Cond: (id = trapping_snaring_trapfeature.trap_type_id)
-> Index Scan using trapping_snaring_groupfeature_pkey on trapping_snaring_groupfeature (cost=0.14..0.28 rows=1 width=112) (actual time=0.001..0.001 rows=1 loops=11336)
Index Cond: (trapping_snaring_trapfeature.group_id = id)
-> Index Scan using trapping_session_pkey on trapping_session (cost=0.14..0.17 rows=1 width=307) (actual time=0.000..0.000 rows=0 loops=11336)
Index Cond: (trapping_snaring_trapwork.session_id = id)
-> Index Scan using contacts_contact_pkey on contacts_contact (cost=0.29..2257.76 rows=40098 width=178) (actual time=0.006..17.350 rows=50661 loops=1)
Planning time: 19.557 ms
Execution time: 143.044 ms