简体   繁体   English

同一表上的2 LEFT OUTER JOIN会冻结服务器

[英]2 LEFT OUTER JOIN on the same table freezes server

I am trying to run a query on table 'apst_mailings' storing the content of each newsletter we send to our subscribers. 我正在尝试在表'apst_mailings'上运行查询,该表存储我们发送给订户的每个新闻通讯的内容。 Each time we attempt to send the email to an individual we insert a row in apst_mailings_accuses reporting the time and state of the sending, and the ID of the newletter. 每次我们尝试向个人发送电子邮件时,我们都会在apst_mailings_accuses中插入一行,以报告发送时间和状态以及通讯的ID。 I wanna list the newsletters and count the total sent and successfully sent for each. 我想列出新闻通讯,并计算每个通讯已发送和成功发送的总数。

    SELECT m.id AS code_mailing, 
    COUNT(a.adh_code) AS num, COUNT(b.adh_code) AS succes
    FROM apst_mailings AS m
        LEFT OUTER JOIN apst_mailings_accuses AS a
            ON a.id_mailing = m.id
        LEFT OUTER JOIN apst_mailings_accuses AS b
            ON b.id_mailing = m.id
            AND b.etat = 'succes'
    GROUP BY m.id

And it simply hangs the server forever. 它只是将服务器永久挂起。 I have tried each join in separated queries and it worked without problem: 我曾尝试在单独的查询中的每个联接,它都可以正常工作:

// Counts the email sent per mailing
    SELECT m.id AS code_mailing, 
    COUNT(a.adh_code) AS num
    FROM apst_mailings AS m
        LEFT OUTER JOIN apst_mailings_accuses AS a
            ON a.id_mailing = m.id
    GROUP BY m.id

    SELECT m.id AS code_mailing, 
    COUNT(b.adh_code) AS succes
    FROM apst_mailings AS m
        LEFT OUTER JOIN apst_mailings_accuses AS b
            ON b.id_mailing = m.id
            AND b.etat = 'succes'
    GROUP BY m.id

I could split my query but the reason why it doesn't work really bothers me. 我可以拆分查询,但无法正常工作的原因令我感到困扰。 Can anyone explain please? 谁能解释一下?

Thanks! 谢谢!

You can what you want in a much simpler way by using a single join and using SUM to perform a conditional count. 通过使用单个联接并使用SUM执行条件计数,可以以更简单的方式获得所需的内容。

SELECT
   m.id AS code_mailing, 
   COUNT(a.adh_code) AS num,
   SUM(a.etat = 'succes') AS succes
FROM apst_mailings AS m
LEFT OUTER JOIN apst_mailings_accuses AS a
ON a.id_mailing = m.id
GROUP BY m.id

But the reason why your query doesn't work is because you're joining ALL the rows in subquery a with ALL the matching rows in subquery b in a huge cross join. 但是,为什么您的查询不工作的原因是因为你在子查询所有匹配的行加盟子查询所有b在一个巨大的交叉连接。 This could generate a huge temporary result set, which is presumably why the query takes forever to terminate. 这可能会生成一个巨大的临时结果集,这大概就是为什么查询需要永远终止的原因。 And even if it did terminate, your counts would be completely off - they would be the product of the two counts. 即使终止,您的计数也将完全消失-它们将是两个计数的乘积。

To solve it do the GROUP BY first. 要解决此问题,请先执行GROUP BY Then JOIN the result of that to your main table. 然后将其结果JOIN主表。

SELECT
   m.id AS code_mailing, 
   IFNULL(a.num, 0) AS num,
   IFNULL(b.succes, 0) AS succes
FROM apst_mailings AS m
LEFT OUTER JOIN (
   SELECT id_mailing, COUNT(adh_code) AS num
   FROM apst_mailings_accuses 
   GROUP BY id_mailing
) a
ON a.id_mailing = m.id
LEFT OUTER JOIN (
    SELECT id_mailing, COUNT(adh_code) AS succes
    FROM apst_mailings_accuses
    WHERE etat = 'succes'
    GROUP BY id_mailing
) b
ON b.id_mailing = m.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM