我有兩個表,posts和posts_relationship。避免查詢中的文件夾
posts:id, title, text, lang, timestamp
posts_relationship: id, post_id, subcategory_id
這是我的查詢:
SELECT posts.title,
posts.timestamp
FROM posts_relationship
INNER JOIN posts ON posts.id = posts_relationship.post_id
WHERE posts.lang = 'it' AND
posts.timestamp <= NOW() AND
posts_relationship.subcategory_id = 21
ORDER BY posts.timestamp DESC
我已經posts.lang
,posts.timestamp
,posts_relationships.post_id
和posts_relationship.subcategory_id
添加索引。
但與解釋我總是臨時或Filesort。
我怎樣才能只有「使用哪裏」?
一個'posts.id'將有所幫助。取決於大小,mysql可能會選擇不使用索引來替換較小的結果集 – exussum
用靜態日期替換您的<= NOW()'(從應用程序派生,作爲示例),並通過'(timestamp,lang)'創建索引表'職位'。請注意,MySQL永遠不能使用多個索引,因此,'(timestamp,lang)'索引將適合'WHERE'和'ORDER BY'子句,但不會使用'subcategory_id'索引。所以它不是100%保證你會避免文件。文件也可能意味着它實際上是在_memory_中排序(這要快得多) –
文件排序是針對'order by'而不是'where'。 –