gpt4 book ai didi

mysql - 多次连接同一个表时优化Mysql查询

转载 作者:行者123 更新时间:2023-11-29 01:50:19 24 4
gpt4 key购买 nike

Can anybody help me optimize this mysql query?
The query is taking more than 1 min to fetch 1000 rows of data.
The sub query with FIND_IN_SET(id,GROUP_CONCAT(t.id))!=0 in the select statement is taking too much time to fetch data.

您还可以提供可用于优化我们必须多次连接同一个表的查询的提示。查询如下:

   SELECT mem.id,
CASE
WHEN t.due_date IS NOT NULL THEN
(SELECT description FROM descriptionTable WHERE CODE=
(SELECT task_type FROM memberTask WHERE FIND_IN_SET(id,GROUP_CONCAT(t.id))!=0 AND due_date=MIN(t.due_date) LIMIT 1))
ELSE
(SELECT description FROM descriptionTable WHERE CODE=
(SELECT task_type FROM memberTask WHERE FIND_IN_SET(id,GROUP_CONCAT(t1.id))!=0 AND due_date=MIN(t1.due_date) LIMIT 1))
END AS task_type
FROM member mem
INNER JOIN memberProgram p ON mem.id=p.member_id
LEFT JOIN memberTask t ON t.program=p.prog_name AND t.member_id=p.member_id AND t.status=1
LEFT JOIN memberTask t1 ON t1.member_id=p.member_id AND t1.status=1 AND t1.program IS NULL
GROUP BY mem.id

提前致谢!!!

最佳答案

您使用查询的方式会增加查询执行的复杂性,并且它必须经过大量交叉连接的行才能提取所需的行。更好的解决方案将首先从目标表中提取所需的行:memberTask,然后将其连接到主查询。

SELECT 
REPLACE(GROUP_CONCAT(COALESCE(CASE WHEN t1.program IS NOT NULL THEN t1.id ELSE NULL END,'')),',','') AS first_dueDate,
REPLACE(GROUP_CONCAT(COALESCE(CASE WHEN t1.program IS NOT NULL THEN 'first' ELSE 'second' END,'')),',','') AS selectFlag,
REPLACE(GROUP_CONCAT(COALESCE(CASE WHEN t1.program IS NULL THEN t1.id ELSE NULL END,'')),',','') AS first_dueDate,
aa.member_id FROM memberTask t1
INNER JOIN (SELECT due_date,program,aa.member_id,MAX(tsk.id) AS selId FROM memberTask tsk
INNER JOIN ( SELECT t.member_id,t.program,t.task_type,MIN(due_date) AS seldate FROM memberTask t WHERE tsk.status=1
GROUP BY t.member_id,IFNULL(t.program,''))aa
ON tsk.member_id=aa.member_id AND IFNULL(tsk.program,'')=IFNULL(aa.program,'') AND tsk.due_date=aa.seldate
GROUP BY due_date,program,aa.member_id) bb ON t1.id=bb.selId GROUP BY t1.member_id,t1.program

查询的分解如下:

SELECT t.member_id,t.program,t.task_type,MIN(due_date) AS seldate FROM memberTask t WHERE tsk.status=1 
GROUP BY t.member_id,IFNULL(t.program,'')

这是最内层的查询,它将根据 memberid 及其选择最小截止日期的程序提取记录。

SELECT due_date,program,aa.member_id,MAX(tsk.id) AS selId FROM memberTask tsk 
INNER JOIN (Inner Most Query)aa
ON tsk.member_id=aa.member_id AND IFNULL(tsk.program,'')=IFNULL(aa.program,'') AND tsk.due_date=aa.seldate
GROUP BY due_date,program,aa.member_id

此查询将提取具有相同截止日期的行中的最大 id

SELECT 
REPLACE(GROUP_CONCAT(COALESCE(CASE WHEN t1.program IS NOT NULL THEN t1.id ELSE NULL END,'')),',','') AS first_dueDate,
REPLACE(GROUP_CONCAT(COALESCE(CASE WHEN t1.program IS NOT NULL THEN 'first' ELSE 'second' END,'')),',','') AS selectFlag,
REPLACE(GROUP_CONCAT(COALESCE(CASE WHEN t1.program IS NULL THEN t1.id ELSE NULL END,'')),',','') AS first_dueDate,
aa.member_id FROM memberTask t1
INNER JOIN (second query) bb ON t1.id=bb.selId GROUP BY t1.member_id,t1.program

这会将整个多行数据拉到单行。

现在,在将所选数据加入主查询时,您应该使用以下情况。

SELECT * FROM member mem
INNER JOIN program pro om mem.id=pro.member_id
LEFT JOIN tmpTable tl ON t1.member_id=mem.member_id AND
CASE WHEN FIND_IN_SET(pro.`prog_name`, t1.prog_relate_task) THEN t1.program=pro.program AND t1.selectFlag='first'
ELSE t1.selectFlag='second' END

这些是对你的情况的一般理解。您可以根据需要更改拉取记录

告诉我它是否有效?

关于mysql - 多次连接同一个表时优化Mysql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46951405/

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