我有两张桌子:
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。专辑按艺术家名称 ASC、专辑名称 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 | |
+----+-------------+---------------+------+-----------------+-----------------+---------+------------------------------------+-------+---------------------------------+