我无法弄清楚如何优化此查询,请注意t_flowhistory表有大约16500行,并且以下查询只是不执行,但是当在较小的数据库上触发时,相同的查询工作正常。有什么办法可以优化这个查询吗?
SELECT
t_flowhistory.a_productid,
t_flowhistory.a_torole,
t_product.a_reference,
t_flowhistory.a_assigneddate
FROM
((select * from t_flowhistory WHERE a_flowhistoryid in
(SELECT max(a_flowhistoryid) FROM t_flowhistory
GROUP by a_productid)) as t_flowhistory)
INNER JOIN t_product ON t_product.a_productid = t_flowhistory.a_productid
WHERE
(t_flowhistory.a_status like 'Assigned' or t_flowhistory.a_status like 'rejected')
and t_flowhistory.a_isresolved = '1'
and t_product.a_active = 0
and t_product.a_ispublished=0
and t_flowhistory.a_torole = 2
ORDER BY t_flowhistory.a_assigneddate desc
t_flowhistory的表结构:
列类型为空默认
a_flowhistoryid(Primary)bigint(20)没有
a_productid bigint(20)是NULL
a_fromuserid int(10)是NULL
a_fromrole int(10)是NULL
a_torole int(10)是NULL
a_status enum('已分配','已移动','已完成','被拒绝')是已分配的
a_isresolved enum(' 0',' 1')是1
a_reasonid int(10)是NULL
a_remarks varchar(250)是NULL
a_assigneddate datetime是NULL
t_products的表结构
列类型为空默认
a_productid(Primary)int(11)No
a_reference varchar(42)是NULL
a_price decimal(20,6)是0.000000
a_defaultcategoryid int(10)是0
a_sequence int(10)是100000
a_wholesaleprice decimal(20,6)是0.000000
a_linkrewrite varchar(128)是NULL
a_metatitle varchar(128)是NULL
a_metakeywords varchar(255)是NULL
a_metadescription varchar(255)是NULL
a_ispublished tinyint(1)否0
a_active tinyint(1)是1
a_createddate datetime是NULL
a_createdby int(11)是NULL
a_modifieddate datetime是NULL
a_modifiedby int(11)是NULL
答案 0 :(得分:2)
尝试此查询可能是优化
SELECT
t_flowhistory.a_productid,
t_flowhistory.a_torole,
t_product.a_reference,
t_flowhistory.a_assigneddate
FROM t_flowhistory
join (SELECT max(a_flowhistoryid) as a_flowhistoryid FROM t_flowhistory
GROUP by a_productid) a on a.a_flowhistoryid=t_flowhistory.a_flowhistoryid
INNER JOIN t_product ON t_product.a_productid = t_flowhistory.a_productid
WHERE
(t_flowhistory.a_status like 'Assigned' or t_flowhistory.a_status like 'rejected')
and t_flowhistory.a_isresolved = '1'
and t_product.a_active = 0
and t_product.a_ispublished=0
and t_flowhistory.a_torole = 2
ORDER BY t_flowhistory.a_assigneddate desc