【发布时间】:2012-11-19 17:05:02
【问题描述】:
我有以下查询非常慢(仅需要 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 ║ ╠═════════════╬════════════════════════╬════════╬═ ═════════════════════════╬═════════════════════╬══ ═══════╬══════════════════════════════════════════ ══╬═══════╬══════════════════════════════ ║简单║机会║范围║sales_stage,idx_deleted║sales_stage║78║null║25161║使用where;使用文件排序║ ║ 简单 ║ 用户 ║ 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 ║ 使用 where;使用索引║ ║ 简单 ║ 账户 ║ eq_ref ║ PRIMARY,idx_accnt_id_del ║ PRIMARY ║ 108 ║ version4.accounts_opportunities.account_id ║ 1 ║ 使用where ║ ║ 简单 ║ 机会_cstm ║ eq_ref ║ PRIMARY ║ PRIMARY ║ 108 ║ version4.opportunities.id ║ 1 ║ ║ ╚═════════════╩════════════════════════╩════════╩═ ═════════════════════════╩═════════════════════╩══ ═══════╩══════════════════════════════════════════ ══╩═══════╩═════════════════════════════╝【问题讨论】:
-
Using filesort是最糟糕的。 -
是的,如果你看到
Using filesort,你基本上就死定了,尤其是有大量受影响的行。您将不得不考虑在ORDER列上合并索引的方法。 -
你已经设置了哪些索引?
-
这是我在机会表上的索引。为了避免使用文件排序,可以做些什么不同的事情?我们有大量内存并且它没有被最大化,所以我知道它没有诉诸基于文件的表格等。这是我拍摄的索引屏幕截图的链接:d.pr/i/7oEz
-
Using filesort并不意味着它会进入磁盘;这意味着有必要对派生的结果集进行排序。当您像您一样在大胖连接上使用ORDER BY ... LIMIT时,通常会出现这种情况。
标签: mysql performance