2013-10-24 86 views
2

我有一個sql查詢來從4個表中獲取值。在我的查詢需要很多時間。我需要簡化查詢 我需要的是我必須只顯示50條記錄。在我的桌子上,我有90,000條記錄。所以我deciede申請批處理像 首先從第一個表中選擇50個記錄,然後檢查與其他3個表。 如果50是滿意,我將顯示,否則我還得繼續下一個50查詢性能

但是我沒有想法實現

select file_name, 
     A.id, 
     A.reference, 
     user.username, 
     c.update_date 
    from A_Table A, 
     (select reference 
      from B_Table 
      where code = 'xxx' 
      group by reference 
      having count(*) > 1) B, 
     C_Table c, 
     D_Table d 
    where A.reference = B.reference 
    and A.id = c.id 
    and A.code = 'ICG' 
    and c.updated_by = d.user_id 
order by 3 
    limit 20; 
+2

告訴我們解釋計劃,並在桌子上 –

+1

是01和02拼錯,應respecively解讀爲A,B的指標? – alko

+0

一個atble它有90,000條記錄。所以需要很長時間才能從表A的前50行中選擇並與其他 – jackyesind

回答

2

查詢看起來不錯。

添加一些索引將有很大幫助。

假設id列(A_Table.idC_Table.id)已經是PRIMARY KEY列,您不需要爲它們建立索引。

ALTER TABLE A_Table 
    ADD INDEX (reference), 
    ADD INDEX (code); 
ALTER TABLE B_Table 
    ADD INDEX (reference), 
    ADD INDEX (code, reference); 
ALTER TABLE C_Table 
    ADD INDEX (updated_by); 
ALTER TABLE D_Table 
    ADD INDEX (user_id); 
+0

我建議在(代碼,參考)上也添加索引到A和B表 – alko

+0

@alko - 良好的捕獲,我錯過了(B.code,B.ref)。沒有看到解釋,我不知道我看到哪裏(A.code,A.ref)會被使用 –