MySQL查询在MySQL Workbench上超时

时间:2018-07-31 13:13:29

标签: mysql sql mysql-workbench innodb

InnoDB MySQL 5.7

大家好,如标题所示,我一直在使用的查询在 MySQL Workbench 上超时。我使用的查询与一周前相同,但不是花0.5秒,而是花15到25秒。以下是一个示例:

SELECT * FROM <table> ORDER BY <time column> DESC;

另一方面,我正在使用一些相当长的 MySQL 查询,以便从数据中收集一些粗略的统计信息,我希望这些查询花费更长的时间,但是我不确定是否存在更好的方法。我还应该提到,我目前仅具有SELECT/SHOW权限。这是超时的较长查询:

SELECT DATE(<time_sent_column>) as Update_Date, 
ABS(AVG(UNIX_TIMESTAMP(<time_created_column>) - UNIX_TIMESTAMP(<time_sent_column>))) AS AVG_Latency,
AVG((SELECT 
    ABS(AVG(UNIX_TIMESTAMP(<time_created_column>) - UNIX_TIMESTAMP(<time_sent_column>))) 
    FROM <table_name> 
    WHERE ((UNIX_TIMESTAMP(<time_created_column>) - UNIX_TIMESTAMP(<time_sent_column>) < 10800))))
AS Total_Average_Latency,
STDDEV(UNIX_TIMESTAMP(<time_created_column>) - UNIX_TIMESTAMP(<time_sent_column>)) as STDDEV_Latency,
AVG((SELECT 
    ABS(STDDEV(UNIX_TIMESTAMP(<time_created_column>) - UNIX_TIMESTAMP(<time_sent_column>))) 
    FROM <table_name> 
    WHERE ((UNIX_TIMESTAMP(<time_created_column>) - UNIX_TIMESTAMP(<time_sent_column>) < 10800))))
AS Total_STDDEV_Latency,
VAR_SAMP(UNIX_TIMESTAMP(<time_created_column>) - UNIX_TIMESTAMP(create_date)) as Variance_Latency,
AVG((SELECT 
    ABS(VAR_SAMP(UNIX_TIMESTAMP(<time_created_column>) - UNIX_TIMESTAMP(<time_sent_column>))) 
    FROM <table_name>
    WHERE ((UNIX_TIMESTAMP(<time_created_column>) - UNIX_TIMESTAMP(<time_sent_column>) < 10800))))
AS Average_Variance_Latency,
MIN(ABS((UNIX_TIMESTAMP(<time_created_column>) - UNIX_TIMESTAMP(<time_sent_column>)))) AS Min_Latency,
MAX(ABS((UNIX_TIMESTAMP(<time_created_column>) - UNIX_TIMESTAMP(<time_sent_column>)))) AS Max_Latency
FROM <table_name>
WHERE ((ABS(UNIX_TIMESTAMP(<time_created_column>) - UNIX_TIMESTAMP(<time_sent_column>)) < 10800) AND created_at > '2018-05-01')
GROUP BY DATE(<time_created_column>)
ORDER BY DATE(<time_created_column>) DESC;

我可以将所有这些分解,但是如果可能的话,我希望在查询后将它们放在一个表中。.我对 MySQL 还是很陌生,所以我不确定我可以使用JOINs或仅具有SELECT/SHOW权限的任何此类内容。

数据库处于其连接的50%,最小的流量,98.7%的密钥效率,87.5%的InnoDB缓冲区使用率,每秒0次读/写。谢谢!

1 个答案:

答案 0 :(得分:0)

(在下面,我假设time_created小于time_sent;如果不是给定的,请调整代码。)

我将从

开始
CREATE TEMPORARY TABLE t
    SELECT DATE(time_sent)    AS Update_Date,
           DATE(time_created) AS Created_Date,
           UNIX_TIMESTAMP(time_sent) - UNIX_TIMESTAMP(time_created)) AS Latency
        FROM table_name
        WHERE UNIX_TIMESTAMP(time_sent) - UNIX_TIMESTAMP(time_created)) < 10800
          AND created_at > '2018-05-01';

,然后从该临时表中构建查询。下一个查询将需要更少的 lot 击键。而且它不需要任何子查询。

(我假设create_date是一个错字?)

(也许您想在>=中使用created_at > '2018-05-01';?)

请注意,< 10800的测试可能在WHERE以外的所有表达式中都是多余的。