python-day45--mysql索引

一 、介绍

为何要有索引?

一些复杂的查询操作,对查询语句的优化显然是重中之重。说起加速查询,就不得不提到索引了。

什么是索引?

索引在MySQL中也叫做“键”,是存储引擎用于快速找到记录的一种数据结构。索引对于良好的性能
非常关键,尤其是当表中的数据量越来越大时,索引对于性能的影响愈发重要。
索引优化应该是对查询性能优化最有效的手段了。索引能够轻易将查询性能提高好几个数量级。
索引相当于字典的音序表,如果要查某个字,如果不使用音序表,则需要从几百页中逐页去查。

二 、索引的原理

  一 索引原理

  索引的目的在于提高查询效率,与我们查阅图书所用的目录是一个道理:先定位到章,然后定位到该章下的一个小节,然后找到页数。

  本质都是:通过不断地缩小想要获取数据的范围来筛选出最终想要的结果,同时把随机的事件变成顺序的事件,也就是说,有了这种索引机制,我们可以总是用同一种查找方式来          锁定数据。

  二 磁盘IO与预读

  考虑到磁盘IO是非常高昂的操作,计算机操作系统做了一些优化,当一次IO时,不光把当前磁盘地址的数据,而是把相邻的数据也都读取到内存缓冲区内,因为局部预读性原理告          诉我们,当计算机访问一个地址的数据的时候,与其相邻的数据也会很快被访问到。每一次IO读取的数据我们称之为一页(page)。具体一页有多大数据跟操作系统有关,一般为4k          或8k,也就是我们读取一页内的数据时候,实际上才发生了一次IO,这个理论对于索引的数据结构设计非常有帮助。

  三 索引的数据结构: btree   (层级越小,速度越快)

  进行一次io 就会往内存中读一个磁盘块(浅蓝色),它内部包含好多数据项--预读原理(蓝色块),黄色表示指针

  

  

  ###b+树的查找过程
  如图所示,如果要查找数据项29,那么首先会把磁盘块1由磁盘加载到内存,此时发生一次IO,在内存中用二分查找确定29在17和35之间,锁定磁盘块1的P2指针,内存时间因为          非常短(相比磁盘的IO)可以忽略不计,通过磁盘块1的P2指针的磁盘地址把磁盘块3由磁盘加载到内存,发生第二次IO,29在26和30之间,锁定磁盘块3的P2指针,通过指针加载        磁盘块8到内存,发生第三次IO,同时内存中做二分查找找到29,结束查询,总计三次IO。真实的情况是,3层的b+树可以表示上百万的数据,如果上百万的数据查找只需要三次            IO,性能提高将是巨大的,如果没有索引,每个数据项都要发生一次IO,那么总共需要百万次的IO,显然成本非常非常高。

  ###b+树性质
  1.被加索引的字段的数据量要尽量的小:通过上面的分析,我们知道IO次数取决于b+数的高度h,假设当前数据表的数据为N,每个磁盘块的数据项的数量是m,则有h=㏒(m+1)            N,当数据量N一定的情况下,m越大,h越小;而m = 磁盘块的大小 / 数据项的大小,磁盘块的大小也就是一个数据页的大小,是固定的,如果数据项占的空间越小,数据项的数          量越多,树的高度越低。这就是为什么每个数据项,即索引字段要尽量的小,比如int占4字节,要比bigint8字节少一半。这也是为什么b+树要求把真实的数据放到叶子节点而不是          内层节点,一旦放到内层节点,磁盘块的数据项会大幅度下降,导致树增高。当数据项等于1时将会退化成线性表。

  个人理解:每个硬盘块的容量是固定的,如果每个数据项数据量过大,那么硬盘块所装下的数据块就少,导致btree模型的层数增多,增加io阻塞,所以每个被加索引的字段的数据          量要尽量的小一些
  2.索引的最左匹配特性:当b+树的数据项是复合的数据结构,比如(name,age,sex)的时候,b+数是按照从左到右的顺序来建立搜索树的,比如当(张三,20,F)这样的数据来检索的时          候,b+树会优先比较name来确定下一步的所搜方向,如果name相同再依次比较age和sex,最后得到检索的数据;但当(20,F)这样的没有name的数据来的时候,b+树就不知道下          一步该查哪个节点,因为建立搜索树的时候name就是第一个比较因子,必须要先根据name来搜索才能知道下一步去哪里查询。比如当(张三,F)这样的数据来检索时,b+树可以用          name来指定搜索方向,但下一个字段age的缺失,所以只能把名字等于张三的数据都找到,然后再匹配性别是F的数据了, 这个是非常重要的性质,即索引的最左匹配特性。

三 MySQL索引管理

  一 功能

#1. 索引的功能就是加速查找
#2. mysql中的primary key,unique,联合唯一也都是索引,这些索引除了加速查找以外,还有约束的功能

  二 MySQL的索引分类

普通索引INDEX:加速查找

唯一索引:
    -主键索引PRIMARY KEY:加速查找+约束(不为空、不能重复)
    -唯一索引UNIQUE:加速查找+约束(不能重复)

联合索引:
    -PRIMARY KEY(id,name):联合主键索引
    -UNIQUE(id,name):联合唯一索引
    -INDEX(id,name):联合普通索引
举个例子来说,比如你在为某商场做一个会员卡的系统。

这个系统有一个会员表
有下列字段:
会员编号 INT
会员姓名 VARCHAR(10)
会员身份证号码 VARCHAR(18)
会员电话 VARCHAR(10)
会员住址 VARCHAR(50)
会员备注信息 TEXT

那么这个 会员编号,作为主键,使用 PRIMARY
会员姓名 如果要建索引的话,那么就是普通的 INDEX
会员身份证号码 如果要建索引的话,那么可以选择 UNIQUE (唯一的,不允许重复)

#除此之外还有全文索引,即FULLTEXT
会员备注信息 , 如果需要建索引的话,可以选择全文搜索。
用于搜索很长一篇文章的时候,效果最好。
用在比较短的文本,如果就一两行字的,普通的 INDEX 也可以。
但其实对于全文搜索,我们并不会使用MySQL自带的该索引,而是会选择第三方软件如Sphinx,专门来做全文搜索。

#其他的如空间索引SPATIAL,了解即可,几乎不用
各个索引的应用场景

  三 索引的两大类型hash与btree

#我们可以在创建上述索引的时候,为其指定索引类型,分两类
hash类型的索引:查询单条快,范围查询慢
btree类型的索引:b+树,层数越多,数据量指数级增长(我们就用它,因为innodb默认支持它)

#不同的存储引擎支持的索引类型也不一样
InnoDB 支持事务,支持行级别锁定,支持 B-tree、Full-text 等索引,不支持 Hash 索引;
MyISAM 不支持事务,支持表级别锁定,支持 B-tree、Full-text 等索引,不支持 Hash 索引;
Memory 不支持事务,支持表级别锁定,支持 B-tree、Hash 等索引,不支持 Full-text 索引;
NDB 支持事务,支持行级别锁定,支持 Hash 索引,不支持 B-tree、Full-text 等索引;
Archive 不支持事务,支持表级别锁定,不支持 B-tree、Hash、Full-text 等索引;

  四 创建/删除索引的语法

 
  

1 创建索引
    - 在创建表时就创建
        create table s1(
        id int,
        name char(6),
        age int,
        email varchar(30),
        index(id)           #不可以直接在字段后直接加 如:id int index
        );
    - 在创建表后创建
        create index name on s1(name);#添加普通索引
        create unique index age on s1(age);#添加唯一索引
        alter table s1 add primary key(id);#添加主键索引
        create index name on s1(id,name);#添加联合普通索引

 
  

2 删除索引
    drop index id on s1;
    drop index name on s1;
    alter table s1 drop primary key;#删除主键索引

 
 

 

四 、覆盖索引与索引合并

 

#覆盖索引:
    - 在索引文件中直接获取数据
    http://blog.itpub.net/22664653/viewspace-774667/

#分析
select * from s1 where id=123;
该sql命中了索引,但未覆盖索引。
利用id=123到索引的数据结构中定位到该id在硬盘中的位置,或者说再数据表中的位置。
但是我们select的字段为*,除了id以外还需要其他字段,这就意味着,我们通过索引结构取到id还不够,还需要利用该id再去找到该id所在行的其他字段值,这是需要时间的,很明显,如果我们只select id,就减去了这份苦恼,如下
select id from s1 where id=123;
这条就是覆盖索引了,命中索引,且从索引的数据结构直接就取到了id在硬盘的地址,速度很快

 

#索引合并:把多个单列索引合并使用

#分析:
组合索引能做到的事情,我们都可以用索引合并去解决,比如
create index ne on s1(name,email);#组合索引
我们完全可以单独为name和email创建索引

组合索引可以命中:
select * from s1 where name='egon' ;
select * from s1 where name='egon' and email='adf';

索引合并可以命中:
select * from s1 where name='egon' ;
select * from s1 where email='adf';
select * from s1 where name='egon' and email='adf';

乍一看好像索引合并更好了:可以命中更多的情况,但其实要分情况去看,如果是name='egon' and email='adf',那么组合索引的效率要高于索引合并,如果是单条件查,那么还是用索引合并比较合理

 

五、正确使用索引

加索引

#1. 一定是为搜索条件的字段创建索引,比如select * from t1 where age > 5;就需要为age加上索引

#2. 在表中已经有大量数据的情况下,建索引会很慢,且占用硬盘空间,插入删除更新都很慢,只有查询快
比如create index idx on s1(id);会扫描表中所有的数据,然后以id为数据项,创建索引结构,存放于硬盘的表中。
建完以后,再查询就会很快了

#3. 需要注意的是:innodb表的索引会存放于s1.ibd文件中,而myisam表的索引则会有单独的索引文件table1.MYI

 

1 加索引提速: 大范围类的查找条件 加索引没有意义   > < != like between and 
mysql> select count(*) from s1 where id=1000;
+----------+
| count(*) |
+----------+
|        1 |
+----------+
1 row in set (0.12 sec)

mysql> select count(*) from s1 where id>1000;
+----------+
| count(*) |
+----------+
|   298999 |
+----------+
1 row in set (0.12 sec)

mysql> create index a on s1(id)
    -> ;
Query OK, 0 rows affected (3.21 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select count(*) from s1 where id=1000;
+----------+
| count(*) |
+----------+
|        1 |
+----------+
1 row in set (0.00 sec)

mysql> select count(*) from s1 where id>1000;
+----------+
| count(*) |
+----------+
|   298999 |
+----------+
1 row in set (0.12 sec)

mysql> select count(*) from s1 where id>1000 and id < 2000;
+----------+
| count(*) |
+----------+
|      999 |
+----------+
1 row in set (0.00 sec)

mysql> select count(*) from s1 where id>1000 and id < 300000;
+----------+
| count(*) |
+----------+
|   298999 |
+----------+
1 row in set (0.13 sec)




特别的:如果是主键,则还是会走索引
        select * from tb1 where nid != 123

特别的:如果是主键或索引是整数类型,则还是会走索引
        select * from tb1 where nid > 123
        select * from tb1 where num > 123
        
        
        
2 区分度低(多重复)的字段不能加索引      

(从左到右找到区分度最高的字段来判断)
mysql> select count(*) from s1 where name='xxx';
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)     #因为名字里没有xxx

mysql> select count(*) from s1 where name='egon';
+----------+
| count(*) |
+----------+
|   299999 |
+----------+
1 row in set (0.19 sec)     #相当于全表扫描


mysql> select count(*) from s1 where name='egon' and age=123123123123123;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.45 sec)    #相当于全表扫描2遍

mysql> create index c on s1(age);
Query OK, 0 rows affected (3.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select count(*) from s1 where name='egon' and age=123123123123123;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)       #name不能区分出来,它从左到右 找到能区分出来的条件

mysql> select count(*) from s1 where name='egon' and age=10;
+----------+
| count(*) |
+----------+
|   299999 |
+----------+
1 row in set (0.35 sec)      #因为age字段区分度低


mysql> select count(*) from s1 where name='egon' and age=10 and id>3000 and id < 4000;
+----------+
| count(*) |
+----------+
|      999 |
+----------+
1 row in set (0.00 sec)       #name和age都不能区分出来,它从左到右 找到能区分出来的条件


mysql> select count(*) from s1 where name='egon' and age=10 and id>3000 and email='xxxx';
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.47 sec)                 #因为要全表查找一遍email=xxxx
                ###从左到右找到区分度最高的字段来判断

mysql> create index d on s1(email);
Query OK, 0 rows affected (4.83 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select count(*) from s1 where name='egon' and age=10 and id>3000 and email='xxxx';
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

mysql> drop index a on s1;
Query OK, 0 rows affected (0.10 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> drop index b on s1;
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> drop index c on s1;
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc s1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | char(20)    | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
| email | varchar(30) | YES  | MUL | NULL    |       |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> select count(*) from s1 where name='egon' and age=10 and id>3000 and email='xxxx';
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)



3 增加联合索引,关于范围查询的字段要放到后面   
 select count(*) from s1 where name='egon' and age=10 and id>3000 and email='xxxx';
index(name,email,age,id)

 select count(*) from s1 where name='egon' and age> 10 and id=3000 and email='xxxx';
index(name,email,id,age)

 select count(*) from s1 where name like 'egon' and age= 10 and id=3000 and email='xxxx';
index(email,id,age,name)


mysql> desc s1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | char(20)    | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
| email | varchar(30) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)


mysql> select count(*) from s1 where name='egon' and age=10 and id>3000 and email='xxxx';
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.18 sec)

mysql> create index xxx on s1(age,email,name,id);
Query OK, 0 rows affected (6.89 sec)
Records: 0  Duplicates: 0  Warnings: 0


mysql> select count(*) from s1 where name='egon' and age=10 and id>3000 and email='xxxx';
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)




4. 最左前缀匹配
index(id,age,email,name)
#条件中一定要出现id
id
id age
id email
id name

email #不行
mysql> select count(*) from s1 where id=3000;
+----------+
| count(*) |
+----------+
|        1 |
+----------+
1 row in set (0.11 sec)

mysql> create index xxx on s1(id,name,age,email);
Query OK, 0 rows affected (6.44 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql>  select count(*) from s1 where id=3000;
+----------+
| count(*) |
+----------+
|        1 |
+----------+
1 row in set (0.00 sec)

mysql>  select count(*) from s1 where name='egon';
+----------+
| count(*) |
+----------+
|   299999 |
+----------+
1 row in set (0.16 sec)

mysql>  select count(*) from s1 where email='egon3333@oldboy.com';
+----------+
| count(*) |
+----------+
|        1 |
+----------+
1 row in set (0.15 sec)

mysql>  select count(*) from s1 where id=1000 and email='egon3333@oldboy.com';
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

mysql>  select count(*) from s1 where email='egon3333@oldboy.com' and id=3000;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)







5.索引列不能参与计算,保持列“干净”

mysql> select count(*) from s1 where id=3000;
+----------+
| count(*) |
+----------+
|        1 |
+----------+
1 row in set (0.14 sec)

mysql> create index a on s1(id);
Query OK, 0 rows affected (3.44 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql>  select count(*) from s1 where id=3000;
+----------+
| count(*) |
+----------+
|        1 |
+----------+
1 row in set (0.00 sec)

mysql>  select count(*) from s1 where id*3=3000;
+----------+
| count(*) |
+----------+
|   0 |
+----------+
1 row in set (0.12 sec)   # 因为每次来值的时候都要计算一次


mysql>  select count(*) from s1 where reverse(id)=3000;
+----------+
| count(*) |
+----------+
|   0 |
+----------+
1 row in set (0.13 sec)     #每次来值的时候都要反转


mysql> select count(*) from s1 where id=3000/3;
+----------+
| count(*) |
+----------+
|        1 |
+----------+
1 row in set (0.00 sec)    #可以在后面计算,但是不可以在id内计算








6.类型不一致 无法用到索引

mysql> select count(*) from s1 where email='xxxxx';
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec) 



mysql> select count(*) from s1 where email=333333;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set 65535 warings(0.21 sec)    #因为类型不一致,索引不起作用




7.#排序条件为索引,则select字段必须也是索引字段,否则无法命中
order by

select name from s1 order by email desc;
当根据索引排序时候,select查询的字段如果不是索引,则不走索引
select email from s1 order by email desc;

特别的:如果对主键排序,则还是走索引:
    select * from tb1 order by nid desc;

    
    
8. or

mysql> select count(*) from s1 where id=3000;
+----------+
| count(*) |
+----------+
|        1 |
+----------+
1 row in set (0.00 sec)   #前提是id 已经有索引了


mysql> select count(*) from s1 where id=3000 or email='xxxxx';
+----------+
| count(*) |
+----------+
|        1 |
+----------+
1 row in set (0.13 sec)     


create index d on s1(email);
Query OK, 0 rows affected (4.77 sec)
Records: 0  Duplicates: 0  Warnings: 0


mysql> select count(*) from s1 where id=3000 or email='xxxxx';
+----------+
| count(*) |
+----------+
|        1 |
+----------+
1 row in set (0.00 sec)  


联合索引适用于and  但是不使用or     索引合并适用于or 也适用于and

 

六、索引总结:

一 、并不是说我们创建了索引就一定会加快查询速度,如下索引未命中
    1. 大范围类的查找条件 加索引没有意义   >  <  !=  like  between and 
        特别的:如果是主键,则还是会走索引
        select * from tb1 where nid != 123

        特别的:如果是主键或索引是整数类型,则还是会走索引
        select * from tb1 where nid > 123
        select * from tb1 where num > 123

    2. 使用函数
        select * from tb1 where reverse(id) = 1000;
    3.联合索引适用于and  但是不使用or     索引合并适用于or 也适用于and
    4.类型不一致
        select count(*) from s1 where email=333333;   #应该是字符串类型
    5.排序条件为索引,则select字段必须也是索引字段,否则无法命中
        select name from s1 order by email desc;
        当根据索引排序时候,select查询的字段如果不是索引,则不走索引
    6.组合索引最左前缀
        如果组合索引为:(name,email)
        name and email       -- 使用索引
        email and name       -- 使用索引
        name                 -- 使用索引
        email                -- 不使用索引
    7.count(1)或count(列)代替count(*)在mysql中没有差别了
    
二、其他注意事项

- 避免使用select *
- count(1)或count(列) 代替 count(*)   #在mysql中可以忽略此条
- 创建表时尽量时 char 代替 varchar     #最左前缀匹配 -- char 是定长,而varchar是变长
- 表的字段顺序固定长度的字段优先        #最左前缀匹配
- 组合索引代替多个单列索引(经常使用多个条件查询时)    
- 尽量使用短索引                       #字段的数据量尽量小
- 使用连接(JOIN)来代替子查询(Sub-Queries)
- 连表时注意条件类型需一致
- 字段的区分度低 不适合建索引,例:性别不适合

三 若想利用索引达到预想的提高查询速度的效果,我们在添加索引时,必须遵循以下原则
 在sql语句查找条件中找到关于范围的条件时,在创建索引时要往后放

  #1.最左前缀匹配原则,非常重要的原则,
  create index ix_name_email on s1(name,email,)
  - 最左前缀匹配:必须按照从左到右的顺序匹配
  select * from s1 where name='egon'; #可以
  select * from s1 where name='egon' and email='asdf'; #可以
  select * from s1 where email='alex@oldboy.com'; #不可以
  mysql会一直向右匹配直到遇到范围查询(>、<、between、like)就停止匹配,比如a = 1 and b = 2 and c           > 3 and d = 4 如果建立(a,b,c,d)顺序的索引,d是用不到索引的,如果建立(a,b,d,c)的索引则都可以用      到,a,b,d的顺序可以任意调整。

  #2.=和in可以乱序,比如a = 1 and b = 2 and c = 3 建立(a,b,c)索引可以任意顺序,mysql的查询优化      器会帮你优化成索引可以识别的形式

  #3.尽量选择区分度高的列作为索引,区分度的公式是count(distinct col)/count(*),表示字段不重复的比      例,比例越大我们扫描的记录数越少,唯一键的区分度是1,而一些状态、性别字段可能在大数据面前区分度就是0,      那可能有人会问,这个比例有什么经验值吗?使用场景不同,这个值也很难确定,一般需要join的字段我们都要求      是0.1以上,即平均1条扫描10条记录

  #4.索引列不能参与计算,保持列“干净”,比如from_unixtime(create_time) = ’2014-05-29’就不能使用    到索引,原因很简单,b+树中存的都是数据表中的字段值,但进行检索时,需要把所有元素都应用函数才能比较,显      然成本太大。所以语句应该写成create_time = unix_timestamp(’2014-05-29’);

 

七、查询优化神器-explain

关于explain命令相信大家并不陌生,具体用法和字段含义可以参考官网explain-output,这里需要强调rows是核心指标,绝大部分rows小的语句执行一定很快(有例外,下面会讲到)。所以优化语句基本上都是在优化rows。

执行计划:让mysql预估执行操作(一般正确)
    all < index < range < index_merge < ref_or_null < ref < eq_ref < system/const
    id,email
    
    慢:
        select * from userinfo3 where name='alex'
        
        explain select * from userinfo3 where name='alex'
        type: ALL(全表扫描)
            select * from userinfo3 limit 1;
    快:
        select * from userinfo3 where email='alex'
        type: const(走索引)

http://blog.itpub.net/29773961/viewspace-1767044/

 

八、慢查询优化的基本步骤

0.先运行看看是否真的很慢,注意设置SQL_NO_CACHE
1.where条件单表查,锁定最小返回记录表。这句话的意思是把查询语句的where都应用到表中返回的记录数最小的表开始查起,单表每个字段分别查询,看哪个字段的区分度最高
2.explain查看执行计划,是否与1预期一致(从锁定记录较少的表开始查询)
3.order by limit 形式的sql语句让排序的表优先查
4.了解业务方使用场景
5.加索引时参照建索引的几大原则
6.观察结果,不符合预期继续从0分析

九、慢日志管理

慢日志
            - 执行时间 > 10
            - 未命中索引
            - 日志文件路径
            
        配置:
            - 内存
                show variables like '%query%';
                show variables like '%queries%';
                set global 变量名 =- 配置文件
                mysqld --defaults-file='E:\wupeiqi\mysql-5.7.16-winx64\mysql-5.7.16-winx64\my-default.ini'
                
                my.conf内容:
                    slow_query_log = ON
                    slow_query_log_file = D:/....
                    
                注意:修改配置文件之后,需要重启服务
MySQL日志管理
========================================================
错误日志: 记录 MySQL 服务器启动、关闭及运行错误等信息
二进制日志: 又称binlog日志,以二进制文件的方式记录数据库中除 SELECT 以外的操作
查询日志: 记录查询的信息
慢查询日志: 记录执行时间超过指定时间的操作
中继日志: 备库将主库的二进制日志复制到自己的中继日志中,从而在本地进行重放
通用日志: 审计哪个账号、在哪个时段、做了哪些事件
事务日志或称redo日志: 记录Innodb事务相关的如事务执行时间、检查点等
========================================================
一、bin-log
1. 启用
# vim /etc/my.cnf
[mysqld]
log-bin[=dir\[filename]]
# service mysqld restart
2. 暂停
//仅当前会话
SET SQL_LOG_BIN=0;
SET SQL_LOG_BIN=1;
3. 查看
查看全部:
# mysqlbinlog mysql.000002
按时间:
# mysqlbinlog mysql.000002 --start-datetime="2012-12-05 10:02:56"
# mysqlbinlog mysql.000002 --stop-datetime="2012-12-05 11:02:54"
# mysqlbinlog mysql.000002 --start-datetime="2012-12-05 10:02:56" --stop-datetime="2012-12-05 11:02:54" 

按字节数:
# mysqlbinlog mysql.000002 --start-position=260
# mysqlbinlog mysql.000002 --stop-position=260
# mysqlbinlog mysql.000002 --start-position=260 --stop-position=930
4. 截断bin-log(产生新的bin-log文件)
a. 重启mysql服务器
b. # mysql -uroot -p123 -e 'flush logs'
5. 删除bin-log文件
# mysql -uroot -p123 -e 'reset master' 


二、查询日志
启用通用查询日志
# vim /etc/my.cnf
[mysqld]
log[=dir\[filename]]
# service mysqld restart

三、慢查询日志
启用慢查询日志
# vim /etc/my.cnf
[mysqld]
log-slow-queries[=dir\[filename]]
long_query_time=n
# service mysqld restart
MySQL 5.6:
slow-query-log=1
slow-query-log-file=slow.log
long_query_time=3
查看慢查询日志
测试:BENCHMARK(count,expr)
SELECT BENCHMARK(50000000,2*3);
日志管理

 

转载于:https://www.cnblogs.com/liuwei0824/p/7512073.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值