简体   繁体   English

MySQL查询执行非常慢

[英]MySQL query execution very slow

I have this query: 我有这个查询:

UPDATE invitations SET added=added+1 WHERE chat_id NOT IN (SELECT * FROM (SELECT LEFT(name,14) AS chat_id FROM rules WHERE name LIKE '-%-credit' AND value< 1522744396) AS iner)

every time I execute it it takes like ages and it never does what i want it to do. 每次执行它都需要花费很多时间,而且它从不执行我想要的操作。 for example I have run it now and it has taken 1600 seconds so far and nothing happened yet. 例如,我现在就运行它,到目前为止已经花费了1600秒,但什么都没有发生。 In show proccesslist I see that it's in the State of "Sending data". 在显示过程列表中,我看到它处于“正在发送数据”状态。 What's wrong with this query? 这个查询怎么了?

The table rules has at most 100K rows and the inner query returns like 2K rows. 该表规则最多具有10万行,内部查询返回的结果类似于2K行。 the results of the inner query are one column and are like: chat_id -1001167043635 -1001167043643 ... 内部查询的结果为一列,类似于:chat_id -1001167043635 -1001167043643 ...

when I run the inner query alone, it takes like 0.007 seconds. 当我单独运行内部查询时,大约需要0.007秒。

And the invitations table has 500K rows and are like: user_id | 邀请表有50万行,例如:user_id | chat_id | chat_id | added 55545323 | 添加55545323 | -1001167043635 | -1001167043635 | 23 23

it would be appreciated if you tell me what i have done wrong and tell me how to fix it. 如果您能告诉我我做错了什么并告诉我如何解决,将不胜感激。

Why do you run a "Select *"? 为什么要运行“选择*”? Does the inner Select not fullfill your request? 内部Select是否无法满足您的要求?

Have you set an index on chat_id? 您在chat_id上设置索引了吗?

You might think that the subquery in NOT IN executes only once for the entire query. 您可能会认为NOT IN中的子查询仅对整个查询执行一次。 The thing is that the for every row in OUTER QUERY, the INNER QUERY will be executed once. 事实是,对于“外部查询”中的每一行,“内部查询”将执行一次。 We faced this issue once. 我们曾经面对这个问题。 Check with mysql EXPLAIN query. 用mysql EXPLAIN查询检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM