一,索引管理

 索引分类:
普通索引INDEX:加速查找 唯一索引:
-主键索引PRIMARY KEY:加速查找+约束(不为空、不能重复)
-唯一索引UNIQUE:加速查找+约束(不能重复) 联合索引:
-PRIMARY KEY(id,name):联合主键索引
-UNIQUE(id,name):联合唯一索引
-INDEX(id,name):联合普通索引 1 创建索引
- 在创建表时就创建
create table s1(
id int,
name char(6),
age int,
email varchar(30),
index(id)
);
- 在创建表后创建
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 add primary key(id,name);联合主键索引
alter table s1 drop primary key;#删除主键索引 3 正确使用索引
select sql_no_cache * from s1 where email='xxx'; #命中索引,速度很快
select sql_no_cache * from s1 where email like '%old%'; #无法使用索引,速度依然很慢 4 存储过程,主要用来生成多数据,然后按照普通查询和索引查询对比一下查询的时间
#1. 准备表
create table s1(
id int,
name varchar(20),
gender char(6),
email varchar(50)
); #2. 创建存储过程,实现批量插入记录
delimiter $$ #声明存储过程的结束符号为$$
create procedure auto_insert1()
BEGIN
declare i int default 1;
while(i<3000000)do
insert into s1 values(i,concat('egon',i),'male',concat('egon',i,'@oldboy'));
set i=i+1;
end while;
END$$ #$$结束
delimiter ; #重新声明分号为结束符号 #3. 查看存储过程
show create procedure auto_insert1\G #4. 调用存储过程
call auto_insert1(); #5. 删除存储过程
drop procedure auto_insert1; #6. 显示数据库中所有存储的存储过程基本信息,包括所属数据库,存储过程名称,创建时间等
show procedure status

二,索引使用

 ** 索引必须是一个明确的值才能体现其查询速度,例如where id=30521,如果是范围操作(大于,小于,between),就是还是需要循环判断,索引就不起作用

 1 加索引提速
无索引
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)
** 索引必须是一个能查询明确的值才能体现其查询速度, 2 范围
#范围小的话,索引有用
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)
** 范围小可以体现索引的作用,大范围还是要逐个循环 3 区分度低的字段不能加索引
有索引
mysql> create index b on s1(name)
-> ;
Query OK, 0 rows affected (3.21 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> select count(*) from s1 where name='xxx';
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec) mysql> select count(*) from s1 where name='egon';
+----------+
| count(*) |
+----------+
| 299999 |
+----------+
1 row in set (0.19 sec)
** 表内name都是egon这个值,所以有30万个egon,都是要一行一行去匹配 mysql> select count(*) from s1 where name='egon' and age=123123123123123;
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.45 sec) mysql> select count(*) from s1 where name='dfsdfdsfdfdsfdsfdsf' and age=123123123123123;
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
** 同上面结论 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) mysql> select count(*) from s1 where name='egon' and age=10;
+----------+
| count(*) |
+----------+
| 299999 |
+----------+
1 row in set (0.35 sec)
** 之所以加速,因为and是同时,也就是age右边可以瞬间定位,再比对nmae mysql> select count(*) from s1 where name='egon' and age=10;
+----------+
| count(*) |
+----------+
| 999 |
+----------+
1 row in set (0.00 sec)
age=10 区分度高 mysql> select count(*) from s1 where name='egon' and agw>50;
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.47 sec) mysql> select count(*) from s1 where name='egon' and age>100 and age < 600;
+----------+
| count(*) |
+----------+
| 999 |
+----------+
1 row in set (0.00 sec)
** 区分度高原则,范围原则小,差异就小 4 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)
** 区分度高原则,范围原则小,差异就小,email最高 5 增加联合索引,关于范围查询的字段要放到后面
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> 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) 6. 联合索引,最左前缀匹配,多用于and
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)
** 最左匹配,如果没有Id就会有时间得到结果 6.索引列不能参与计算,参与计算,所以就没有意义,保持列“干净”
mysql> select count(*) from s1 where id*3;
+----------+
| count(*) |
+----------+
| 299999 |
+----------+
1 row in set (0.16 sec) ** 用不上索引
like 函数 or 类型不一致 != > < order by 类型不一致
mysql> select count(*) from s1 where id='';
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set mysql> select count(*) from s1 where id=3000;
+----------+
| count(*) |
+----------+(0.11 sec)
| 1 |
+----------+
1 row in set (0.00 sec) order by 模糊
select id from s1 order by id; or 只有id是索引,必须是左右2边单独有索引才能提速,叫做索引合并
mysql> select count(*) from s1 where id=1000 or email='xxx';
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.13 sec) 慢查询
在Mysqld下面配置
slow-query-log=1 开
slow-query-log-file=slow.log 存放位置
long_query_time=3 超时限制3毫秒就会被记录 在cmd里面
set global slow-query-log=ON;
show variables like '%query%';
set session long_query_time=3;

python开发mysql:索引的更多相关文章

  1. python 3 mysql 索引原理与慢查询优化

    python 3 mysql 索引原理与慢查询优化 一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最 ...

  2. python开发mysql:视图、触发器、事务、存储过程、函数

    一 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的 ...

  3. python开发mysql:mysql安装(windows)&密码找回&存储引擎简介&库表的增删改查

    一,mysql安装 下载地址 https://dev.mysql.com/downloads/file/?id=471342 解压后,将目录C:\mysql-5.7.19-winx64\bin添加到计 ...

  4. python开发mysql:Pymysql模块

    pymysql模块的使用 #1 基本使用 # import pymysql # conn=pymysql.connect(host='localhost',user='root',password=' ...

  5. python开发mysql:单表查询&多表查询

    一 单表查询,以下是表内容 一 having 过滤 1.1 having和where select * from emp where id > 15; 解析过程;from > where ...

  6. python开发mysql:表关系&单表简单查询

    一 一对多,多对一 1.1 建立多对一 ,一对多的关系需要注意 先建立被关联的表,被关联的字段必须保证时唯一的 在创建关联的表,关联的字段一定是可以重复的 1.2 示例: 出版社 多对一,多个老师可能 ...

  7. python开发mysql:mysql数据类型&约束条件

    一 整形 只有Int类型跟存储没有关系,显示的是宽度,其他类型都是限制 整形类型:[(m)][unsigned][zerofill] 作用:存储年龄,等级,id,各种号码 m,代表显示宽度 默认11 ...

  8. python与MySQL数据库

    python与MySQL数据库 慕课网连接 我使用的软件:python2.7 + MySQL+ Navicat for MySQL + Atom 注意:你的数据库表格类型的引擎为:InnoDB :字符 ...

  9. Python操作MySQL以及数据库索引

    目录 python操作MySQL 安装 使用 SQL注入问题 MySQL的索引 为什么使用索引 索引的种类 主键索引 唯一索引 普通索引 索引优缺点 不会命中索引的情况 explain 索引覆盖 My ...

随机推荐

  1. extundelete实现Linux下文件/文件夹数据恢复!

    我用的是Centos系统,在安装extundelete之前需要安装e2fsprogs,e2fsprogs-libs,e2fsprogs-devel. 这里用:yum install e2fsprogs ...

  2. java正则表达式(基础篇)

    1.数量表达 {n} :出现n次 {m,n}:最少出现m次,最多出现n次 *:表示出现>=0次,相当于{0,} +:表示出现>=1次,相当于{1,} ?:表示出现1次或0次 |:左右两边正 ...

  3. 7z压缩gopath的src的批处理

    7zGoPath.bat @echo off pushd "%~dp0" :config for /f "delims=" %%t in ('powershel ...

  4. [转]Python读写文件

    1.open使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('thefile.txt ...

  5. python协程函数应用 列表生成式 生成器表达式

    协程函数应用 列表生成式 生成器表达式   一.知识点整理: 1.可迭代的:对象下有_iter_方法的都是可迭代的对象 迭代器:对象._iter_()得到的结果就是迭代器 迭代器的特性: 迭代器._n ...

  6. ubuntu 安装python3.7 以及安装pip3 出现Command '('lsb_release', '-a')' returned non-zero exit status 1问题解决

    最近因为电脑重装,东西全没了,总计一下最近重装环境的过程. 如果没有安装包,请下载: wget http://www.python.org/ftp/python/3.7.0/Python-3.7.0. ...

  7. 30-THREE.JS 圆环

    <!DOCTYPE html> <html> <head> <title>Example 05.03 - Basic 2D geometries - R ...

  8. opencv:鼠标操作

    示例程序: #include <opencv.hpp> using namespace cv; #define WINDOW_NAME "程序窗口" // ------ ...

  9. Project Euler 126 - Cuboid layers

    这题先是推公式… 狂用不完全归纳+二次回归,最后推出这么一个奇怪的公式 \[f(t,x,y,z)=4(t-1)(x+y+z+t-2)+2(xy+yz+xz)\] 表示长宽高为\(x\).\(y\).\ ...

  10. idea远程debug调试设置

    1.idea设置 1.1 加入Tomcat Server选择Remote 1.2:设置对应的參数 xxx.xxx.152.67:8080为远程Tomcatserver的IP地址和port,这里能够设置 ...