2
select m.ID,m.cell_number,cm.id 
from corporate_main_member m
   , corporate_main_corporate_membership cm 
WHERE m.status = 'active' 
   AND m.ID IN (
      select cm.FK_Member_ID 
      from corporate_main_corporate_membership cm 
      WHERE cm.status = 'active' 
        AND cm.ID IN (
           select gm.FK_Corporate_Membership_ID 
           from corporate_main_group_membership gm 
           WHERE gm.status = 'active' 
             AND gm.FK_Group_id IN (168,169,170)
             Group BY gm.FK_Corporate_Membership_ID
           )
      ) 
   AND cm.fk_corporation_id = 5 
   AND cm.FK_Member_ID = m.id

上:

1 Million records of corporate_main_member 
2 Million records of corporate_main_corporate_membership
3 Million records of corporate_main_group_membership

上面的查询停止了 mysql!它一直在工作,直到前天我使用脚本将数千条记录添加到corporate_main_member 和corporate_main_corporate_membership 表中。不确定它是否相关或数据库大小超过了导致严重性能问题的某个阈值。

有什么帮助吗?我对索引或任何东西一无所知。架构很明显。

4

1 回答 1

2

试试这个,

SELECT  m.ID,
        m.cell_number,
        cm.id
FROM    corporate_main_member m
        INNER JOIN corporate_main_corporate_membership cm
            ON  cm.FK_Member_ID = m.id  AND
                cm.fk_corporation_id = 5
        INNER JOIN
        (
            SELECT  ccm.FK_Member_ID
            FROM    corporate_main_corporate_membership ccm
                    INNER JOIN corporate_main_group_membership gm
                        ON ccm.ID = gm.FK_Corporate_Membership_ID
            WHERE   ccm.STATUS = 'active' AND
                    gm.STATUS = 'active' AND
                    gm.FK_Group_id IN ( 168, 169, 170 )
            GROUP   BY ccm.FK_Member_ID
        ) s ON  m.ID = s.FK_Member_ID
WHERE   m.STATUS = 'active'
于 2013-01-12T16:24:15.343 回答