有一条sql语句 select a.name ,b.name from tableA a,tableB b where a.id = b.id and (其他过滤条件)
其中a.id 为tableA主键 有唯一性索引 数据量才1千多
b.id 为日志表tableB 的外键 ,慢去条件的才8000多条记录
用如上语句查询比较慢(可以明显感觉有停顿)如果改成这样:select a.name ,b.name from tableA a,tableB b where b.id = a.id and .....
既把等值比较的顺序改变一下即可
速度可以接受不知道什么原因,等值比较的原理是什么?请不吝赐教
其中a.id 为tableA主键 有唯一性索引 数据量才1千多
b.id 为日志表tableB 的外键 ,慢去条件的才8000多条记录
用如上语句查询比较慢(可以明显感觉有停顿)如果改成这样:select a.name ,b.name from tableA a,tableB b where b.id = a.id and .....
既把等值比较的顺序改变一下即可
速度可以接受不知道什么原因,等值比较的原理是什么?请不吝赐教
第一种情况 里面有一个提示:Range checked for each record (index map: 0x2)
而另外一个是useing where
其实上面叫你贴出那些完整结果,你都没贴,别人很难帮你分析的从你上面extra抓的信息看来,你后面的查询条件应该还有范围查询之类的情况建议你贴完整吧,如这样:
mysql> explain select * from tb_1 \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: tb_1
type: index
possible_keys: NULL
key: ix_tb_1b
key_len: 68
ref: NULL
rows: 1
Extra: Using index
1 row in set (0.02 sec)mysql>有这样的结果才容易分析
1 PRIMARY <derived4> ALL 33
4 DERIVED t21 ALL 33
2 DERIVED <derived3> ALL 8170
2 DERIVED t02 ALL INDEX_JBPMWFID 1216 Range checked for each record (index map: 0x2)
3 DERIVED t30 ALL 89465 Using where; Using temporary; Using filesort
*************************************************************************************第二种方式 比较快的方式1 PRIMARY <derived2> ALL 299 Using temporary; Using filesort
1 PRIMARY <derived4> ALL 33
4 DERIVED t21 ALL 33
2 DERIVED <derived3> ALL 8170
2 DERIVED t02 ALL INDEX_JBPMWFID 1216 Using where
3 DERIVED t30 ALL 89465 Using where; Using temporary; Using filesort
以上就是用explain + sql语句 所有的输出了
请高手赐教
+------+---------------------+
| id | date |
+------+---------------------+
| 1 | 2009-10-10 11:11:11 |
| 2 | 2009-10-10 11:11:13 |
| 3 | 2009-10-10 11:11:15 |
| 4 | 2009-10-10 11:11:17 |
+------+---------------------+
4 rows in set (0.08 sec)mysql>