mysql 多表 排序规则_MySQL,按多个表中的多个列排序

我有两张桌子:

Table of Artists (tbl_artist):

artist_id - primary key

artist_name - has index

Table of Albums (tbl_album):

album_id - primary key

album_artist_id - foreign key, has index

album_name - has index too

表在生产服务器上有很多记录(艺术家 – 60k,专辑 – 250k).

在索引页面上有一个专辑列表,分页步骤= 50.

相册按artist_name ASC,album_name ASC排序.所以简化的查询如下:

SELECT *

FROM tbl_artist, tbl_album

WHERE album_artist_id = artist_id

ORDER BY artist_name, album_name

LIMIT 0, 50

查询执行时间很长.可能是因为按不同表格的列排序.当我只留下1个排序时 – 查询立即执行.

在这种情况下可以做些什么?非常感谢.

编辑:

说明:

+----+-------------+---------------+--------+------------------+---------+---------+-----------------------------------+--------+---------------------------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+---------------+--------+------------------+---------+---------+-----------------------------------+--------+---------------------------------+

| 1 | SIMPLE | tbl_album | ALL | album_artist_id | NULL | NULL | NULL | 254613 | Using temporary; Using filesort |

| 1 | SIMPLE | tbl_artist | eq_ref | PRIMARY | PRIMARY | 4 | db.tbl_album.album_artist_id | 1 | |

+----+-------------+---------------+--------+------------------+---------+---------+-----------------------------------+--------+---------------------------------+

用STRAIGHT_JOIN解释

+----+-------------+---------------+------+-----------------+-----------------+---------+------------------------------------+-------+---------------------------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+---------------+------+-----------------+-----------------+---------+------------------------------------+-------+---------------------------------+

| 1 | SIMPLE | tbl_artist | ALL | PRIMARY | NULL | NULL | NULL | 57553 | Using temporary; Using filesort |

| 1 | SIMPLE | tbl_album | ref | album_artist_id | album_artist_id | 4 | db.tbl_artist.artist_id | 5 | |

+----+-------------+---------------+------+-----------------+-----------------+---------+------------------------------------+-------+---------------------------------+

最佳答案 尝试将(album_artist_id)上的索引更改为(album_artist_id,album_name)上的索引.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值