我有两张桌子:
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)上的索引.