gpt4 book ai didi

mysql - mysql针对嵌套查询的查询优化

转载 作者:行者123 更新时间:2023-11-29 07:34:39 25 4
gpt4 key购买 nike

我无法弄清楚如何优化此查询,请注意,t_flowhistory 表有约 16500 行,并且以下查询不会执行,但是在较小的数据库上触发时,相同的查询可以正常工作。有什么方法可以优化这个查询吗?

选择
t_flowhistory.a_productid,
t_flowhistory.a_torole,
t_product.a_reference,
t_flowhistory.a_assigneddate

((从 t_flowhistory WHERE a_flowhistoryid 中选择 *
(从 t_flowhistory 中选择 max(a_flowhistoryid)
按 a_productid)) 作为 t_flowhistory) 进行分组
内连接 t_product ON t_product.a_productid = t_flowhistory.a_productid
在哪里
(t_flowhistory.a_status 如“已分配”或 t_flowhistory.a_status 如“已拒绝”)
和 t_flowhistory.a_isresolved = '1'
且 t_product.a_active = 0
和 t_product.a_ispublished=0
和 t_flowhistory.a_torole = 2
按 t_flowhistory.a_assigneddate desc 排序

t_flowhistory 的表结构:

列类型默认为空
a_flowhistoryid(主要)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 日期时间 是 NULL

t_products 的表结构

列类型默认为空
a_productid(主) int(11) 否
a_reference varchar(42) 是 NULL
a_price 小数(20,6) 是 0.000000
a_defaultcategoryid int(10) 是 0
a_sequence int(10) 是 100000
a_wholesaleprice 小数(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 日期时间 是 NULL
a_createdby int(11) 是 NULL
a_modifieddate 日期时间 是 NULL
a_modifiedby int(11) 是 NULL

最佳答案

尝试这个查询可能会被优化

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

关于mysql - mysql针对嵌套查询的查询优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31259986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com