如何避免联合所有查询上的文件输出

时间:2013-10-18 09:18:32

标签: mysql

我有这个查询,如果合并使用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 

2 个答案:

答案 0 :(得分:0)

在我看来,唯一的区别是WHERE子句。只需使用OR。

另外,ORDER BY不是在这种情况下完成的,不是因为MySQL有问题,而是因为它没有逻辑意义。

还有一个旧的MySQL错误,它使所有UNION都使用临时表: http://bugs.mysql.com/bug.php?id=50674 并且排序由查询生成的临时表意味着文件输出

答案 1 :(得分:0)

@Federico

“还有一个旧的MySQL错误,它使所有UNION使用临时表:http://bugs.mysql.com/bug.php?id=50674并订购由查询生成的临时表文件”

我做了一个回复,因为注释最多允许代码格式化

C ++源代码关闭(文件sql / sql_union.cc)MySQL 5.5.32联合看起来总是创建一个临时表.. 请参阅代码注释和函数create_tmp_table

 /*
  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)))