一,索引管理

 索引分类:
普通索引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. 在线演示demo

    *{display:none} 仿微博添加和删除动画 body{} input,button,select,textarea{outline:none;} .sdiv{width:400px;} .b ...

  2. 通过使用Netty实现RPC

    目标:通过使用Netty框架实现RPC(远程过程调用协议),技术储备为以后实现分布式服务框架做技术储备.在这里实现自定义协议主要实现远程方法调用. 技术分析: 1.通过Java的反射技术我们可以获取对 ...

  3. HyperV创建NAT网络

    创建NAT内部网络,使用PowerShell完成. https://docs.microsoft.com/zh-cn/virtualization/hyper-v-on-windows/user-gu ...

  4. 卸载MicrosoftBAF(删除C:\CommonFramework\instdata.dat)

    发现有个可疑文件夹 C:\CommonFramework ,而且还会不停的删除创建 C:\CommonFramework\instdata.dat 最后被发现这是必应的一个框架程序在捣鬼,在微软论坛里 ...

  5. saltstack笔记

    Saltstack类似于puppet salt的核心功能使用命令 发送到远程系统是并行的而不是串行的使用安全加密的协议使用最小最快的网络载荷提供简单的编程接口 Python编写,相当轻量级通讯层采用z ...

  6. lua基础---函数

    Lua的函数功能很强大,保留了C语言的一些基本的特性,但是也有C语言没有的特性,比如,lua可以在一个函数返回多个值,我们来看看下面这个案例: 解释运行: lua test5.lua --定义一个函数 ...

  7. ORM版,学生信息管理单表查询..

    mysql 建学生表及课程表 添加内容 view.py from django.shortcuts import render,HttpResponse,redirect from . import ...

  8. typedeifn typename

    1.类型说明typedef 类型说明的格式为: typedef  类型 定义名; 类型说明只定义了一个数据类型的新名字而不是定义一种新的数据类型.定义名表示这个类型的新名字. 例如: 用下面语句定义整 ...

  9. springboot 不同环境不同的配置

    前言 我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发.测试.生产等.其中每个环境的数据库地址.服务器端口等等配置都会不同,如果在为不同环境打包时都要频 ...

  10. gulp 流处理

    包含的内容:     穿插流     流合并     流队列     流筛选 1. 穿插流 // passthrough stream 把流传递给其他的 // use gulp.src(glob,{p ...