我有以下查询需要几分钟才能在mysql上运行:
SELECT * FROM
sys_quote_master AS g1
JOIN (SELECT order_id, order_base_id, max(order_date_last_revised) as mostrecent
FROM sys_quote_master group by order_base_id) AS g2
ON g2.mostrecent = g1.order_date_last_revised
ORDER BY g1.order_id;
查询运行几分钟,最终我收到此错误:
表' /tmp/#sql_4f0_1.MYI' ;;的密钥文件不正确尝试修复它
有人可以帮助我并告诉我出了什么问题吗?
答案 0 :(得分:0)
如果您使用EXPLAIN PLAN,我认为您将看到内部SELECT必须执行TABLE SCAN才能找到max(order_date_last_revised)。
我想知道如果使用GROUP BY和HAVING,子选择会更快吗?
http://www.java2s.com/Code/SQL/Select-Clause/GROUPandHAVINGwithsubquery.htm
请求EXPLAIN PLAN告诉你我是否正确。
答案 1 :(得分:0)
我没有对此进行测试,但尝试将查询更改为
SELECT *
FROM sys_quote_master g3
WHERE (g3.order_id, g3.order_base_id) IN (
SELECT g2.order_id, g2.order_base_id
FROM sys_quote_master g2
group by order_id, order_base_id
having max(order_date_last_revised) = (
select g1.order_date_last_revised
from sys_quote_master g1
where g2.order_id = g1.order_id
and g2.order_base_id = g1.order_base_id
)
);
假设您在(order_id,order_base_id)处有一个索引唯一键,也可能需要更改
having max(order_date_last_revised) = (
到
having max(order_date_last_revised) IN (
希望有所帮助