问题一:
1.有什么可视化的,能够特别直观的分析SQL语句性能的工具?
问题二:
select * limit offset,amount;
select * where id between offset and offset+amount;
select * where id > offset limit amount;
以上三个SQL语句,哪个性能更佳呢?项目中我们一般用的好像是limit a b 这样的吧,性能又如何呢?
问题一:
1.有什么可视化的,能够特别直观的分析SQL语句性能的工具?
问题二:
select * limit offset,amount;
select * where id between offset and offset+amount;
select * where id > offset limit amount;
以上三个SQL语句,哪个性能更佳呢?项目中我们一般用的好像是limit a b 这样的吧,性能又如何呢?
explain
根据上面提到的explain去比较,就可以得出结果了
mysql> explain select * from users limit 1000,20;
+----+-------------+-------+------+---------------+------+---------+------+--------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+--------+-------+
| 1 | SIMPLE | users | ALL | NULL | NULL | NULL | NULL | 169847 | |
+----+-------------+-------+------+---------------+------+---------+------+--------+-------+
1 row in set (0.00 sec)
mysql> explain select * from users where uid>1000 limit 20;
+----+-------------+-------+-------+---------------+---------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+------+-------+-------------+
| 1 | SIMPLE | users | range | PRIMARY | PRIMARY | 4 | NULL | 84923 | Using where |
+----+-------------+-------+-------+---------------+---------+---------+------+-------+-------------+
1 row in set (0.00 sec)
mysql> explain select * from users where uid between 1000 and 1020;
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | users | range | PRIMARY | PRIMARY | 4 | NULL | 1 | Using where |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)
select * limit offset,amount;
select * where id between offset and offset+amount;
select * where id > offset limit amount; 这条性能最佳
为何?因为id比较走了索引,如果id没有索引,还可以加索引,请记住,select判断性能佳不佳 首先去看下自己的where条件有没有可能走索引,如果自己的几种方案都走了索引,那么再用explain去具体看下到底哪些语句性能最佳
对着这个B+树结构看。它说白了就是一个多路搜索树和一个链表的复合。树的部分支持键值查找,链表的部分支持顺序访问,但哪个都不支持随机访问。
如果你用了LIMIT i
就是告诉他访问第i
个元素,由于它不支持随机访问,它会先找到第一个叶子节点然后跳过i-1
个,就跟普通的链表没什么区别。
如你用了BETWEEN i AND j
那他就会先查找 ID 大于等于i
的第一个节点,方式是从根节点向下找。复杂性是logn
,其中n
是数据总量。也就是说遍历次数是总量的二进制位数,小很多。
15 回答8.3k 阅读
8 回答6.2k 阅读
5 回答3.2k 阅读✓ 已解决
3 回答3.6k 阅读✓ 已解决
1 回答4k 阅读✓ 已解决
3 回答6k 阅读
2 回答2.8k 阅读✓ 已解决
http://www.cnblogs.com/RunFor...