2013-01-12 166 views
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表添加了幾千條記錄。不確定它是相關的還是數據庫大小超過某個導致嚴重性能問題的閾值。

任何幫助?我不知道索引或任何東西。架構是顯而易見的。

+0

您是否嘗試過使用左加入? –

+0

你能描述查詢應該做什麼嗎? –

回答

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'