2017-08-24 79 views
0

我正在閱讀運行查詢的解釋輸出。這是結果:https://explain.depesz.com/s/5EfRPostgreSQL查詢優化,閱讀EXPLAIN

我看到#34行,數據庫正在做一個非常昂貴的索引掃描,導致刪除單個行。

我是否正確地讀這個?另外,想法可能會導致這種情況?

查詢:在此之前加入

explain analyze select *, posts.type as type, posts.created as created, posts.image_url as image_url, posts.id as post_id, posts.organization_id as id, 
    urls.image_url as url_image_url, 
    ts_headline('english',posts.text , to_tsquery('english', 'state') , $$StartSel='<span class="text-highlight">',StopSel=</span>, HighlightAll=true$$) as text, 
    ts_headline('english',posts.attachment_description, to_tsquery('english', 'state') , $$StartSel='<span class="text-highlight">',StopSel=</span>, HighlightAll=true$$) as attachment_description, 
    ts_headline('english',posts.attachment_title, to_tsquery('english', 'state') , $$StartSel='<span class="text-highlight">',StopSel=</span>, HighlightAll=true$$) as attachment_title 
    from vision2.posts15 as posts join vision2.organizations on organizations.id=posts.organization_id left join vision2.urls on urls.id = posts.url_id where chunks @@ to_tsquery('english', 'state') and string_to_array(upper(organizations.irs_state), '') && array['NJ'] and Date(posts.created) >= '2017-08-10' and Date(posts.created) <= '2017-08-24' and Date(posts.partition_date) >= '2017-08-10' and Date(posts.partition_date) <= '2017-08-24' order by posts.created desc offset 0 limit 40 
+0

注意,在各行很便宜(0.013ms),但有在該表870k循環。 您是否嘗試過在date_created和/或塊上創建索引? –

+0

請注意,在規劃的上幾層上,行大小相當大(〜1K)。 (並且哈希表可能會溢出到磁盤)。 (順便說一下:*請*編輯你的查詢到可讀的東西...) – wildplasser

+0

你可以仔細檢查你發佈的查詢是否符合執行計劃嗎?有些東西不匹配,即執行計劃中沒有partition_date篩選器,但它們位於where子句中,執行計劃中的解析器正在查找educ,而查詢顯示('english','state 「)。無論如何,我的直覺是查詢不與SELECT 4執行計劃綁定。執行計劃實際上看起來可能會有兩次在帖子表上流氓加入。 – HodgePodge

回答

0

試圖限制數據。您可以使用CTE,因爲它們已經實現了一次,並且如果您喜歡,可以像優化柵欄或臨時表一樣工作。

所以您的查詢看起來是這樣的:

WITH cte_posts AS (
    select type, created, image_url, id as post_id, organization_id as id, url_id, 
     ts_headline('english', 
        text, 
        to_tsquery('english', 'state'), 
        $$StartSel='<span class="text-highlight">',StopSel=</span>, HighlightAll=true$$) as text, 
     ts_headline('english', 
        attachment_description, 
        to_tsquery('english', 'state'), 
        $$StartSel='<span class="text-highlight">',StopSel=</span>, HighlightAll=true$$) as attachment_description, 
     ts_headline('english', 
        attachment_title, 
        to_tsquery('english', 'state'), 
        $$StartSel='<span class="text-highlight">',StopSel=</span>, HighlightAll=true$$) as attachment_title 
    from vision2.posts15 
    where Date(created) BETWEEN '2017-08-10' AND '2017-08-24' 
    and Date(partition_date) BETWEEN '2017-08-10' AND '2017-08-24' 
    AND chunks @@ to_tsquery('english', 'state') --is that column in posts15 table? 
) 
SELECT cte_posts.*, urls.image_url as url_image_url 
FROM cte_posts 
join vision2.organizations on organizations.id = cte_posts.id 
left join vision2.urls on urls.id = cte_posts.url_id 
     --you could try moving this WHERE to CTE as well, depending on your data 
where string_to_array(upper(organizations.irs_state), '') && array['NJ'] 
order by cte_posts.created desc 
offset 0 
limit 40