什么是索引?
1、索引
索引是表的目录,在查找内容之前可以先在目录中查找索引位置,以此快速定位查询数据。对于索引,会保存在额外的文件中。
2、索引,是数据库中专门用于帮助用户快速查询数据的一种数据结构。类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置,然后直接获取即可。
索引由数据库中一列或多列组合而成,其作用是提高对表中数据的查询速度索引的优点是可以提高检索数据的速度索引的缺点是创建和维护索引需要耗费时间索引可以提高查询速度,会减慢写入速度
索引分类
1.普通索引2.唯一索引3.全文索引4.单列索引5.多列索引6.空间索引7.主键索引8.组合索引
- 普通索引:仅加速查询
- 唯一索引:加速查询 + 列值唯一(可以有null)
- 主键索引:加速查询 + 列值唯一 + 表中只有一个(不可以有null)
- 组合索引:多列值组成一个索引,专门用于组合搜索,其效率大于索引合并
- 全文索引:对文本的内容进行分词,进行搜索
索引合并,使用多个单列索引组合搜索覆盖索引,select的数据列只用从索引中就能够取得,不必读取数据行,换句话说查询列要被所建的索引覆盖
如何创建索引?记住一个单词—explain
创建表的时候创建索引
rush: sql; gutter: true; first-line: 1 hljs"> 名称 字段类型 [完整性约束条件],
,,,,[名称](字段名称[(长度)])
[
在已经存在的表上创建索引:
rush: sql; gutter: true; first-line: 1 hljs markdown">1.CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX 索引名称 ON 表名{字段名称[(长度)] [ASC|DESC]}
2.ALTER TABLE tbl_name ADD [名称(字段名称[
如何删除索引?
rush: sql; gutter: true; first-line: 1 hljs">名称
1.普通索引
创建表+索引
rush: sql; gutter: true; first-line: 1 hljs">teral">null auto_increment primary teral">null,email teral">null,extra
创建索引
rush: sql; gutter: true; first-line: 1 hljs">
删除索引
rush: sql; gutter: true; first-line: 1 hljs">
查看索引
rush: sql; gutter: true; first-line: 1 hljs">
注意:对于创建索引时如果是BLOB 和 TEXT 类型,必须指定length。
rush: sql; gutter: true; first-line: 1 hljs">
2、唯一索引
创建表+唯一索引
rush: sql; gutter: true; first-line: 1 hljs">teral">null auto_increment primary
创建唯一索引
rush: sql; gutter: true; first-line: 1 hljs">
删除唯一索引
rush: sql; gutter: true; first-line: 1 hljs">
3、主键索引
创建表+创建主键
rush: sql; gutter: true; first-line: 1 hljs">teral">null auto_increment primary <span class="hljs-keyword">OR
<span class="hljs-keyword">create <span class="hljs-keyword">table in1(
nid <span class="hljs-built_in">int <span class="hljs-keyword">not <span class="hljs-literal">null auto_increment,primary <span class="hljs-keyword">key(ni1),<span class="hljs-keyword">index ix_name (<span class="hljs-keyword">name)
)
创建主键
rush: sql; gutter: true; first-line: 1 hljs">
删除主键
rush: sql; gutter: true; first-line: 1 hljs">
4、组合索引
组合索引是将n个列组合成一个索引
其应用场景为:频繁的同时使用n列来进行查询,如:where n1 = ‘alex’ and n2 = 666。
创建表
rush: sql; gutter: true; first-line: 1 hljs">teral">null auto_increment primary
创建组合索引
rush: sql; gutter: true; first-line: 1 hljs">
如上创建组合索引之后,查询:
- name and email – 使用索引
- name — 使用索引
- email — 不使用索引
注意:对于同时搜索n个条件时,组合索引的性能好于多个单一索引合并。
相关命令
rush: sql; gutter: true; first-line: 1 hljs">- 查看表结构 desc 表名
-
查看生成表的sql
<span class="hljs-keyword">show <span class="hljs-keyword">create <span class="hljs-keyword">table 表名 -
查看索引
<span class="hljs-keyword">show <span class="hljs-keyword">index <span class="hljs-keyword">from 表名 -
查看执行时间
<span class="hljs-keyword">set profiling = <span class="hljs-number">1;
sql...
<span class="hljs-keyword">show <span class="hljs-keyword">profiles;
使用索引和不使用索引
rush: sql; gutter: true; first-line: 1 hljs ruby">由于索引是专门用于加速搜索而生,所以加上索引之后,查询效率会快到飞起来。<span class="hljs-comment"># 有索引
mysql> select * from tb1 where name = <span class="hljs-string">'wupeiqi-888';
+-----+-------------+---------------------+----------------------------------+---------------------+
<span class="hljs-params">| nid | name <span class="hljs-params">| email | radom <span class="hljs-params">| ctime |
+-----+-------------+---------------------+----------------------------------+---------------------+
<span class="hljs-params">| 889 | wupeiqi-<span class="hljs-number">888 <span class="hljs-params">| wupeiqi888@live.com | <span class="hljs-number">5312269e76a16a90b8a8301d5314204b <span class="hljs-params">| 2016-08-03 09:33:35 |
+-----+-------------+---------------------+----------------------------------+---------------------+
<span class="hljs-number">1 row <span class="hljs-keyword">in set (<span class="hljs-number">0.<span class="hljs-number">00 sec)<span class="hljs-comment"># 无索引
mysql> select * from tb1 where email = <span class="hljs-string">'wupeiqi888@live.com';
+-----+-------------+---------------------+----------------------------------+---------------------+
<span class="hljs-params">| nid | name <span class="hljs-params">| email | radom <span class="hljs-params">| ctime |
+-----+-------------+---------------------+----------------------------------+---------------------+
<span class="hljs-params">| 889 | wupeiqi-<span class="hljs-number">888 <span class="hljs-params">| wupeiqi888@live.com | <span class="hljs-number">5312269e76a16a90b8a8301d5314204b <span class="hljs-params">| 2016-08-03 09:33:35 |
+-----+-------------+---------------------+----------------------------------+---------------------+
<span class="hljs-number">1 row <span class="hljs-keyword">in set (<span class="hljs-number">1.23 sec)
正确使用索引
数据库表中添加索引后确实会让查询速度起飞,但前提必须是正确的使用索引来查询,如果以错误的方式使用,则即使建立索引也会不奏效。
即使建立索引,索引也不会生效:
rush: sql; gutter: true; first-line: 1 hljs">- like '%xx'
函数
组合索引最左前缀
如果组合索引为:(name,email)
name and email <span class="hljs-comment">-- 使用索引
name <span class="hljs-comment">-- 使用索引
email <span class="hljs-comment">-- 不使用索引
其他注意事项
rush: sql; gutter: true; first-line: 1 hljs diff">查询时)
查询(Sub-Queries)
limit分页
rush: sql; gutter: true; first-line: 1 hljs">每页显示10条:
当前 118 120, 125
倒序:
大 小
980 970 7 6 6 5 54 43 32
21 19 98
下一页:
<span class="hljs-keyword">select
*
<span class="hljs-keyword">from
tb1
<span class="hljs-keyword">where
nid < (<span class="hljs-keyword">select nid <span class="hljs-keyword">from (<span class="hljs-keyword">select nid <span class="hljs-keyword">from tb1 <span class="hljs-keyword">where nid < 当前页最小值 <span class="hljs-keyword">order <span class="hljs-keyword">by nid <span class="hljs-keyword">desc <span class="hljs-keyword">limit 每页数据 *【<a href="https://www.jb51.cc/tag/yema/" target="_blank" class="keywords">页码</a>-当前页】) A <span class="hljs-keyword">order <span class="hljs-keyword">by A.nid <span class="hljs-keyword">asc <span class="hljs-keyword">limit <span class="hljs-number">1)
<span class="hljs-keyword">order <span class="hljs-keyword">by
nid <span class="hljs-keyword">desc
<span class="hljs-keyword">limit <span class="hljs-number">10;
<span class="hljs-keyword">select
*
<span class="hljs-keyword">from
tb1
<span class="hljs-keyword">where
nid < (<span class="hljs-keyword">select nid <span class="hljs-keyword">from (<span class="hljs-keyword">select nid <span class="hljs-keyword">from tb1 <span class="hljs-keyword">where nid < <span class="hljs-number">970 <span class="hljs-keyword">order <span class="hljs-keyword">by nid <span class="hljs-keyword">desc <span class="hljs-keyword">limit <span class="hljs-number">40) A <span class="hljs-keyword">order <span class="hljs-keyword">by A.nid <span class="hljs-keyword">asc <span class="hljs-keyword">limit <span class="hljs-number">1)
<span class="hljs-keyword">order <span class="hljs-keyword">by
nid <span class="hljs-keyword">desc
<span class="hljs-keyword">limit <span class="hljs-number">10;
上一页:
<span class="hljs-keyword">select
*
<span class="hljs-keyword">from
tb1
<span class="hljs-keyword">where
nid < (<span class="hljs-keyword">select nid <span class="hljs-keyword">from (<span class="hljs-keyword">select nid <span class="hljs-keyword">from tb1 <span class="hljs-keyword">where nid > 当前页最大值 <span class="hljs-keyword">order <span class="hljs-keyword">by nid <span class="hljs-keyword">asc <span class="hljs-keyword">limit 每页数据 *【当前页-<a href="https://www.jb51.cc/tag/yema/" target="_blank" class="keywords">页码</a>】) A <span class="hljs-keyword">order <span class="hljs-keyword">by A.nid <span class="hljs-keyword">asc <span class="hljs-keyword">limit <span class="hljs-number">1)
<span class="hljs-keyword">order <span class="hljs-keyword">by
nid <span class="hljs-keyword">desc
<span class="hljs-keyword">limit <span class="hljs-number">10;
<span class="hljs-keyword">select
*
<span class="hljs-keyword">from
tb1
<span class="hljs-keyword">where
nid < (<span class="hljs-keyword">select nid <span class="hljs-keyword">from (<span class="hljs-keyword">select nid <span class="hljs-keyword">from tb1 <span class="hljs-keyword">where nid > <span class="hljs-number">980 <span class="hljs-keyword">order <span class="hljs-keyword">by nid <span class="hljs-keyword">asc <span class="hljs-keyword">limit <span class="hljs-number">20) A <span class="hljs-keyword">order <span class="hljs-keyword">by A.nid <span class="hljs-keyword">desc <span class="hljs-keyword">limit <span class="hljs-number">1)
<span class="hljs-keyword">order <span class="hljs-keyword">by
nid <span class="hljs-keyword">desc
<span class="hljs-keyword">limit <span class="hljs-number">10;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
执行计划
explain + 查询sql – 用于显示sql执行信息参数,根据参考信息可以进行sql优化
explain select * from tb2;
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
arams">| id | select_type arams">| table | type arams">| possible_keys | key arams">| key_len | ref arams">| rows | Extra arams">|
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| arams">| SIMPLE | tb2 arams">| ALL | NULL arams">| NULL | NULL arams">| NULL | arams">| NULL |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
执行计划
explain | ALL | NULL | NULL | NULL | NULL | teral">null
select_type
<a href="https://www.jb51.cc/tag/chaxun/" target="_blank" class="keywords">查询</a>类型
SIMPLE 简单<a href="https://www.jb51.cc/tag/chaxun/" target="_blank" class="keywords">查询</a>
PRIMARY 最外层<a href="https://www.jb51.cc/tag/chaxun/" target="_blank" class="keywords">查询</a>
SUBQUERY 映射为子<a href="https://www.jb51.cc/tag/chaxun/" target="_blank" class="keywords">查询</a>
DERIVED 子<a href="https://www.jb51.cc/tag/chaxun/" target="_blank" class="keywords">查询</a>
UNION 联合
UNION RESULT 使用联合的结果
...
table
正在访问的表名
type
<a href="https://www.jb51.cc/tag/chaxun/" target="_blank" class="keywords">查询</a>时的访问方式,<a href="https://www.jb51.cc/tag/xingneng/" target="_blank" class="keywords">性能</a>:all < index < range < index_merge < ref_or_null < <span class="hljs-keyword">ref < eq_ref < sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>/<span class="hljs-keyword">const
ALL 全表扫描,对于数据表从头到尾找一遍
<span class="hljs-keyword">select * <span class="hljs-keyword">from tb1;
特别的:如果有limit限制,则找到之后就不在继续向下扫描
<span class="hljs-keyword">select * <span class="hljs-keyword">from tb1 <span class="hljs-keyword">where email = <span class="hljs-string">'seven@live.com'
<span class="hljs-keyword">select * <span class="hljs-keyword">from tb1 <span class="hljs-keyword">where email = <span class="hljs-string">'seven@live.com' limit <span class="hljs-number">1;
虽然上述两个语句都会进行全表扫描,第二句使用了limit,则找到<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>后就不再继续扫描。
INDEX 全索引扫描,对索引从头到尾找一遍
<span class="hljs-keyword">select nid <span class="hljs-keyword">from tb1;
RANGE 对索引列进行范围查找
<span class="hljs-keyword">select * <span class="hljs-keyword">from tb1 <span class="hljs-keyword">where name < <span class="hljs-string">'alex';
PS:
between and
<span class="hljs-keyword">in
> >= < <= 操作
注意:!= 和 > 符号
INDEX_MERGE 合并索引,使用多个单列索引<a href="https://www.jb51.cc/tag/sousuo/" target="_blank" class="keywords">搜索</a>
<span class="hljs-keyword">select * <span class="hljs-keyword">from tb1 <span class="hljs-keyword">where name = <span class="hljs-string">'alex' <span class="hljs-function">or nid <span class="hljs-title">in (<span class="hljs-p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>ms"><span class="hljs-number">11,<span class="hljs-number">22,<span class="hljs-number">33);
REF 根据索引查找<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>或多个值
<span class="hljs-keyword">select * <span class="hljs-keyword">from tb1 <span class="hljs-keyword">where name = <span class="hljs-string">'seven';
EQ_REF 连接时使用primary key 或 unique类型
<span class="hljs-keyword">select tb2.nid,tb1.name <span class="hljs-keyword">from tb2 left <span class="hljs-keyword">join tb1 on tb2.nid = tb1.nid;
CONST 常量
表最多有<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>匹配行,因为仅有一行,<a href="https://www.jb51.cc/tag/zaizhe/" target="_blank" class="keywords">在这</a>行的列值可被优化器剩余部分认为是常数,<span class="hljs-keyword">const表很快,因为它们只读取一次。
<span class="hljs-keyword">select nid <span class="hljs-keyword">from tb1 <span class="hljs-keyword">where nid = <span class="hljs-number">2 ;
SY<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a> 系统
表仅有一行(=系统表)。这是<span class="hljs-keyword">const联接类型的<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>特例。
<span class="hljs-keyword">select * <span class="hljs-keyword">from (<span class="hljs-keyword">select nid <span class="hljs-keyword">from tb1 <span class="hljs-keyword">where nid = <span class="hljs-number">1) <span class="hljs-keyword">as A;
possible_keys
可能使用的索引
key
真实使用的
key_len
<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>中使用索引字节长度
rows
<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>估计为了找到所需的行而要读取的行数 ------ 只是预估值
extra
该列包含<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a><a href="https://www.jb51.cc/tag/jiejue/" target="_blank" class="keywords">解决</a><a href="https://www.jb51.cc/tag/chaxun/" target="_blank" class="keywords">查询</a>的详细信息
“Using index”
此值表示<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>将使用覆盖索引,以避免访问表。不要把覆盖索引和index访问类型弄混了。
“Using <span class="hljs-keyword">where”
这意味着<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>服务器将在存储引擎检索行后再进行过滤,许多<span class="hljs-keyword">where条件里涉及索引中的列,当(并且如果)它读取索引时,就能被存储引擎检验,因此不是所有带<span class="hljs-keyword">where子句的<a href="https://www.jb51.cc/tag/chaxun/" target="_blank" class="keywords">查询</a>都会<a href="https://www.jb51.cc/tag/xianshi/" target="_blank" class="keywords">显示</a>“Using <span class="hljs-keyword">where”。有时“Using <span class="hljs-keyword">where”的出现就是<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>暗示:<a href="https://www.jb51.cc/tag/chaxun/" target="_blank" class="keywords">查询</a>可受益于不同的索引。
“Using temporary”
这意味着<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>在对<a href="https://www.jb51.cc/tag/chaxun/" target="_blank" class="keywords">查询</a>结果排序时会使用<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>临时表。
“Using filesort”
这意味着<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>会对结果使用<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>外部索引排序,而不是按索引次序从表里读取行。<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>有两种<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>排序算法,这两种排序方式都可以在内存或者磁盘上完成,explain不会告诉你<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>将使用哪一种<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>排序,也不会告诉你排序会在内存里还是磁盘上完成。
“<span class="hljs-function">Range <span class="hljs-keyword">checked <span class="hljs-keyword">for each <span class="hljs-title">record(<span class="hljs-p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>ms">index map: N)”
这个意味着没有好用的索引,新的索引将在联接的每一行上重新估算,N是<a href="https://www.jb51.cc/tag/xianshi/" target="_blank" class="keywords">显示</a>在possible_keys列中索引的位图,并且是冗余的。</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
慢日志查询
rush: sql; gutter: true; first-line: 1 hljs ini">teral">OFF 是否开启慢日志记录
文件
teral">OFF 为使用索引的搜索是否记录
注:查看当前配置信息: show variables like ‘%query%’ 修改当前配置: set global 变量名 = 值
b、查看MysqL慢日志
MysqLdumpslow -s at -a /usr/local/var/MysqL/MacBook-Pro-3-slow.log
参数详解
rush: sql; gutter: true; first-line: 1 hljs">"""
-v 版本
-d 调试模式
-s ORDER 排序方式
what to sort by (al,at,ar,c,l,r,t),'at' is default
al: average <span class="hljs-keyword">lock <span class="hljs-keyword">time
ar: average <span class="hljs-keyword">rows sent
<span class="hljs-keyword">at: average <span class="hljs-keyword">query <span class="hljs-keyword">time
c: <span class="hljs-keyword">count
l: <span class="hljs-keyword">lock <span class="hljs-keyword">time
r: <span class="hljs-keyword">rows sent
t: <span class="hljs-keyword">query <span class="hljs-keyword">time
-r 反转顺序,默认文件倒序拍。<span class="hljs-keyword">reverse the <span class="hljs-keyword">sort <span class="hljs-keyword">order (largest <span class="hljs-keyword">last instead <span class="hljs-keyword">of <span class="hljs-keyword">first)
-t <span class="hljs-keyword">NUM 显示前N条just <span class="hljs-keyword">show the top n queries
-a 不要将<span class="hljs-keyword">sql中数字转换成N,字符串转换成S。don<span class="hljs-string">'t abstract all numbers to N and strings to 'S<span class="hljs-string">'
-n NUM abstract numbers with at least n digits within names
-g PATTERN 正则匹配;grep: only consider stmts that include this string
-h HOSTNAME MysqL机器名或者IP;hostname of db server for -slow.log filename (can be wildcard),default is '<span class="hljs-string">',i.e. match all
-i NAME name of server instance (if using MysqL.server startup script)
-l 总时间中不减去锁定时间;don't subtract <span class="hljs-keyword">lock <span class="hljs-keyword">time <span class="hljs-keyword">from total <span class="hljs-keyword">time
<span class="hljs-string">"""
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。