請讓我有這個查詢使用filesort如果組合(聯合所有),但如果單獨執行工作正常沒有文件排序。在這裏,我不想單獨執行它,但想要實現union all選項。請有人幫忙。如何避免聯合所有查詢上的文件夾
(SELECT STRAIGHT_JOIN *,
topic_post_time,
topic_title,
topic_id AS tid,
p.userid AS profile_id,
Concat(first_name, ' ', last_name) AS poster_name,
Concat(first_name, '.', last_name) AS profile_name,
forum_id,
topic_last_post_time,
sch_name_abbrev,
picture_small_url,
profile_pix_upload_path,
profile_pix_upload_path,
LEFT(post_text, 100) AS post_text
FROM _forum_topics FORCE INDEX(topic_poster)
INNER JOIN _profile p
ON (p.userid = _forum_topics.topic_poster)
INNER JOIN _users
ON p.userid = _users.userid
INNER JOIN _class
ON _users.classid = _class.classid
INNER JOIN _unit
ON _class.unitid = _unit.unitid
INNER JOIN _department
ON _unit.deptid = _department.deptid
INNER JOIN _faculty
ON _department.facid = _faculty.facid
INNER JOIN _university
ON (_faculty.schid = _university.schid)
WHERE _forum_topics.sub_forum_id IN(133, 134, 135, 136,
137, 138)
ORDER BY topic_last_post_time DESC
LIMIT 0, 20)
UNION ALL
(SELECT STRAIGHT_JOIN *,
topic_post_time,
topic_title,
topic_id AS tid,
p.userid AS profile_id,
Concat(first_name, ' ', last_name) AS poster_name,
Concat(first_name, '.', last_name) AS profile_name,
forum_id,
topic_last_post_time,
sch_name_abbrev,
picture_small_url,
profile_pix_upload_path,
profile_pix_upload_path,
LEFT(post_text, 100) AS post_text
FROM _sch_forum_topics s FORCE INDEX(topic_poster)
INNER JOIN _profile p
ON (p.userid = s.topic_poster)
INNER JOIN _users
ON p.userid = _users.userid
INNER JOIN _class
ON _users.classid = _class.classid
INNER JOIN _unit
ON _class.unitid = _unit.unitid
INNER JOIN _department
ON _unit.deptid = _department.deptid
INNER JOIN _faculty
ON _department.facid = _faculty.facid
INNER JOIN _university
ON _faculty.schid = _university.schid
ORDER BY topic_last_post_time DESC
LIMIT 0, 20)
LIMIT
0, 20
我是正確,查詢中唯一的區別是WHERE條件? – scones
是文件放慢執行時間?因爲filesort通常不會因爲記錄數較低而減慢記錄「Using temporary; Using filesort」可能很糟糕,因爲它可能會導致臨時MyISAM文件需要使用隨機磁盤讀取I/O的快速排序算法和隨機數寫入I/O公式(假設快速排序最佳情況和4 ms磁盤隨機訪問(10K磁盤))((20 log 20)* 0.004)〜0.1秒 –
但是,如果文件排序算法得到錯誤的估計值,記錄/來自MySQL優化器的行.. STRAIGHT_JOIN可能是一個原因。 –