Performace Lag in Mysql query

十年热恋 提交于 2019-12-11 14:11:33

问题


This mysql query takes 5 minutes 20 seconds to execute

SELECT PROVIDER, COUNT(DISTINCT(NAME)) FROM   Test  WHERE NAME NOT IN (SELECT NAME 
FROM Test   WHERE OPERATION = 'SIGN_IN' and Test.CREATED_TIME BETWEEN UNIX_TIMESTAMP(CURRENT_DATE()  - INTERVAL 1 DAY) *    1000  AND UNIX_TIMESTAMP(CURRENT_DATE()) * 1000) 
AND Test.CREATED_TIME BETWEEN UNIX_TIMESTAMP(CURRENT_DATE()  - INTERVAL 1 DAY) * 1000  AND UNIX_TIMESTAMP(CURRENT_DATE()) * 1000 AND OPERATION='VALIDATE'  GROUP BY PROVIDER;

The explain give the following result

explain SELECT  PROVIDER, COUNT(DISTINCT(NAME)) FROM   Test  WHERE NAME NOT IN  
(SELECT NAME  FROM Test   WHERE OPERATION = 'SIGN_IN' and Test.CREATED_TIME BETWEEN 
UNIX_TIMESTAMP(CURRENT_DATE()  - INTERVAL 1 DAY) * 1000  AND UNIX_TIMESTAMP(CURRENT_DATE()) * 1000) AND         Test.CREATED_TIME    BETWEEN UNIX_TIMESTAMP(CURRENT_DATE()  - INTERVAL 1 DAY) * 1000  AND UNIX_TIMESTAMP(CURRENT_DATE()) * 1000 AND OPERATION='VALIDATE'  GROUP BY PROVIDER;
+----+--------------------+-----------------+----------------+----------------------------------------------------------------------+-------------------------+---------+------+--------+------------------------------------+
| id | select_type        | table           | type           | possible_keys                                                        | key                     | key_len | ref  | rows   | Extra                              |
+----+--------------------+-----------------+----------------+----------------------------------------------------------------------+-------------------------+---------+------+--------+------------------------------------+
|  1 | PRIMARY            | Test | ALL            | Test_CTndx,Test_CORndx                         | NULL                    | NULL    | NULL | 137523 | Using where; Using filesort        |
|  2 | DEPENDENT SUBQUERY | Test | index_subquery | Test_NAMEndx,Test_CTndx,Test_CORndx | Test_NAMEndx | 303     | func |    148 | Using where; Full scan on NULL key |
+----+--------------------+-----------------+----------------+----------------------------------------------------------------------+-------------------------+---------+------+--------+------------------------------------+

Number of rows in a table is 50,000.

How can I optimize this ?


回答1:


Rewriting your query to replace the NOT IN with an equivalent NOT EXISTS statement should be faster.

For example, try something like this:

SELECT t1.PROVIDER, COUNT(DISTINCT(t1.NAME)) 
FROM   Test t1 
WHERE t1.CREATED_TIME BETWEEN UNIX_TIMESTAMP(CURRENT_DATE()  - INTERVAL 1 DAY) * 1000  AND UNIX_TIMESTAMP(CURRENT_DATE()) * 1000 
AND t1.OPERATION='VALIDATE'  
and NOT EXISTS (
  select null
  from test t2
  where t2.OPERATION = 'SIGN_IN' 
  and t2.CREATED_TIME BETWEEN UNIX_TIMESTAMP(CURRENT_DATE()  - INTERVAL 1 DAY) *    1000  AND UNIX_TIMESTAMP(CURRENT_DATE()) * 1000
  and t2.name = t1.name
)
GROUP BY t1.PROVIDER;


来源:https://stackoverflow.com/questions/12305066/performace-lag-in-mysql-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!