[英]MySQL, order by multiple columns from multiple tables
我有两个表:
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)
上的索引。
您需要先索引( artist_name
, artist_id
),然后再索引( album_artist_id
, album_name
)。 原因是因为您的artist_id
在artist_id
和album_artist_id
之间,所以它必须对索引执行相同的album_artist_id
,以产生用于排序的最终需求索引( artist_name
, album_name
)。
然后,您需要将订单更改为: ORDER BY artist_name, artist_id, album_name
。 这是因为可能存在两个artist_name
,这将导致其顺序不符合您的期望。 同样,它将阻止它使用索引。
仅使用artist_name
上的artist_name
,而album_name
不能提供足够的信息来进行这种排序,您所拥有的只是名称的有序列表,没有任何内容表明它们如何连接到另一个表。
在您order by
的列上添加索引。
如果您想知道为什么查询需要这么长时间,请查看EXPLAIN
。 例:
explain select * from your_table order by col1, col2
在排序依据的列上添加索引将是一个不错的选择。 是的,它将占用服务器中的更多空间,但是执行将是最佳状态。 阅读有关索引的更多信息: http : //dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html或http://www.tizag.com/mysqlTutorial/mysql-index.php
要注意的主要问题是,如果您没有完整的where子句,该子句只能通过按索引排序的列来解析,则需要扫描多少行才能解析。 如果仅检查50行以提供10行结果集,则状态良好,但如果为5000行,则可能需要重新考虑索引。
您可以做的第二件事是将sort_buffer_size增加大
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.