2014-03-24 174 views
-2

我有這個疑問:MySQL查詢與多個INNER JOIN太慢

$sql="SELECT t1.*, t2.*, t3.*,t4.*,t5.*,t6.*,t7.*,t8.*,t9.asciiname as asciiname_9, t10.asciiname as asciiname_10,t11.*,t12.* 
     FROM anunturi t1 
      INNER JOIN tip_proprietate t2 
        ON t1.tip = t2.id_prop 
      INNER JOIN anunt_preturi t3 
        ON t1.id_anunt = t3.ext 
      INNER JOIN anunturi_images t5 
        ON t1.id_anunt = t5.id_ext 
      INNER JOIN tranzactie t4 
        ON t1.tranzactie = t4.id_tranzactie 
      INNER JOIN anunt_descriere t6 
        ON t1.id_anunt = t6.ext 
      INNER JOIN anunt_locatie t7 
        ON t1.id_anunt = t7.ext 
      INNER JOIN zone t8 
        ON t7.zona = t8.id_zona 
      INNER JOIN locatii t9 
        ON t7.judet = t9.admin1_code 
        AND t9.feature_code='ADM1' 
      INNER JOIN locatii t10 
        ON t7.oras = t10.geonameid 
      INNER JOIN anunt_suprafete t11 
        ON t1.id_anunt = t11.ext 
      INNER JOIN tip_locuinta t12 
        ON t1.tip2 = t12.id_loc 
    GROUP BY t1.id_anunt 
     WHERE t1.status=0 
    ORDER BY t1.id_anunt DESC 
     LIMIT 3"; 

,我查詢之前使用SET SQL_BIG_SELECTS=1。它運行速度很慢。

+0

你有IND exes在你加入的列上?你真的需要所有表中的所有列嗎?這需要多久? – Prix

+1

索引,表格,解釋,廚房水槽。 – Mihai

+0

我有索引列,我需要所有的列。 – user3456749

回答

1

除了試圖從幾乎每個桌子拉每一列,你的狀態 你已經有指標,我會盡力確保以下覆蓋索引

table   index 
locatii  (feature_code, admin1_code, asciiname) 
locatii  (geonameid, asciiname) <-- for use when join via T10 
anunt_locatie (judet, zona, oras, ext) 
anunturi  (id_anunt, status) 

然後,我會重組查詢放T9別名第一,如果該表 預計將有一組較小的記錄返回VS基於零狀態T1別名 ...並添加關鍵字「STRAIGHT_JOIN」

SELECT STRAIGHT_JOIN 
     t1.*, 
     t2.*, 
     t3.*, 
     t4.*, 
     t5.*, 
     t6.*, 
     t7.*, 
     t8.*, 
     t9.asciiname as asciiname_9, 
     t10.asciiname as asciiname_10, 
     t11.*, 
     t12.* 
    FROM 
     locatii t9 
     INNER JOIN anunt_locatie t7 
      ON t9.admin1_code = t7.judet 

      INNER JOIN zone t8 
       ON t7.zona = t8.id_zona 

      INNER JOIN locatii t10 
       ON t7.oras = t10.geonameid 

      INNER JOIN anunturi t1 
       ON t7.ext = t1.id_anunt 
       AND t1.status = 0 
       INNER JOIN tip_proprietate t2 
        ON t1.tip = t2.id_prop 
       INNER JOIN anunt_preturi t3 
        ON t1.id_anunt = t3.ext 
       INNER JOIN tranzactie t4 
        ON t1.tranzactie = t4.id_tranzactie 
       INNER JOIN anunturi_images t5 
        ON t1.id_anunt = t5.id_ext 
       INNER JOIN anunt_descriere t6 
        ON t1.id_anunt = t6.ext 
       INNER JOIN anunt_suprafete t11 
        ON t1.id_anunt = t11.ext 
       INNER JOIN tip_locuinta t12 
        ON t1.tip2 = t12.id_loc 
    GROUP BY 
     t1.id_anunt 
    WHERE 
     t9.feature_code='ADM1' 
    ORDER BY 
     t1.id_anunt DESC 
    LIMIT 3