[英]SQL long query optimization
Trying to figure out, how to optimize runtime of vital SQL query.试图弄清楚,如何优化重要 SQL 查询的运行时。 Perhaps there is a expert here who could help me with this.也许这里有一位专家可以帮助我解决这个问题。
My PHP -> Sql query:我的 PHP -> Sql 查询:
SELECT ws_manager_repl_log.*, kst_cs_shopping.thread_id, kst_cs_shopping.stage, kst_cs_shopping.user_id
FROM ws_manager_repl_log
LEFT JOIN kst_cs_shopping ON ws_manager_repl_log.thread_id = kst_cs_shopping.thread_id
WHERE
kst_cs_shopping.stage != 'shoping'
AND ws_manager_repl_log.updated_at <= 1599982332
AND ws_manager_repl_log.mark_as_removed IS NULL
ORDER BY ws_manager_repl_log.updated_at DESC LIMIT 15 OFFSET 15
Here is a problem:这里有一个问题:
**Run Time: 4.206827**
Select Type Table Type Possible Keys Key Key Len Ref Rows Extra
SIMPLE ws_manager_repl_log ALL 415 Using where; Using temporary; Using filesort
SIMPLE kst_cs_shopping ALL 45831 Using where; Using join buffer (flat, BNL join)
Very long runtime... How to iptimize such query?运行时间很长...如何优化此类查询? It there any way?它有什么办法吗? Appreciate any help.感谢任何帮助。
Screen 1: https://i.imgur.com/wF0du28.png屏幕 1: https : //i.imgur.com/wF0du28.png
Screen 2: Explain: https://i.imgur.com/4lOhyso.png屏幕 2:解释: https : //i.imgur.com/4lOhyso.png
Screen3: Profiling: https://i.imgur.com/cU2SPgI.png屏幕 3:分析: https ://i.imgur.com/cU2SPgI.png
Screen4: Index def: https://i.imgur.com/UAfioBF.png Screen4:索引定义: https ://i.imgur.com/UAfioBF.png
First, the LEFT JOIN
is superfluous, because the WHERE
clause turns it into an INNER JOIN
.首先, LEFT JOIN
是多余的,因为WHERE
子句将它变成了INNER JOIN
。 And table aliases would make the query easier to write and to read:表别名将使查询更易于编写和阅读:
SELECT mrl.*, s.thread_id, s.stage, s.user_id
FROM ws_manager_repl_log mrl JOIN
kst_cs_shopping s
ON mrl.thread_id = s.thread_id
WHERE s.stage <> 'shoping' AND
mrl.updated_at <= 1599982332 AND
mrl.mark_as_removed IS NULL
ORDER BY mrl.updated_at DESC
LIMIT 15 OFFSET 15;
It is not clear what indexes would be best for this.目前尚不清楚哪些索引最适合于此。 One possibility is ws_manager_repl_log(mark_as_removed, updated_at, thread_id)
and kst_cs_shopping, thread_id, stage, user_id)
.一种可能性是ws_manager_repl_log(mark_as_removed, updated_at, thread_id)
和kst_cs_shopping, thread_id, stage, user_id)
。
That said, there are other possibilities, but the question doesn't provide enough information.也就是说,还有其他可能性,但问题没有提供足够的信息。 What filters (if any) are the most selective?哪些过滤器(如果有)最具选择性? What is the relationship between the tables, 1-1?表1-1之间是什么关系? 1-n? 1-n? n-1? n-1? How many rows are returned before the limit
?在limit
之前返回多少行?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.