1
這第一個查詢有一個和clausle更多,從200毫秒減慢到9-13秒 我不明白爲什麼它如此。sql查詢慢3 leftjoins與其中clausel
如果我刪除所有where子句我得到〜200ms只是如果我再添加一個,它會很慢。
SELECT DISTINCT a.* , p.*, p2.*, p3.*
FROM article a
LEFT JOIN pro p ON a.id = p.article_id
LEFT JOIN pro p2 ON a.id = p2.article_id
LEFT JOIN pro p3 ON a.id = p3.article_id
WHERE a.is_active = true
AND p.name = 'hotel_stars'
AND p2.name = 'article_journey_days'
AND p3.name = 'article_persons'
AND p3.int_value > 0 AND p3.int_value < 7
AND p.int_value > 0 AND p.int_value < 5
結果
319 Datensätze
Laufzeit gesamt: 9,602.081 ms
SELECT DISTINCT a.* , p.*, p2.*, p3.*
FROM article a
LEFT JOIN property p ON a.id = p.article_id
LEFT JOIN property p2 ON a.id = p2.article_id
LEFT JOIN property p3 ON a.id = p3.article_id
WHERE a.is_active = true
AND p.name = 'hotel_stars'
AND p2.name = 'article_property_journey_days'
AND p3.name = 'article_property_persons'
AND p3.int_value > 0 AND p3.int_value < 7
// AND p.int_value > 0 AND p.int_value < 5 (removed)
結果
469 Datensätze
Laufzeit gesamt: 278.453 ms
問題出在哪裏? THX
編輯EXPLAIN PLAN:
HashAggregate (cost=24113.80..24113.81 rows=1 width=3528)
-> Nested Loop (cost=0.00..24113.69 rows=1 width=3528)
Join Filter: (a.id = p2.article_id)
-> Nested Loop (cost=0.00..16889.70 rows=1 width=2488)
-> Nested Loop (cost=0.00..16856.58 rows=4 width=2080)
Join Filter: (p.article_id = p3.article_id)
-> Seq Scan on property p (cost=0.00..8335.87 rows=115 width=1040)
Filter: ((int_value > 0) AND (int_value < 5) AND ((name)::text = 'hotel_stars'::text))
-> Materialize (cost=0.00..8336.41 rows=107 width=1040)
-> Seq Scan on property p3 (cost=0.00..8335.87 rows=107 width=1040)
Filter: ((int_value > 0) AND (int_value < 7) AND ((name)::text = 'article_property_persons'::text))
-> Index Scan using article_pkey on article a (cost=0.00..8.27 rows=1 width=408)
Index Cond: (id = p.article_id)
Filter: is_active
-> Seq Scan on property p2 (cost=0.00..7185.05 rows=3115 width=1040)
Filter: ((name)::text = 'article_property_journey_days'::text)
16 Datensätze
Laufzeit gesamt: 11.153 ms
改爲
SELECT DISTINCT a.* , p.*, p2.*, p3.*
FROM article a
INNER JOIN pro p ON a.id = p.article_id AND p.name = 'hotel_stars' AND p.int_value > 0 AND p.int_value < 5
INNER JOIN pro p2 ON a.id = p2.article_id AND p2.name = 'article_journey_days'
INNER JOIN pro p3 ON a.id = p3.article_id AND p3.name = 'article_persons' AND p3.int_value > 0 AND p3.int_value < 7
WHERE a.is_active = true
結果:
319 Datensätze
Laufzeit gesamt: 9,315.863 ms
HashAggregate (cost=24113.80..24113.81 rows=1 width=3528)
-> Nested Loop (cost=0.00..24113.69 rows=1 width=3528)
Join Filter: (a.id = p2.article_id)
-> Nested Loop (cost=0.00..16889.70 rows=1 width=2488)
-> Nested Loop (cost=0.00..16856.58 rows=4 width=2080)
Join Filter: (p.article_id = p3.article_id)
-> Seq Scan on property p (cost=0.00..8335.87 rows=115 width=1040)
Filter: ((int_value > 0) AND (int_value < 5) AND ((name)::text = 'hotel_stars'::text))
-> Materialize (cost=0.00..8336.41 rows=107 width=1040)
-> Seq Scan on property p3 (cost=0.00..8335.87 rows=107 width=1040)
Filter: ((int_value > 0) AND (int_value < 7) AND ((name)::text = 'article_property_persons'::text))
-> Index Scan using article_pkey on article a (cost=0.00..8.27 rows=1 width=408)
Index Cond: (id = p.article_id)
Filter: is_active
-> Seq Scan on property p2 (cost=0.00..7185.05 rows=3115 width=1040)
Filter: ((name)::text = 'article_property_journey_days'::text)
16 Datensätze
Laufzeit gesamt: 4.314 ms
類似的問題:(
解釋計劃 - 索引?數據量? – Randy
添加了解釋計劃。屬性表〜2000篇文章〜460 – Wykk
'哪裏'只涉及到連接表的條款應該被移動到左邊的連接'在'節 – foibs