一,索引管理

 索引分类:
普通索引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. activity启动模式之singleInstance

    activity启动模式之singleInstance 一.简介 Log.d("C2", getTaskId()+"");里面的getTaskId()可以获取a ...

  2. 习题-第7章Web自动化测试

    一.选择题 1.如果火狐浏览器不在默认安装路径,编写Selenium的设置代码,横线处应该填入(    ), System.setProperty(“webdriver.firefox._______ ...

  3. jQ通过cookie记住用户名

    总结要点: 一.封装的一个cookie函数: 二.获取input的value值:$("input[name=user]").val() 三.设置input的value值:$(&qu ...

  4. ARM的异常处理方式

    1.什么是异常? 正常工作之外的流程都叫异常 异常会打断正在执行的工作,并且一般我们希望异常处理完成后继续回来执行原来的工作 中断是异常的一种 2.异常向量表 所有的CPU都有异常向量表,这是CPU设 ...

  5. LeetCode OJ:Find Peak Element(寻找峰值元素)

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  6. 在Windows下为PHP5.6安装redis扩展

    Redis 安装 Window 下安装 下载地址:https://github.com/MSOpenTech/redis/releases. Redis 支持 32 位和 64 位.这个需要根据你系统 ...

  7. 《锋利的jQuery》读书笔记(DOM+事件)

    前阵子买了一批书,就从锋利的jQuery看起吧,书中一些太过常规以及没有强记必要性的操作就不记录了. 1.DOM加载后执行JS $(document).ready(function(){ //.... ...

  8. 使用 nvm 管理多版本 node

    首先,使用下面的命令来安装 nvm $ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | b ...

  9. iOS开发之谓词Predicate和对象数组的排序

    我们在开发中经常使用的Predicate谓词,主要是正则表达式的使用,今天给大家简单的讲讲怎样去使用谓词. 因为内容比较简单,所以直接上代码展示: NSMutableArray *people_arr ...

  10. Precision/Recall、ROC/AUC、AP/MAP等概念区分

    1. Precision和Recall Precision,准确率/查准率.Recall,召回率/查全率.这两个指标分别以两个角度衡量分类系统的准确率. 例如,有一个池塘,里面共有1000条鱼,含10 ...