MySQL [cloud]> explain SELECT -> COUNT(*) -> FROM -> ( -> SELECT -> eb.slyGroupCo AS groupCo, -> e.code AS entCo, -> e.name AS entNa, -> e.show_meth AS showMeth, -> e.type + '' AS tag, -> min(eb.BeginDa) AS beginda, -> max(eb.EndDa) AS endda, , -> GROUP_CONCAT(p.`Name`) AS pkgs -> FROM -> shopbi eb, -> shop dt, -> prodg p, -> ent e, -> bdf bd -> WHERE -> e.id = eb.enterpriseid -> AND eb.id = dt.ShopBillID -> AND dt.ProductPKGID = p.ID -> AND eb.shopState = 0 -> AND eb.slymdCode IS NOT NULL -> AND bd.DESC_ = 'S024739' -> AND e.ORGID = bd.ID_ -> AND e.code LIKE '%ENT8853%' -> GROUP BY -> e.id, -> eb.id -> ORDER BY -> max(eb.createDate) DESC -> ) a; +----+-------------+------------+--------+-----------------------------------------------------+--------------------+---------+---------------------------+-------+---------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+-----------------------------------------------------+--------------------+---------+---------------------------+-------+---------------------------------+ | 1 | PRIMARY | | ALL | NULL | NULL | NULL | NULL | 23072 | NULL | | 2 | DERIVED | p | ALL | PRIMARY | NULL | NULL | NULL | 224 | Using temporary; Using filesort | | 2 | DERIVED | dt | ref | PRIMARY,FKD55C954DBC4687E4,FKD55C954DA4BA8D24 | FKD55C954DA4BA8D24 | 98 | cloudplat.p.ID | 103 | Using index | | 2 | DERIVED | eb | eq_ref | PRIMARY,FKEB7193FD322835EC,INDEX_SHOPBILL_SLYMDCODE | PRIMARY | 98 | cloudplat.dt.ShopBillID | 1 | Using where | | 2 | DERIVED | e | eq_ref | PRIMARY,FKD027336111B1115F | PRIMARY | 98 | cloudplat.eb.EnterpriseID | 1 | Using where | | 2 | DERIVED | bd | eq_ref | PRIMARY | PRIMARY | 182 | cloudplat.e.ORGID | 1 | Using where | +----+-------------+------------+--------+-----------------------------------------------------+--------------------+---------+---------------------------+-------+---------------------------------+ 6 rows inset (0.002 sec
1 看到一个MySQL的语句,首先我先看type,尤其复杂的SQL,先看type这里有没有 ALL, 在type中ALL的字段出现,说明在这部分存在全表扫描,那么首先我们就要先分析这块的部分是否有问题。在查看了表信息后,发现对应的索引和主键都是齐全的,关键为什么走了ALL,主要的原因是一个表中的行数非常的少,且与另一个表连接是主外键关系,所以必然对于小表走了全表扫描,所以排除了这部分索引有问题。
MySQL [cloud]> select count(*) from product; +----------+ | count(*) | +----------+ | 261 | +----------+ 1 row inset (0.002 sec)
MySQL [cloudplat]> select count(*) from shopbi; +----------+ | count(*) | +----------+ | 935312 | +----------+ 1 row inset (1.174 sec)