MySql 查询超慢

标签 mysql performance

我有以下查询,该查询非常慢(仅需要 4-5 秒即可获得结果。我想知道是否有人可以看到我可以对此查询做不同的事情来加快速度?

谢谢!

SELECT
accounts.id AS account_id,
accounts. NAME AS account_name,
accounts.assigned_user_id account_id_owner,
users.user_name AS assigned_user_name,
opportunities_cstm.firstname_c, opportunities_cstm.lastname_c,
opportunities.`name`, TRIM(
    Concat(
        Ifnull(
            opportunities_cstm.firstname_c,
            ''
        ),
        ' ',
        Ifnull(
            opportunities_cstm.lastname_c,
            ''
        )
    )
) AS 'cfull' FROM
opportunities 
LEFT JOIN users ON opportunities.assigned_user_id = users.id 
LEFT JOIN accounts_opportunities ON opportunities.id = accounts_opportunities.opportunity_id 
LEFT JOIN accounts ON accounts_opportunities.account_id = accounts.id
LEFT JOIN opportunities_cstm ON opportunities.id = opportunities_cstm.id_c
WHERE
(
    (
        opportunities.sales_stage IN (
            'Prospecting',
            'Appointment Set',
            'MeetAndGreet',
            'Qualification',
            'Needs Analysis',
            'Locating Vehicle',
            'Demo',
            'Trade Evaluation',
            'Negotiation',
            'Manager T/O',
            'Write Up',
            'Credit App Submitted',
            'Pending Finance',
            'Loan Approval',
            'Deposit',
            'Delayed Decision',
            'Sold-Vehicle Ordered',
            'Sold-Pending Finance',
            'Sold/Pending Delivery',
            'Price Quoted',
            'Service Pending'
        )
    )
)
AND (
accounts_opportunities.deleted IS NULL
OR accounts_opportunities.deleted = 0
)
AND (
accounts.deleted IS NULL
OR accounts.deleted = 0
)
AND opportunities.deleted = 0
ORDER BY
opportunities.date_entered DESC,
opportunities.id DESC
LIMIT 0,21

以下是同一查询的解释:

╔═════════════╦════════════════════════╦════════╦══════════════════════════╦═════════════════════╦═════════╦════════════════════════════════════════════╦═══════╦═════════════════════════════╗
║ select_type ║         table          ║  type  ║      possible_keys       ║         key         ║ key_len ║                    ref                     ║ rows  ║            extra            ║
╠═════════════╬════════════════════════╬════════╬══════════════════════════╬═════════════════════╬═════════╬════════════════════════════════════════════╬═══════╬═════════════════════════════╣
║ simple      ║ opportunities          ║ range  ║ sales_stage, idx_deleted ║ sales_stage         ║      78 ║ null                                       ║ 25161 ║ Using where; Using filesort ║
║ simple      ║ users                  ║ eq_ref ║ PRIMARY, idx_id_deleted  ║ PRIMARY             ║     108 ║ version4.opportunities.assigned_user_id    ║     1 ║                             ║
║ simple      ║ accounts_opportunities ║ ref    ║ idx_oppid_del_accid      ║ idx_oppid_del_accid ║     111 ║ version4.opportunities.id                  ║     1 ║ Using where; Using index    ║
║ simple      ║ accounts               ║ eq_ref ║ PRIMARY,idx_accnt_id_del ║ PRIMARY             ║     108 ║ version4.accounts_opportunities.account_id ║     1 ║ Using where                 ║
║ simple      ║ opportunities_cstm     ║ eq_ref ║ PRIMARY                  ║ PRIMARY             ║     108 ║ version4.opportunities.id                  ║     1 ║                             ║
╚═════════════╩════════════════════════╩════════╩══════════════════════════╩═════════════════════╩═════════╩════════════════════════════════════════════╩═══════╩═════════════════════════════╝

最佳答案

我看到两个问题。

首先,您使用两个不同的 WHERE (... IS NULL OR ... = 0) 标准。这些速度慢得难以形容。这是因为索引对于查找 NULL 值没有用处。如果您可以消除这些 deleted 列中 NULL 的可能性,也许通过声明它们 NOT NULL DEFAULT 0 您可以将这些条件更改为 WHERE ... = 0 。这应该会加快很多事情的速度。这是因为索引对于查找 NULL 值没有用处。

其次,您要创建一个巨大的连接结果集,然后对其进行排序以查找最新的项目。

在加入之前,您可以尝试从“机会”表中预先选择项目。做这样的事情:

SELECT   whatever....
FROM (
         SELECT * 
           FROM opportunities
          WHERE opportunities.deleted = 0
            AND opportunities.sales_stage IN (
            'Prospecting',
            'Appointment Set',  etc etc  ...
            'Service Pending' )
       ORDER BY opportunities.date_entered DESC,
                opportunities.id DESC
          LIMIT 0,21
      ) opportunities 
LEFT JOIN users ON opportunities.assigned_user_id = users.id
...
ORDER BY
opportunities.date_entered DESC,
opportunities.id DESC
LIMIT 0,21

这很可能可以通过从连接的右侧删除一堆记录来减少 LEFT JOIN 操作的基数,从而加快速度。

关于MySql 查询超慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13458665/

相关文章:

python - 如何在迭代 pandas 数据框时提高性能?

c++ - 双重简单的 for 循环与一个复杂的循环

java - 写入 Lucene 索引,一次一个文档,随着时间的推移变慢

java - 相同的代码块在java中的运行时间是不同的。这是为什么?

java - 带有 jsp 和 mysql 的 ajax : java. sql.SQLException:用户 'root' @'localhost' 的访问被拒绝(使用密码:是)

mysql - SUBQUERY 和 INNER JOIN - MySQL 和 SQL 之间的区别

php - 如何将我的 ionic 应用程序连接到 MySQL 数据库?

php - 为什么 mysql Insert 查询 *仅* 在表为空时失败?

php - 尝试检查具有相同值的电子邮件是否已存在

iphone - 高效修改CGColor