mysql索引创建和使用细节
最近困扰自己很久的膝盖积液手术终于做完,在家养伤,逛技术博客看到easyswoole开发组成员仙士可博客有关mysql索引方面的知识,自己打算重温下。
正常业务起步数据表数据量较少,不用考虑使用索引,当后期累积的数据数量非常可观时,使用索引是提升查询的一条途径,其他的像表分区,分库分表等等。
【索引创建】
索引的创建需要考虑被创建索引的字段区分度,比如一张表里面有渠道channel,渠道可期种类不超过3种,win系,安卓系,iOS系,而数据表数据量有一百万,平均下来每个渠道各是1/3也就是33万数据,这样的数据量就是否基于channel 索引区别都不会太大。
但是如果基于date字段做索引,如20200114,一年一百万,除以365天,平均下来每天300条数据。这个区分度是相当大。
同样的索引使用 33w数据查询显然效率低于300条数据。
索引可以加快mysql服务查询速度,但不是索引越多越好,因为insert或update的同时存放索引的文件也需要进行更新,会影响数据插入更新的速度,如果对数据实时性有要求的,无疑会受较大影响。
这里挑两种情况演示给大家看下。
【索引失效】
一. 单字段索引:字段是string类型,传入int类型参数。
MySQL [test_db]> show create table test_users\G;
*************************** 1. row ***************************
Table: test_users
Create Table: CREATE TABLE `test_users` (
`uid` int(11) unsigned NOT NULL AUTO_INCREMENT,
`username` char(15) NOT NULL,
`created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_id` char(11) NOT NULL DEFAULT '',
PRIMARY KEY (`uid`),
KEY `testindex` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1306001 DEFAULT CHARSET=utf8mb4
1 row in set (0.04 sec) ERROR: No query specified #开启profile
MySQL [test_db]> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.03 sec)
#开始查询
MySQL [test_db]> select * from test_users where user_id='';
Empty set (0.04 sec) MySQL [test_db]> select * from test_users where user_id=97737;
Empty set (0.14 sec) #关闭profile
MySQL [test_db]> set profiling=0;
Query OK, 0 rows affected, 1 warning (0.03 sec) #explain查看一下
MySQL [test_db]> explain select * from test_users where user_id='' ;
+----+-------------+------------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
| 1 | SIMPLE | test_users | NULL | ref | testindex | testindex | 44 | const | 1 | 100.00 | NULL |
+----+-------------+------------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.05 sec) MySQL [test_db]> explain select * from test_users where user_id=97737;
+----+-------------+------------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| 1 | SIMPLE | test_users | NULL | ALL | testindex | NULL | NULL | NULL | 306078 | 10.00 | Using where |
+----+-------------+------------+------------+------+---------------+------+---------+------+--------+----------+-------------+
1 row in set, 3 warnings (0.04 sec)
#以上可见当使用user_id匹配int类型时,key=null,索引失效
#再看profile分析结果,可见加单引号比起不加单引号快上10倍左右
MySQL [test_db]> show profiles;
+----------+------------+-------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-------------------------------------------------+
| 1 | 0.01234100 | select * from test_users where user_id='' |
| 2 | 0.10183000 | select * from test_users where user_id=97737 |
+----------+------------+-------------------------------------------------+
2 rows in set, 1 warning (0.04 sec) #再看更详细的分析
MySQL [test_db]> show profile cpu,block io,swaps for query 1;
+----------------------+----------+----------+------------+--------------+---------------+-------+
| Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out | Swaps |
+----------------------+----------+----------+------------+--------------+---------------+-------+
| starting | 0.000088 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| checking permissions | 0.000006 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| Opening tables | 0.000021 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| init | 0.003386 | 0.001000 | 0.000000 | 240 | 0 | 0 |
| System lock | 0.000027 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| optimizing | 0.000011 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| statistics | 0.007039 | 0.000000 | 0.000000 | 592 | 0 | 0 |
| preparing | 0.000023 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| executing | 0.000003 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| Sending data | 0.001661 | 0.000000 | 0.000000 | 176 | 0 | 0 |
| end | 0.000008 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| query end | 0.000011 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| closing tables | 0.000012 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| freeing items | 0.000044 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| cleaning up | 0.000003 | 0.000000 | 0.000000 | 0 | 0 | 0 |
+----------------------+----------+----------+------------+--------------+---------------+-------+
15 rows in set, 1 warning (0.03 sec) MySQL [test_db]> show profile cpu,block io,swaps for query 2;
+----------------------+----------+----------+------------+--------------+---------------+-------+
| Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out | Swaps |
+----------------------+----------+----------+------------+--------------+---------------+-------+
| starting | 0.000081 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| checking permissions | 0.000006 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| Opening tables | 0.000022 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| init | 0.002129 | 0.000000 | 0.000000 | 72 | 0 | 0 |
| System lock | 0.000010 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| optimizing | 0.000009 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| statistics | 0.000028 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| preparing | 0.000014 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| executing | 0.000002 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| Sending data | 0.099419 | 0.092986 | 0.000000 | 400 | 0 | 0 |
| end | 0.000016 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| query end | 0.000012 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| closing tables | 0.000026 | 0.001000 | 0.000000 | 0 | 0 | 0 |
| freeing items | 0.000054 | 0.000000 | 0.000000 | 0 | 0 | 0 |
| cleaning up | 0.000003 | 0.000000 | 0.000000 | 0 | 0 | 0 |
+----------------------+----------+----------+------------+--------------+---------------+-------+
15 rows in set, 1 warning (0.04 sec)
#通过对比可以发现主要耗时在sending data,而其他地方相差不大
#mysql官网对sending data对解释
#Sending data:The thread is reading and processing rows for a SELECT statement, and sending data to the client.
#Because operations occurring during this state tend to perform large amounts of disk access (reads), it is often the longest-running state over the lifetime of a given query.
#大意即是:线程正在为一个select语句读取和处理行,并且发送数据到客户端。因为这期间操作倾向于大量的磁盘访问(读取),所以这常是整个查询周期中运行时间最长的阶段。
未完待续,下一篇讲int类型,传入string类型参数有什么不一样...
mysql索引创建和使用细节的更多相关文章
- mysql索引创建和使用细节(二)
上篇粗略记录当mysql字段类型是string,传入int类型参数后失效当问题. 现在测试下mysql字段是int类型,传参string类型会发生什么. 题外话,最近膝盖手术后还在家养伤中,只怪自己以 ...
- 索引-mysql索引创建、查看、删除及使用示例
mysql索引创建.查看.删除及使用示例 1.创建索引: ALTER TABLE用来创建普通索引.UNIQUE索引或PRIMARY KEY索引. ALTER TABLE table_name ADD ...
- Database基础(二):MySQL索引创建与删除、 MySQL存储引擎的配置
一.MySQL索引创建与删除 目标: 本案例要求熟悉MySQL索引的类型及操作方法,主要练习以下任务: 普通索引.唯一索引.主键索引的创建/删除 自增主键索引的创建/删除 建立员工表yg.工资表gz, ...
- MySQl索引创建
一.什么是索引? 索引用来快速地寻找那些具有特定值的记录,所有MySQL索引都以B-树的形式保存.如果没有索引,执行查询时MySQL必须从第一个记录开始扫描整个表的所有记录,直至找到符合要求的记录.表 ...
- Mysql索引创建及删除
1.索引 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的My ...
- mysql索引 ->创建索引、修改索引、删除索引的命令语句
查看表中已经存在 index:show index from table_name; 创建和删除索引索引的创建可以在CREATE TABLE语句中进行,也可以单独用CREATE INDEX或ALTER ...
- MYSQL 索引创建与使用
可能用到索引的地方: where 子句,order by,group by 不需要创建索引的情况: 1. 表比较小 2.赋值有限的列(枚举),不要创建索引.创建的索引返回的行越少越好,此时区分度大. ...
- mysql索引创建&查看&删除
1.索引作用 在索引列上,除了上面提到的有序查找之外,数据库利用各种各样的快速定位技术,能够大大提高查询效率.特别是当数据量非常大,查询涉及多个表时,使用索引往往能使查询速度加快成千上万倍. 例如,有 ...
- Mysql索引(究极无敌细节版)
参考了: https://www.jianshu.com/p/ace3cd6526c4 推荐up主https://space.bilibili.com/377905911 推荐书籍<mysql是 ...
随机推荐
- Python--day27--设计模式
设计模式:单例模式
- HDU 1372
题意:模拟国际象棋马的走棋方式,和中国象棋一样马走日,8X8的棋盘,问从起点到终点的最短步数,国际象棋中数字代表行row,字母代表列column, 思路:记忆化深搜. #include<cstd ...
- Django入门3--Models
- MSBuild 常用参数
本文告诉大家在 MSBuild 里面常用的参数 一般的 msbuild 在编译的时候都会添加很多参数,用法如下 进入对应编译的 sln 或 csproj 文件所在的文件夹,执行下面命名 msbuild ...
- Linux 内核驱动结构嵌入
如同大部分驱动核心结构的情形, device_driver 结构常常被发现嵌到一个更高级的, 总 线特定的结构. lddbus 子系统不会和这样的趋势相反, 因此它已定义了它自己的 ldd_drive ...
- es6笔记 day4---模块化
模块化: 注意:需要放到服务器环境 1.如何定义模块? export 东西 export const a = 12; export { a as aaa, b as banana } 2 ...
- <% %>、<%! %>和<%= %>
<% %> 此标签内的内容在jsp编译的时候,将被编译成servlet的_jspService()方法.这个方法用作服务器端向客户端输出.因此这对标签里边不能在定义方法了,因为在Java ...
- dotnet 修复找不到 System.ServiceProcess 定义
本文告诉大家如果复制网上一段代码发现 System.ServiceProcess 提示找不到方法或定义,需要手动添加引用 例如下面一段代码 using System.ServiceProcess; p ...
- Trendalyzer is an information visualization software
Trendalyzer is an information visualization software for animation of statistics that was initially ...
- 写时拷贝COW(copy-on-write)
写时拷贝技术是通过"引用计数"实现的,在分配空间的时候多分配4个字节,用来记录有多少个指针指向块空间,当有新的指针指向这块空间时,引用计数加一,当要释放这块空间时,引用计数 ...