日期:2014-05-16  浏览次数:20803 次

关于sql语句的问题
EXPLAIN
SELECT b.CO,d.C3,d.C4 FROM 
ST32 b
LEFT JOIN 
ST100_main c 
ON b.CO = c.CO
LEFT JOIN 
ST100_2 d 
ON c.CV = d.CV
WHERE b.CO LIKE '%600004%'
结果如下:
 id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------------+-------+--------------------------+
| 1 | SIMPLE | b | index | NULL | COC6 | 103 | NULL | 29616 | Using where; Using index |
| 1 | SIMPLE | c | ref | PRIMARY | PRIMARY | 98 | dzhst2.b.CO | 6263 | |
| 1 | SIMPLE | d | ref | PRIMARY,CVC3 | CVC3 | 8 | dzhst2.c.CV | 19 |  
但是我在这里加上一个d.C3 = '20120417000000' 条件之后速度就特别慢。
EXPLAIN
SELECT b.CO,d.C3,d.C4 FROM 
ST32 b
LEFT JOIN 
ST100_main c 
ON b.CO = c.CO
LEFT JOIN 
ST100_2 d 
ON c.CV = d.CV
WHERE b.CO LIKE '%600004%' AND d.C3 = '20120417000000';
+----+-------------+-------+------+---------------+------+---------+-------------------+--------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+-------------------+--------+--------------------------+
| 1 | SIMPLE | c | ALL | PRIMARY,CV | NULL | NULL | NULL | 626345 | |
| 1 | SIMPLE | b | ref | PRIMARY,COC6 | COC6 | 98 | dzhst2.c.CO | 1 | Using where; Using index |
| 1 | SIMPLE | d | ref | PRIMARY,CVC3 | CVC3 | 17 | dzhst2.c.CV,const | 1 | Using where |
+----+-------------+-------+------+---------------+------+---------+-------------------+--------+--------------------------
这里在ST100_main c 后面添加 force index(PRIMARY) 也没有效果。show index 语句也贴一下吧,
SHOW INDEX FROM ST100_main;
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| ST100_main | 0 | PRIMARY | 1 | CO | A | NULL | NULL | NULL | | BTREE | | |
| ST100_main | 0 | PRIMARY | 2 | C1 | A | 626345 | NULL | NULL | | BTREE | | |
| ST100_main | 1 | CV | 1 | CV | A | 626345 | NULL | NULL | YES | BTREE | | |
| ST100_main | 1 | C1 | 1 | C1 | A | 257 | NULL | NULL | | BTREE | | |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
谢谢耐心看完这些东西,给我一点指点。

------解决方案--------------------
WHERE b.CO LIKE '%600004%':无法使用索引
------解决方案--------------------
引用SHOW INDEX FROM ST100_main;