关于MYSQL中OR语句有关的优化问题
时间:2011-07-30
来源:互联网
select id,char_name from table_chara where attack_id= 123 or defend_id= 123
我对attack_id和defend_id分别设置了Normal类型的索引
这样子搞的话是全盘扫描,对吧?
照网上的做法,用union all的办法,将上面改成:
select id,char_name from table_chara where attack_id= 123 union all select id,char_name from table_chara where defend_id= 123
我通过explain分析,得出下面的结果:
+------+--------------+----------------+------+---------------+-----------+---------+-------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+--------------+----------------+------+---------------+-----------+---------+-------+------+----------------+
| 1 | PRIMARY | log_pet_battle | ref | attack_id | attack_id | 8 | const | 1 | |
| 2 | UNION | log_pet_battle | ref | defend_id | defend_id | 9 | const | 4 | Using where |
| NULL | UNION RESULT | <union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | Using filesort |
+------+--------------+----------------+------+---------------+-----------+---------+-------+------+----------------+
请问这种优化方法正确吗?最后那一行是什么意思呢?
我对attack_id和defend_id分别设置了Normal类型的索引
这样子搞的话是全盘扫描,对吧?
照网上的做法,用union all的办法,将上面改成:
select id,char_name from table_chara where attack_id= 123 union all select id,char_name from table_chara where defend_id= 123
我通过explain分析,得出下面的结果:
+------+--------------+----------------+------+---------------+-----------+---------+-------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+--------------+----------------+------+---------------+-----------+---------+-------+------+----------------+
| 1 | PRIMARY | log_pet_battle | ref | attack_id | attack_id | 8 | const | 1 | |
| 2 | UNION | log_pet_battle | ref | defend_id | defend_id | 9 | const | 4 | Using where |
| NULL | UNION RESULT | <union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | Using filesort |
+------+--------------+----------------+------+---------------+-----------+---------+-------+------+----------------+
请问这种优化方法正确吗?最后那一行是什么意思呢?
作者: moumouguo 发布时间: 2011-07-30
你的方法可以。最后一行是合并两个结果集。
作者: ACMAIN_CHM 发布时间: 2011-07-30
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28