Jay*_*len 4 mysql sql-order-by
我试图从多个表中提取数据,当我使用 ORDER BY 日期时间字段时,它会在至少 10 秒后返回结果,但是如果我在没有 ORDER BY 的情况下执行相同的查询,则它会在 2 秒内返回结果。
这是我当前的查询
SELECT
ph.call_subject AS callSubject,
ac.account_name AS accountName,
DATE_FORMAT(ph.trigger_on, "%c/%e/%Y %h:%i %p") AS triggerOn,
ind.name AS industry,
cc.call_code_name AS callCode
FROM phone_calls AS ph
INNER JOIN accounts AS ac ON ph.account_id = ac.account_id
INNER JOIN industries AS ind ON ind.industry_id = ac.industry_id
INNER JOIN call_codes AS cc ON ph.call_code_id = cc.call_code_id
WHERE ac.status = 1 AND ph.status = 1 AND ph.owner_id = 1 AND ac.do_not_call = 0
AND ph.trigger_on BETWEEN '2012-11-19 00:00:00' AND '2013-03-19 23:59:59'
ORDER BY ph.trigger_on ASC LIMIT 0,1000
Run Code Online (Sandbox Code Playgroud)
以下字段都是 INT(11) UNSIGNED 类型
ph.account_id
ac.account_id
ind.industry_id
ac.industry_id
ph.call_code_id
cc.call_code_id
ph.owner_id
Run Code Online (Sandbox Code Playgroud)
以下字段均为 tinyint(1) 类型
ac.status
ph.status
ac.do_not_call
Run Code Online (Sandbox Code Playgroud)
此字段是日期时间类型
ph.trigger_on
Run Code Online (Sandbox Code Playgroud)
请注意,accounts 有 300K 条记录,phone_calls 有 500 万条记录。我该怎么做才能更快地执行此 ORDER BY?请注意,我的所有 where 子句字段、所有 ON 子句和 ph.trigger_on 都已编入索引。我使用的是 InnoDB 存储引擎而不是 MyIsam。
谢谢
Please try this:
Build an index on the columns (phone_calls.trigger_on, phone_calls.status, phone_calls.owner_id) Call it pcto
Change your FROM clause to:
FROM phone_calls AS ph FORCE INDEX (pcto)
Run Code Online (Sandbox Code Playgroud)
This is the ideal. If it does not work, then add a comment and I will give you another method that it guaranteed to work and give you the performance improvement that you need.
PLEASE NOTE: It doesn't matter (and indeed does no good) to have indexes built on "every" column in your query. MySQL can only use ONE index per table (or more correctly per table alias). You need to build the indexes that we are telling you to.
| 归档时间: |
|
| 查看次数: |
11017 次 |
| 最近记录: |