mysql - 我怎样才能提高这个 MySQL 查询的性能?

标签 mysql indexing filesort

我有一个 MySQL 查询:

   SELECT DISTINCT 
          c.id, 
          c.company_name, 
          cd.firstname, 
          cd.surname, 
          cis.description AS industry_sector 
     FROM (clients c) 
     JOIN clients_details cd ON c.id = cd.client_id 
LEFT JOIN clients_industry_sectors cis ON cd.industry_sector_id = cis.id 
    WHERE c.record_type='virgin'
 ORDER BY date_action, company_name asc, id desc 
    LIMIT 30

clients 表有大约 60-70k 行,并且有一个“id”、“record_type”、“date_action”和“company_name”的索引——不幸的是,查询仍然需要 5 秒以上才能完成。删除“ORDER BY”将其减少到大约 30 毫秒,因为不需要文件排序。有什么方法可以更改此查询以改进 5 秒以上的响应时间吗?

最佳答案

参见:http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html

特别是:

In some cases, MySQL cannot use indexes to resolve the ORDER BY (..). These cases include the following:
(..)
You are joining many tables, and the columns in the ORDER BY are not all from the first nonconstant table that is used to retrieve rows. (This is the first table in the EXPLAIN output that does not have a const join type.)

关于mysql - 我怎样才能提高这个 MySQL 查询的性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3943630/

相关文章:

mysql - 使用索引,使用临时,使用文件排序 - 如何解决这个问题?

php - mysql 连接查询问题

php - 在没有时区偏移的情况下将 MYSQL 时间戳转换为 ISO-8601

php - 获得正确的 MAX 结果,但没有在 MySQL 中为留言板获得正确的 COUNT 值

mysql - Doctrine2 中从 n+1 到 n-1 的订购日期

python - 自动查找非 nan 值及其对应的索引点

MySQL 更新查询不使用索引列

c++ - MongoDb 使用 C++ 驱动程序创建稀疏索引

MySQL 5.1 在存在索引时使用文件排序事件

mysql - 为什么我不能在我已经为其建立索引的 MySQL 查询中摆脱文件排序