博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
order by不走索引的思考
阅读量:6922 次
发布时间:2019-06-27

本文共 2446 字,大约阅读时间需要 8 分钟。

mysql> desc user;

+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id     | int(11)     | NO   | PRI | NULL    | auto_increment |
| userid | varchar(30) | NO   | MUL | NULL    |                |
| server | varchar(10) | NO   |     | NULL    |                |
| date   | datetime    | NO   |     | NULL    |                |
+--------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> show index from user;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| user  |          0 | PRIMARY  |            1 | id          | A         |           6 |     NULL | NULL   |      | BTREE      |         |               |
| user  |          1 | userid   |            1 | userid      | A         |           6 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
 

mysql> explain select * from user order by userid;   

+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | user  | ALL  | NULL          | NULL | NULL    | NULL |    6 | Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)

没有用到索引,改之:

mysql> explain select id,userid from user order by userid;          

+----+-------------+-------+-------+---------------+--------+---------+------+------+-------------+
| id | select_type | table | type  | possible_keys | key    | key_len | ref  | rows | Extra       |
+----+-------------+-------+-------+---------------+--------+---------+------+------+-------------+
|  1 | SIMPLE      | user  | index | NULL          | userid | 32      | NULL |    6 | Using index |
+----+-------------+-------+-------+---------------+--------+---------+------+------+-------------+
1 row in set (0.00 sec)

已用到索引。

 

本文转自 liang3391 51CTO博客,原文链接:http://blog.51cto.com/liang3391/827115

转载地址:http://tvujl.baihongyu.com/

你可能感兴趣的文章
电感器
查看>>
百度编辑器序号和项目符号不能显示解决
查看>>
ios8--加载图片
查看>>
Java读写文件的几种方式
查看>>
SQL Server BI Step by Step SSIS 7 (End) --- 事务,错误输出,事件处理,日志记录
查看>>
ADO.NET基础复习(二)
查看>>
IE浏览器无法上网设置
查看>>
ASP.NET 中的 authentication(验证)与authorization(授权)
查看>>
EBS应用临时文件或数据的清理
查看>>
JQuery ajax调用asp.net的webMethod
查看>>
android 64 sd卡读写的操作
查看>>
3.Swift翻译教程系列——Swift基础知识
查看>>
获得内核函数地址的四种方法
查看>>
在接口中不要存在实现代码
查看>>
字符输入流Reader简要概括
查看>>
C#知识点总结系列:4、C#中Monitor和Lock以及区别
查看>>
Google Adsense 的秘密-如何用Google Adsense赚更多的钱
查看>>
Ext.Net学习笔记22:Ext.Net Tree 用法详解
查看>>
oracle 12c install for linux
查看>>
嘀咕 - Windows Phone 7版本
查看>>