Mysql 如何避免在union all查询上进行文件排序
请我有这个查询,如果组合使用filesort(UNIONALL),但如果单独执行,则无需进行文件排序即可。在这里,我不想单独执行它,而是想实现UNIONALL选项。请找个人帮忙Mysql 如何避免在union all查询上进行文件排序,mysql,Mysql,请我有这个查询,如果组合使用filesort(UNIONALL),但如果单独执行,则无需进行文件排序即可。在这里,我不想单独执行它,而是想实现UNIONALL选项。请找个人帮忙 (SELECT STRAIGHT_JOIN *, topic_post_time, topic_title, topic_id AS tid,
(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子句。只要用手术室 此外,ORDER BY并不是在这种上下文中完成的,这不是因为MySQL有缺陷,而是因为它没有逻辑意义 还有一个老的MySQL错误,它使所有联合使用临时表: 对查询生成的临时表进行排序意味着文件排序 “还有一个老的MySQL错误,它使所有联合使用临时表:对查询生成的临时表进行排序意味着文件排序” 我做了一个anwser,因为注释不允许代码格式化 C++源代码关闭(文件sql/sql\u union.cc)MySQL 5.5.32 union看起来总是创建一个临时表。。 请参阅代码注释和函数create_tmp_表
/*
Create a temporary table to store the result of select_union.
SYNOPSIS
select_union::create_result_table()
thd thread handle
column_types a list of items used to define columns of the
temporary table
is_union_distinct if set, the temporary table will eliminate
duplicates on insert
options create options
DESCRIPTION
Create a temporary table that is used to store the result of a UNION,
derived table, or a materialized cursor.
RETURN VALUE
0 The table has been created successfully.
1 create_tmp_table failed.
*/
bool
select_union::create_result_table(THD *thd_arg, List<Item> *column_types,
bool is_union_distinct, ulonglong options,
const char *alias)
{
DBUG_ASSERT(table == 0);
tmp_table_param.init();
tmp_table_param.field_count= column_types->elements;
if (! (table= create_tmp_table(thd_arg, &tmp_table_param, *column_types,
(ORDER*) 0, is_union_distinct, 1,
options, HA_POS_ERROR, alias)))
/*
创建一个临时表来存储select_union的结果。
提要
选择联合::创建结果表()
螺纹柄
column_类型用于定义列的项目列表
临时桌
如果设置为_union _distinct,则临时表将消除
插入时重复
选项创建选项
描述
创建用于存储联合结果的临时表,
派生表或物化游标。
返回值
0已成功创建表。
1创建tmp表格失败。
*/
布尔
选择联合::创建结果表(THD*THD参数,列表*列类型,
布尔是一个独特的、很长的选择,
常量字符*别名)
{
DBUG_断言(表==0);
tmp_table_param.init();
tmp_table_param.field_count=列类型->元素;
如果(!(表=创建tmp表(thd参数和tmp表参数,*列类型),
(顺序*)0,是不同的,1,
选项、位置错误、别名)
我是否正确,查询中唯一的区别是WHERE
条件?文件排序是否会减慢执行时间?因为文件排序通常不会随着“使用临时;使用文件排序”的记录数较少而减慢可能是不好的,因为它可能导致临时MyISAM文件需要使用快速排序算法进行排序,使用随机磁盘读取I/O和随机写入I/O公式(假设快速排序最佳情况和4毫秒磁盘随机访问(10K磁盘))((20日志20)*0.004)~0.1秒,但实际上,如果filesort算法从MySQL优化器获得的记录/行估计值错误,则filesort可能会很慢。直接连接可能是导致这种情况的原因。