2017-06-15 50 views
1

新(ISH)到Postgres的,試圖提高查詢性能:性能平均/組由[Postgres的]

SELECT avg(calendar.price) 
FROM calendar 
INNER JOIN listings ON calendar.listing_id = listings.id 
WHERE listings.city = 'London' 
GROUP BY calendar.date; 

列表是〜150K行,歷爲約30億美元。該查詢需要20秒來執行。

我對listing.id,listings.city,calendar.listing_id和calendar.date有單獨的索引。

這可以優化嗎?

非常感謝!

+1

https://wiki.postgresql.org/wiki/Slow_Query_Questions –

+0

「我需要更快地做到這一點,所以我可以城市之間進行切換。」 - 你也可以按城市選擇和分組。 –

回答

0

在calendar.city上添加一個索引,並確保listing_id和calendar.id是相同的數據類型。如果不是,則在您的查詢中將它們轉換爲相同類型。

如果您執行查詢解釋並將查詢計劃程序的結果複製到https://explain.depesz.com/,則可以更好地瞭解瓶頸的可能位置。

0

複合B樹索引:

listings: (city, id) 
calendar: (listing_id, date, price)