具有多个INNER JOIN的MySQL查询太慢

时间:2014-03-24 18:31:55

标签: mysql sql

我有这个问题:

$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。它运行得很慢。

1 个答案:

答案 0 :(得分: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别名 与t1别名相比,预计会返回较小的记录集 基于零状态...并添加关键字&#34; STRAIGHT_JOIN&#34;

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