簡體   English   中英

GROUP_CONCAT太慢

[英]GROUP_CONCAT too slow

您好,我在查詢中添加了GROUP_CONCAT函數,該函數殺死了我的查詢:/。 我的查詢是:

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;

該查詢有什么問題?

這是您的查詢(某種格式):

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;

是否這是從你的問題不清楚group_concat()是用添加group by 這可能會減慢速度。

limit 10是采用匹配的前10個a.idgroup by進行隱式排序)。 如果使用子查詢執行此操作,則可能會加快查詢速度:

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;

這假定所有聯接都起作用並且匹配記錄。 如果將聯接用於過濾,則返回的行數可能少於10。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM