如何优化一些内部连接Mysql查询

如何优化一些内部连接Mysql查询,mysql,inner-join,Mysql,Inner Join,我的网站每天都有更多的流量,我想知道如何优化MYSQL数据库的这种查询类型 Query_time: 37 Lock_time: 2 Rows_sent: 10 Rows_examined: 354287 SET timestamp=1323180396; 查询: Results for explain extended SELECT a.*, UNIX_TIMESTAMP(a.createdon) AS timestamp, ct.cityname, ct.SeoCityName, sc

我的网站每天都有更多的流量,我想知道如何优化MYSQL数据库的这种查询类型

Query_time: 37 
Lock_time: 2 
Rows_sent: 10 
Rows_examined: 354287
SET timestamp=1323180396;
查询:

Results for explain extended SELECT a.*, UNIX_TIMESTAMP(a.createdon) AS timestamp, ct.cityname, ct.SeoCityName, scat.subcatname, scat.SeoSubcatName, cat.catid, cat.SeoCatName, cat.catname, 
          COUNT(*) AS piccount, p.picfile, UNIX_TIMESTAMP(feat.featuredtill) AS featuredtill 
            FROM prefix_ads a
                 INNER JOIN prefix_es_cities ct ON a.cityid = ct.cityid
         INNER JOIN prefix_es_subcats scat ON a.subcatid = scat.subcatid
         INNER JOIN prefix_es_cats cat ON scat.catid = cat.catid
        LEFT OUTER JOIN prefix_adxfields axf ON a.adid = axf.adid
                LEFT OUTER JOIN prefix_adpics p ON a.adid = p.adid AND p.isevent = '0'        
                LEFT OUTER JOIN prefix_es_featured feat ON a.adid = feat.adid AND feat.adtype = 'A'        
            WHERE scat.catid = 2 
                AND a.enabled = '1' AND a.verified = '1' AND a.expireson >= NOW()                   
            GROUP BY a.adid
            ORDER BY a.createdon DESC
            LIMIT 0, 18:
现在我们开始解释我在数据库连接中使用PDO

Array
(
    [id] => 1
    [0] => 1
    [select_type] => SIMPLE
    [1] => SIMPLE
    [table] => cat
    [2] => cat
    [type] => const
    [3] => const
    [possible_keys] => PRIMARY
    [4] => PRIMARY
    [key] => PRIMARY
    [5] => PRIMARY
    [key_len] => 2
    [6] => 2
    [ref] => const
    [7] => const
    [rows] => 1
    [8] => 1
    [filtered] => 100.00
    [9] => 100.00
    [Extra] => Using temporary; Using filesort
    [10] => Using temporary; Using filesort
)
Array
(
    [id] => 1
    [0] => 1
    [select_type] => SIMPLE
    [1] => SIMPLE
    [table] => scat
    [2] => scat
    [type] => ref
    [3] => ref
    [possible_keys] => PRIMARY,catid
    [4] => PRIMARY,catid
    [key] => catid
    [5] => catid
    [key_len] => 2
    [6] => 2
    [ref] => const
    [7] => const
    [rows] => 12
    [8] => 12
    [filtered] => 100.00
    [9] => 100.00
    [Extra] => 
    [10] => 
)
Array
(
    [id] => 1
    [0] => 1
    [select_type] => SIMPLE
    [1] => SIMPLE
    [table] => a
    [2] => a
    [type] => ref
    [3] => ref
    [possible_keys] => subcatid,cityid,verified,enabled,expireson
    [4] => subcatid,cityid,verified,enabled,expireson
    [key] => subcatid
    [5] => subcatid
    [key_len] => 2
    [6] => 2
    [ref] => some_DB.scat.subcatid
    [7] => some_DB.scat.subcatid
    [rows] => 354287
    [8] => 354287
    [filtered] => 100.00
    [9] => 100.00
    [Extra] => Using where
    [10] => Using where
)
Array
(
    [id] => 1
    [0] => 1
    [select_type] => SIMPLE
    [1] => SIMPLE
    [table] => ct
    [2] => ct
    [type] => eq_ref
    [3] => eq_ref
    [possible_keys] => PRIMARY
    [4] => PRIMARY
    [key] => PRIMARY
    [5] => PRIMARY
    [key_len] => 2
    [6] => 2
    [ref] => some_DB.a.cityid
    [7] => some_DB.a.cityid
    [rows] => 1
    [8] => 1
    [filtered] => 100.00
    [9] => 100.00
    [Extra] => 
    [10] => 
)
Array
(
    [id] => 1
    [0] => 1
    [select_type] => SIMPLE
    [1] => SIMPLE
    [table] => axf
    [2] => axf
    [type] => ref
    [3] => ref
    [possible_keys] => adid
    [4] => adid
    [key] => adid
    [5] => adid
    [key_len] => 4
    [6] => 4
    [ref] => some_DB.a.adid
    [7] => some_DB.a.adid
    [rows] => 1
    [8] => 1
    [filtered] => 100.00
    [9] => 100.00
    [Extra] => Using index
    [10] => Using index
)
Array
(
    [id] => 1
    [0] => 1
    [select_type] => SIMPLE
    [1] => SIMPLE
    [table] => p
    [2] => p
    [type] => ref
    [3] => ref
    [possible_keys] => adid
    [4] => adid
    [key] => adid
    [5] => adid
    [key_len] => 5
    [6] => 5
    [ref] => some_DB.a.adid,const
    [7] => some_DB.a.adid,const
    [rows] => 3
    [8] => 3
    [filtered] => 100.00
    [9] => 100.00
    [Extra] => 
    [10] => 
)
Array
(
    [id] => 1
    [0] => 1
    [select_type] => SIMPLE
    [1] => SIMPLE
    [table] => feat
    [2] => feat
    [type] => eq_ref
    [3] => eq_ref
    [possible_keys] => adid
    [4] => adid
    [key] => adid
    [5] => adid
    [key_len] => 7
    [6] => 7
    [ref] => some_DB.a.adid,const
    [7] => some_DB.a.adid,const
    [rows] => 1
    [8] => 1
    [filtered] => 100.00
    [9] => 100.00
    [Extra] => 
    [10] => 
)
同一查询的更多解释

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
1   SIMPLE  cat     const   PRIMARY     PRIMARY     2   const   1   Using temporary; Using filesort
1   SIMPLE  scat    ref     PRIMARY,catid   catid   2   const   12  
1   SIMPLE  a   ref     subcatid,cityid,verified,enabled,expireson  subcatid    2   some_DB.scat.subcatid   354287  Using where
1   SIMPLE  ct  eq_ref  PRIMARY     PRIMARY     2   some_DB.a.cityid    1   
1   SIMPLE  axf     ref     adid    adid    4   some_DB.a.adid  1   Using index
1   SIMPLE  p   ref     adid    adid    5   some_DB.a.adid,const    3   
1   SIMPLE  feat    eq_ref  adid    adid    7   some_DB.a.adid,const    1 
谢谢

尝试此查询

SELECT 
    a.*, 
    UNIX_TIMESTAMP(a.createdon) AS timestamp, 
    ct.cityname, 
    ct.SeoCityName, 
    scat.subcatname, 
    scat.SeoSubcatName, 
    cat.catid, 
    cat.SeoCatName, 
    cat.catname, 
    COUNT(*) AS piccount, 
    p.picfile, 
    UNIX_TIMESTAMP(feat.featuredtill) AS featuredtill 
FROM 
    prefix_ads a
INNER JOIN 
    prefix_es_cities ct 
ON 
    a.enabled = '1' AND 
    a.verified = '1' AND 
    a.cityid = ct.cityid  AND 
    a.expireson >= NOW()                   
INNER JOIN 
    prefix_es_subcats scat 
ON 
    scat.catid = 2 AND a.subcatid = scat.subcatid
INNER JOIN 
    prefix_es_cats cat 
ON 
    scat.catid = cat.catid
LEFT OUTER JOIN 
    prefix_adxfields axf 
ON 
    a.adid = axf.adid
LEFT OUTER JOIN 
    prefix_adpics p 
ON 
    p.isevent = '0' AND a.adid = p.adid         
LEFT OUTER JOIN 
    prefix_es_featured feat 
ON 
    feat.adtype = 'A' AND a.adid = feat.adid         
GROUP BY 
    a.adid
ORDER BY 
    a.createdon DESC
LIMIT 0, 18;
还可以在联接中使用的表的列上创建必要的索引,并按前缀\u ads.createdon、前缀\u ads.adid…排序


希望这对您有所帮助….

您需要一些索引必须添加解释谢谢您的帮助,但实际上我的查询解释输出与使用临时文件和使用文件检查的原始数据数量相同。在这个问题上,你还有其他的想法吗?