我有一个相当简单的查询,需要花费很多时间。当我做一个解释分析它显示了一个巨大的类型,我不知道它来自哪里。这种情况占88%的时间,但我不明白它为什么存在以及如何删除它。
查询是:
explain analyze SELECT DISTINCT "holidays".*
FROM "holidays"
join periods on
holidays.id = periods.holiday_id
WHERE
(periods.duration_nights >= 5 AND
periods.duration_nights <= 32 AND
periods.price >= 100 AND
periods.price <= 3800 )
LIMIT 12 OFFSET 0;
解释结果如下:
"Limit (cost=825252.03..825253.35 rows=12 width=6699) (actual time=3264.827..3268.273 rows=12 loops=1)"
" -> Unique (cost=825252.03..832958.41 rows=70058 width=6699) (actual time=3264.824..3268.268 rows=12 loops=1)"
" -> Sort (cost=825252.03..825427.17 rows=70058 width=6699) (actual time=3264.822..3265.161 rows=441 loops=1)"
" Sort Key: holidays.id, holidays.product_id, holidays.name, holidays.price, holidays.url, holidays.description, holidays.category, holidays.country, holidays.region, holidays.city, holidays.latitude, holidays.longitude, holidays.service_type, holidays.accommodation_type, holidays.accommodation_code, holidays.accommodation_name, holidays.stars, holidays.rating, holidays.group_holiday, holidays.transport_type, holidays.airline, holidays.departure_airport, holidays.price_unit, holidays.booking_url, holidays.target_group, holidays.tour_operator, holidays.tour_operator_category, holidays.has_pool, holidays.distance_beach, holidays.distance_supermarket, holidays.distance_village, holidays.distance_airport, holidays.created_at, holidays.updated_at, holidays.distance_restaurant, holidays.image, holidays.has_wifi, holidays.has_kids_pool, holidays.has_day_care, holidays.has_supermarket, holidays.has_bar, holidays.has_restaurant, holidays.has_parking"
" Sort Method: external merge Disk: 70784kB"
" -> Hash Join (cost=3027.75..14081.67 rows=70058 width=6699) (actual time=13.708..315.283 rows=70734 loops=1)"
" Hash Cond: (periods.holiday_id = holidays.id)"
" -> Seq Scan on periods (cost=0.00..6996.62 rows=70058 width=4) (actual time=0.011..108.429 rows=70734 loops=1)"
" Filter: ((duration_nights >= 5) AND (duration_nights <= 32) AND (price >= 100::numeric) AND (price <= 3800::numeric))"
" Rows Removed by Filter: 51597"
" -> Hash (cost=443.00..443.00 rows=3100 width=6699) (actual time=7.605..7.605 rows=3100 loops=1)"
" Buckets: 1024 Batches: 32 Memory Usage: 116kB"
" -> Seq Scan on holidays (cost=0.00..443.00 rows=3100 width=6699) (actual time=0.002..1.013 rows=3100 loops=1)"
"Total runtime: 4497.170 ms"
答案 0 :(得分:0)
由于运算符不同,排序就在那里。
要计算集合中的不同元素,您需要检查重复。
检查集合中的重复项的快速方法是按所选列的所有列对行进行排序,并为每一行逐列比较上一行的同一列。如果列不匹配,则不是重复。如果所有列都相同,则它是前一行的副本。迭代,冲洗,重复。
它缓慢的原因是因为它在磁盘上排序。增加work_mem,交换应该消失。
http://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server