[英]GROUP_CONCAT too slow
Hello I added GROUP_CONCAT function to my query and that function killed my query :/. 您好,我在查询中添加了GROUP_CONCAT函数,该函数杀死了我的查询:/。 My query is :
我的查询是:
SELECT u.username,a.user_id,a.id,a.text,a.lang as fromLang,b.lang as toLang,GROUP_CONCAT(DISTINCT b.id) AS translation_ids FROM sentence as a
INNER JOIN sentence_relationship as sr ON
(sr.sentence_id = a.id)
INNER JOIN sentence as b ON
(b.id = sr.translation_id AND a.id = sr.sentence_id)
INNER JOIN users as u ON
(u.id = a.user_id) GROUP BY a.id LIMIT 10;
What is wrong with that query ? 该查询有什么问题?
This is your query (somewhat formatted): 这是您的查询(某种格式):
SELECT u.username, a.user_id, a.id,a.text,a.lang as fromLang, b.lang as toLang,
GROUP_CONCAT(DISTINCT b.id) AS translation_ids
FROM sentence a INNER JOIN
sentence_relationship sr
ON sr.sentence_id = a.id INNER JOIN
sentence b
ON b.id = sr.translation_id AND a.id = sr.sentence_id INNER JOIN
users as u
ON u.id = a.user_id
GROUP BY a.id
LIMIT 10;
It is unclear from your question whether the group_concat()
was added with the group by
. 是否这是从你的问题不清楚
group_concat()
是用添加group by
。 That could slow things down. 这可能会减慢速度。
The limit 10
is taking the first 10 a.id
s that match (the group by
does an implicit ordering). limit 10
是采用匹配的前10个a.id
( group by
进行隐式排序)。 If you do this with a subquery, it will probably speed up the query: 如果使用子查询执行此操作,则可能会加快查询速度:
SELECT u.username, a.user_id, a.id,a.text,a.lang as fromLang, b.lang as toLang,
GROUP_CONCAT(DISTINCT b.id) AS translation_ids
FROM (select s.*
from sentence s
order by a.id
limit 10
) a INNER JOIN
sentence_relationship sr
ON sr.sentence_id = a.id INNER JOIN
sentence b
ON b.id = sr.translation_id AND a.id = sr.sentence_id INNER JOIN
users as u
ON u.id = a.user_id
GROUP BY a.id;
This assumes that all the joins do work and match records. 这假定所有联接都起作用并且匹配记录。 If the joins are used for filtering, then you may get fewer than 10 rows back.
如果将联接用于过滤,则返回的行数可能少于10。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.