0

请我有这个查询,如果组合(联合所有)使用文件排序,但如果单独执行没有文件排序可以正常工作。在这里,我不想单独执行它,但想实现 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 
4

2 回答 2

0

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

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

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

于 2013-10-18T10:09:13.267 回答
0

@费德里科

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

我做了一个anwser,因为评论不允许代码格式化

C++ Source code off (file sql/sql_union.cc) MySQL 5.5.32 union 看起来总是创建一个临时表.. 见代码注释和函数 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)))
于 2013-10-18T10:37:44.000 回答