我必须使用 SQL 命令(文章管理)维护应用程序。
一个查询如下所示:
SET @from_timestamp = '2013-07-01 00:00:00';
SET @serialnumber = '%aaaa01%';
SELECT
test1.article,
test1.serialnumber,
test2.`timestamp` as test2_timestamp,
test2.additionalinfo,
test1.`timestamp` AS test1_timestamp,
test1.text0 AS test1_text0,
test1.text1 AS test1_text1,
test1.text2 AS test1_text2,
test1.text3 AS test1_text,
test3.text1 as test3_text1,
test3.`timestamp` AS test3_timestamp,
test3.`status`,
TIMESTAMPDIFF(MINUTE, test1.`timestamp`, test3.`timestamp`) as DeltaT
FROM (
SELECT
max(`timestamp`) AS TIMESTAMP,
article,
serialnumber,
text1,
text2,
text3,
text4
FROM exampleTable
WHERE
test = 'test1' AND
`timestamp` >= @from_timestamp AND
os0 LIKE @serialnumber
GROUP BY serialnumber
) AS test1
LEFT JOIN (
SELECT
max(`timestamp`) AS TIMESTAMP,
serialnumber,
status,
text1
FROM exampleTable
WHERE
test = 'test3' AND
`timestamp` >= @from_timestamp
GROUP BY serialnumber
) AS test3
ON test1.serialnumber = test3.serialnumber
LEFT JOIN (
SELECT
max(`timestamp`) AS TIMESTAMP,
serialnumber,
article,
text1
FROM exampleTable
WHERE
LENGTH(serialnumber) = 13 AND
test = 'test2' AND
`status` = -1 AND
`timestamp` >= @from_timestamp
GROUP BY serialnumber
) AS test2
ON test1.serialnumber = test2.serialnumber
ORDER BY test1.`timestamp`
它只使用一个保存产品数据的表。但正如预期的那样,这个查询真的很慢。有没有可能的优化?
Edit2:该表有很多数据(最后 COUNT(*) 提供 39589279 ;-) )
Edit3:解释表结果的附加图像
Edit4:请不要责怪我的表定义,它是由其他人开发的非常古老的,并且在不破坏更改的情况下无法更改它。