python开发mysql:索引
一,索引管理
索引分类:
普通索引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:索引的更多相关文章
- python 3 mysql 索引原理与慢查询优化
python 3 mysql 索引原理与慢查询优化 一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最 ...
- python开发mysql:视图、触发器、事务、存储过程、函数
一 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的 ...
- python开发mysql:mysql安装(windows)&密码找回&存储引擎简介&库表的增删改查
一,mysql安装 下载地址 https://dev.mysql.com/downloads/file/?id=471342 解压后,将目录C:\mysql-5.7.19-winx64\bin添加到计 ...
- python开发mysql:Pymysql模块
pymysql模块的使用 #1 基本使用 # import pymysql # conn=pymysql.connect(host='localhost',user='root',password=' ...
- python开发mysql:单表查询&多表查询
一 单表查询,以下是表内容 一 having 过滤 1.1 having和where select * from emp where id > 15; 解析过程;from > where ...
- python开发mysql:表关系&单表简单查询
一 一对多,多对一 1.1 建立多对一 ,一对多的关系需要注意 先建立被关联的表,被关联的字段必须保证时唯一的 在创建关联的表,关联的字段一定是可以重复的 1.2 示例: 出版社 多对一,多个老师可能 ...
- python开发mysql:mysql数据类型&约束条件
一 整形 只有Int类型跟存储没有关系,显示的是宽度,其他类型都是限制 整形类型:[(m)][unsigned][zerofill] 作用:存储年龄,等级,id,各种号码 m,代表显示宽度 默认11 ...
- python与MySQL数据库
python与MySQL数据库 慕课网连接 我使用的软件:python2.7 + MySQL+ Navicat for MySQL + Atom 注意:你的数据库表格类型的引擎为:InnoDB :字符 ...
- Python操作MySQL以及数据库索引
目录 python操作MySQL 安装 使用 SQL注入问题 MySQL的索引 为什么使用索引 索引的种类 主键索引 唯一索引 普通索引 索引优缺点 不会命中索引的情况 explain 索引覆盖 My ...
随机推荐
- D3.js学习笔记(一)——DOM上的数据绑定
开始学习D3.js,网上没有找到很满意的中文教程,但是发现了一个很好的英文教程,讲解的非常详细.从一个初始简单的HTML网页开始,逐步加入D3.js的应用,几乎是逐句讲解.学习的时候,就顺便翻译成中文 ...
- MySQL二进制日志功能介绍
二进制日志记录所有更新数据的SQL语句,其中也包含可能更新数据的SQL语句,例如DELETE语句执行过程中无匹配的行.二进制日志中还包含了与执行SQL语句相关的内容,例如SQL语句执行的时间.错误代码 ...
- asp.net获取URL和IP地址
(转自:http://www.cnblogs.com/JuneZhang/archive/2010/11/26/1888863.html) HttpContext.Current.Request.Ur ...
- 【zzulioj-1731】矩阵
题目描述 输入
- ArrayList、Vector、LinkedList(jdk8)
一.ArrayList分析 1.类和构造方法 public class ArrayList<E> extends AbstractList<E> //可以看到其父类是Abstr ...
- win7 秘钥
链接 安装好Windows7后右击计算机--属性--更改产品密匙 输入以下密匙; RHTBY-VWY6D-QJRJ9-JGQ3X-Q2289 HT6VR-XMPDJ-2VBFV-R9PFY-3VP7R ...
- Java发展前景与职业方向解析
大多数人选择Java可能只是因为听说Java前景好.Java比较好找工作.Java语言在TIOBE排行榜上一直位于前三等等之类的原因,但是Java具体好在哪里,心里却是没有什么概念的.本文为你解答学J ...
- Django 之Ajax
必备知识:json 什么是json 定义 JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式. 它基于 ECMAScript (w3c制定的 ...
- 原生js面向对象写法
Mouse就是一个类,有自己的成员变量和成员方法,成员方法一定加上prototype,避免js原型的坑. var Mouse = function(id) { this.id = id; this.n ...
- 第一章 Oracle10g数据库新特性
1.1 Oracle10g数据库概述 1.1.1 网格数据库 Oracle10g数据库是一种为网格计算而设计的数据库,是第一个用完整集成的软件基础架构来实现网络计算的数据库系统,其中10g的g表示gr ...