ITPub博客

首页 > 数据库 > MySQL > 禁用内嵌视图的排序

禁用内嵌视图的排序

原创 MySQL 作者:壹頁書 时间:2014-04-25 17:36:07 0 删除 编辑
MySQL会对group by字段进行排序,但是一些内嵌视图中的group by本身就是中间过程,没有必要进行排序。
这种场景可以使用order by null禁用内嵌视图的排序。

如下SQL
  1. SELECT DISTINCT a.*, c.downNum
  2. FROM sod_artist a INNER JOIN sod_artist_category_relation b ON a.ArtistID = b.ArtistID LEFT JOIN (SELECT d.ArtistID, COUNT(a.DownNum) AS downNum
  3.     FROM (SELECT downnum, songid
  4.         FROM sod_song_ranking1
  5.         WHERE SongDate = 20140422
  6.         ) a INNER JOIN sod_song_artist_relation d ON a.SongID = d.SongID
  7.     GROUP BY d.ArtistID
  8.     ) c ON a.ArtistID = c.ArtistID
  9. WHERE a.State = 1
  10.     AND b.CategoryID = 1363
  11.     OR b.CategoryID = 1628
  12. ORDER BY c.downNum DESC
  13. LIMIT 0, 45
执行计划:

  1. +----+-------------+-------------------+--------+-----------------+-------------+-------------------+-------+-----------------------------------------------------------+
  2. | id | select_type | table             | type   | possible_keys   | key         | ref               | rows  | Extra                                                     |
  3. +----+-------------+-------------------+--------+-----------------+-------------+-------------------+-------+-----------------------------------------------------------+
  4. | 1  | PRIMARY     | b                 | range  | PRIMARY,Index_1 | PRIMARY     | NULL              | 286   | Using where; Using index; Using temporary; Using filesort |
  5. | 1  | PRIMARY     | a                 | eq_ref | PRIMARY         | PRIMARY     | songod.b.ArtistID | 1     | Using where                                               |
  6. | 1  | PRIMARY     |        | ref    |     | | songod.b.ArtistID | 10    | NULL                                                      |
  7. | 2  | DERIVED     |        | ALL    | NULL            | NULL        | NULL              | 55316 | Using temporary; Using filesort                           |
  8. | 2  | DERIVED     | d                 | ref    | PRIMARY,inx_1   | inx_1       | a.songid          | 1     | Using index                                               |
  9. | 3  | DERIVED     | sod_song_ranking1 | ref    | songdate        | songdate    | const             | 55316 | NULL                                                      |
  10. +----+-------------+-------------------+--------+-----------------+-------------+-------------------+-------+-----------------------------------------------------------+
使用Order By Null禁用排序

  1. SELECT DISTINCT a.*, c.downNum
  2. FROM sod_artist a INNER JOIN sod_artist_category_relation b ON a.ArtistID = b.ArtistID LEFT JOIN (SELECT d.ArtistID, COUNT(a.DownNum) AS downNum
  3.     FROM (SELECT downnum, songid
  4.         FROM sod_song_ranking1
  5.         WHERE SongDate = 20140422
  6.         ) a INNER JOIN sod_song_artist_relation d ON a.SongID = d.SongID
  7.     GROUP BY d.ArtistID order by null
  8.     ) c ON a.ArtistID = c.ArtistID
  9. WHERE a.State = 1
  10.     AND b.CategoryID = 1363
  11.     OR b.CategoryID = 1628
  12. ORDER BY c.downNum DESC
  13. LIMIT 0, 45
执行计划:
可以看到执行计划第四行,已经消除了排序。

    1. +----+-------------+-------------------+--------+-----------------+-------------+-------------------+-------+-----------------------------------------------------------+
    2. | id | select_type | table             | type   | possible_keys   | key         | ref               | rows  | Extra                                                     |
    3. +----+-------------+-------------------+--------+-----------------+-------------+-------------------+-------+-----------------------------------------------------------+
    4. | 1  | PRIMARY     | b                 | range  | PRIMARY,Index_1 | PRIMARY     | NULL              | 286   | Using where; Using index; Using temporary; Using filesort |
    5. | 1  | PRIMARY     | a                 | eq_ref | PRIMARY         | PRIMARY     | songod.b.ArtistID | 1     | Using where                                               |
    6. | 1  | PRIMARY     |        | ref    |     |  | songod.b.ArtistID | 10    | NULL                                                      |
    7. | 2  | DERIVED     |        | ALL    | NULL            | NULL        | NULL              | 55316 | Using temporary                                           |
    8. | 2  | DERIVED     | d                 | ref    | PRIMARY,inx_1   | inx_1       | a.songid          | 1     | Using index                                               |
    9. | 3  | DERIVED     | sod_song_ranking1 | ref    | songdate        | songdate    | const             | 55316 | NULL                                                      |
    10. +----+-------------+-------------------+--------+-----------------+-------------+-------------------+-------+-----------------------------------------------------------+


参考:
http://hudeyong926.iteye.com/blog/785181

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-1149590/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: Redis
下一篇: SQL乘法表
请登录后发表评论 登录
全部评论

注册时间:2013-10-19

  • 博文量
    620
  • 访问量
    6339945