减少运行时间(选择查询执行时间)
甚至查询都不起作用
正在减少代码行
有80张桌子
select
(ant_return_loss_pass='pass')+(ant_cross_isolation_pass='pass') as pass_count,
(ant_return_loss_pass='fail')+(ant_cross_isolation_pass='fail') as fail_count,
(ant_return_loss_pass='') +(ant_cross_isolation_pass='') as blank_count
from
(
select A.serial_no,
A.pass_fail as ant_return_loss_pass
from ant_return_loss A,
(
select max(register_date) as date
from ant_return_loss
where 1=1
and serial_no >= '184500074'
and serial_no <= '184500076'
group by serial_no
) B
where 1 = 1
and A.register_date = B.date
)AA
,(
select A.serial_no,
A.pass_fail as ant_cross_isolation_pass
from ant_cross_isolation A,
(
select max(register_date) as date
from ant_cross_isolation
where 1=1
and serial_no >= '184500074'
and serial_no <= '184500076'
group by serial_no
) B
where 1 = 1
and A.register_date = B.date
)BB
where 1=1
and AA.serial_no = BB.serial_no
不工作或工作600秒
答案 0 :(得分:1)
以下查询使用显式联接语法和大小写表达式,使您的查询更易于理解:
ubuntu@ip-172-31-15-175:~$ vault status
Key Value
--- -----
Seal Type shamir
Initialized true
Sealed true
Total Shares 5
Threshold 3
Unseal Progress 0/3
Unseal Nonce n/a
Version 1.0.0
HA Enabled true
ubuntu@ip-172-31-15-175:~$
要检查它是否可以提高性能,但是需要访问您的数据库。例如,您为SELECT
COUNT( CASE WHEN ant_return_loss_pass = 'pass' AND
ant_cross_isolation_pass = 'pass' THEN 1 END ) AS pass_count
, COUNT( CASE WHEN ant_return_loss_pass = 'fail' AND
ant_cross_isolation_pass = 'fail' THEN 1 END ) AS fail_count
, COUNT( CASE WHEN ant_return_loss_pass = '' AND
ant_cross_isolation_pass = '' THEN 1 END ) AS blank_count
FROM (
SELECT
A.serial_no
, A.pass_fail AS ant_return_loss_pass
FROM ant_return_loss A
INNER JOIN (
SELECT MAX( register_date ) AS date
FROM ant_return_loss
WHERE serial_no >= '184500074'
AND serial_no <= '184500076'
GROUP BY serial_no
) B ON A.register_date = B.date
) AA
INNER JOIN (
SELECT
A.serial_no
, A.pass_fail AS ant_cross_isolation_pass
FROM ant_cross_isolation A
INNER JOIN (
SELECT MAX( register_date ) AS date
FROM ant_cross_isolation
WHERE serial_no >= '184500074'
AND serial_no <= '184500076'
GROUP BY serial_no
) B ON A.register_date = B.date
) BB ON AA.serial_no = BB.serial_no
还是ant_return_loss.serial_no
编制索引将有助于子查询的where子句。
您是否对查询运行任何解释计划?参见:https://dev.mysql.com/doc/refman/5.7/en/explain.html
请注意,为避免运行时间过长,您可以在整个查询的各个部分上使用解释,寻找可能有帮助的索引:例如
ant_cross_isolation.serial_no
然后:
explain
SELECT MAX( register_date ) AS date
FROM ant_return_loss
WHERE serial_no >= '184500074'
AND serial_no <= '184500076'
GROUP BY serial_no
直到您检查完所有部分以提高性能为止。