一,索引管理

 索引分类:
普通索引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. GO学习笔记:函数defer

    Go语言中有种不错的设计,即延迟(defer)语句,你可以在函数中添加多个defer语句.当函数执行到最后时,这些defer语句会按照逆序执行,最后该函数返回.特别是当你在进行一些打开资源的操作时,遇 ...

  2. 分享知识-快乐自己:FastDFS详解

    在使用fdfs之前,需要对其有一定的了解,这篇文章作为准备篇,将针对fdfs的简介,功能性,使用场景等方面进行介绍 一):起源 淘宝网开放平台技术部资深架构师余庆先生首先回顾了自己在Yahoo工作时的 ...

  3. 四十八 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现搜索功能

    Django实现搜索功能 1.在Django配置搜索结果页的路由映射 """pachong URL Configuration The `urlpatterns` lis ...

  4. Ant Design

    https://ant.design/components/form-cn/    (Ant Design of React 中文网) 1.Ant Design of react (相关资料) htt ...

  5. 【spark】连接Hbase

    0.我们有这样一个表,表名为Student 1.在Hbase中创建一个表 表明为student,列族为info 2.插入数据 我们这里采用put来插入数据 格式如下   put  ‘表命’,‘行键’, ...

  6. 005——VUE中的v-text与v-html的使用

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. leetcode_sql_3,181,182,183

    181. Employees Earning More Than Their Managers The Employee table holds all employees including the ...

  8. 查找对应jar的maven包

    当原有项目换成maven项目时,往往不知道具体jar包在maven里叫什么.这边文章教你如何去找到自己想要的jar的maven包. 工具/原料   浏览器 方法/步骤   1 登录一下网站 http: ...

  9. sublime text 3设置浏览器快捷键

    一.设置默认浏览器 1,打开sublime 依次选择 tools > build system > new build system... 2,选择你喜欢的浏览器,右键 > 属性 把 ...

  10. Kubernetes安装部署演示介绍

    四.安装k8s 1.安装 使用的是k8s 1.2.4版本. 将kubernetes.tar.gz 上传主机,并解压. tar -xzvf kubernetes.tar.gz cd kubernetes ...