赞
踩
Pymysql是一个Python的数据库连接库,用于连接MySQL数据库。下面是Pymysql连接MySQL数据库的基本步骤:
安装Pymysql库:可以使用pip安装pymysql模块: pip install pymysql
连接MySQL数据库:使用pymysql库中的connect()方法连接MySQL数据库,需要提供如下参数:
a. host:连接的MySQL主机地址
b. port:连接的MySQL端口号,默认为3306
c. user:连接的MySQL用户名
d. password:连接的MySQL密码
e. database:连接的数据库名称
创建游标对象:使用connection对象的cursor()方法获取游标对象
执行SQL语句:使用游标对象的execute()方法执行SQL语句
获取SQL执行结果:使用游标对象的fetchone()、fetchall()、fetchmany()等方法获取SQL执行结果
关闭游标对象和连接对象:使用游标对象和连接对象的close()方法关闭它们
下面是一个简单的示例代码:
- import pymysql
-
- # 连接MySQL数据库
- connection = pymysql.connect(
- host='localhost',
- port=3306,
- user='root',
- password='123456',
- database='test',
- charset='utf8mb4'
- autocommit=True # 针对增 改 删自动二次确认
- )
-
- # 获取游标对象
- cursor = connection.cursor()
-
- # 执行SQL语句
- sql = "SELECT * FROM students;"
- cursor.execute(sql)
-
- # 获取SQL执行结果
- result = cursor.fetchall()
- print(result)
-
- # 关闭游标对象和连接对象
- cursor.close()
- connection.close()
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
该示例代码连接了本地的MySQL数据库,执行了一个SELECT语句,获取了students表中的所有数据,并打印输出。注意,在实际使用中要确保数据库用户名、密码等信息的安全。
- import pymysql
- # 链接
- conn=pymysql.connect(host='localhost',user='root',password='123',database='egon')
- # 游标
- cursor=conn.cursor()
-
- # 执行sql语句
- # part1
- # sql='insert into userinfo(name,password) values("root","123456");'
- # res=cursor.execute(sql) #执行sql语句,返回sql影响成功的行数
- # print(res)
-
- # part2
- # sql='insert into userinfo(name,password) values(%s,%s);'
- # res=cursor.execute(sql,("root","123456")) #执行sql语句,返回sql影响成功的行数
- # print(res)
-
- # part3
- sql='insert into userinfo(name,password) values(%s,%s);'
- res=cursor.executemany(sql,[("root","123456"),("lhf","12356"),("eee","156")]) #执行sql语句,返回sql影响成功的行数
- print(res)
-
- conn.commit() # 提交后才发现表中插入记录成功
- cursor.close()
- conn.close()
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
fetchone
返回单个的元组,也就是一条记录(row),如果没有结果 则返回 Nonefetchmany
返回多个元组,返回多条记录(row),需要指定具体返回多少个记录。如fetchmany(2) 返回两条记录,默认则为1
fetchall
返回多个元组,即返回多个记录(rows),如果没有结果 则返回 ()需要注明:在MySQL中是NULL,而在Python中则是None
- import pymysql
- # 链接
- conn=pymysql.connect(host='localhost',user='root',password='123',database='egon')
- # 游标
- cursor=conn.cursor()
-
- # 执行sql语句
- sql='select * from userinfo;'
- rows=cursor.execute(sql) #执行sql语句,返回sql影响成功的行数rows,将结果放入一个集合,等待被查询
-
- # cursor.scroll(3,mode='absolute') # 相对绝对位置移动
- # cursor.scroll(3,mode='relative') # 相对当前位置移动
-
- #批量查询
-
- res1=cursor.fetchone()
- res2=cursor.fetchone()
- res3=cursor.fetchone()
- res4=cursor.fetchmany(2)
- res5=cursor.fetchall()
- print(res1)
- print(res2)
- print(res3)
- print(res4)
- print(res5)
- print('%s rows in set (0.00 sec)' %rows)
-
-
-
- conn.commit() # 提交后才发现表中插入记录成功
- cursor.close()
- conn.close()
-
-
- 结果
-
- '''
- (1, 'root', '123456')
- (2, 'root', '123456')
- (3, 'root', '123456')
- ((4, 'root', '123456'), (5, 'root', '123456'))
- ((6, 'root', '123456'), (7, 'lhf', '12356'), (8, 'eee', '156'))
- rows in set (0.00 sec)
- '''
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
MySQL注入是一种安全漏洞,攻击者可以利用它来在MySQL数据库中执行任意代码或访问敏感信息。攻击者通常在输入参数中注入恶意代码,例如输入用户名或密码等。通过注入代码,攻击者可以利用数据库系统的漏洞绕过身份验证、访问敏感数据或对数据库进行破坏。
为防止MySQL注入,应该采取以下措施:
输入数据验证:确保用户输入的数据符合预期类型、格式和长度,过滤掉不必要的字符。
参数化查询:使用预编译的SQL语句和参数,而不是直接拼接SQL语句和参数,从而避免注入攻击。
最小权限原则:为应用程序分配最小的权限,以限制攻击者能够访问的数据库内容。
强密码策略:为数据库应用程序用户设置强密码策略,如强制使用复杂密码、定期更改密码等。
限制错误信息:不要在错误信息中泄露敏感信息,如数据库结构、用户身份验证信息等。
定期更新:定期更新数据库系统和应用程序,以保持安全性。
1.只需要用户名也可以登录
username:>>> kevin111 " -- ddasfdfsdfdsfsdfsdfsdfdsfsdfsd
username:>>> xxx " or 1=1
2.不需要用户名和密码也可以登录
- # 连接MySQL服务端
- conn = pymysql.connect(
- host='127.0.0.1',
- port=3306,
- user='root',
- password='123',
- database='db8_3',
- charset='utf8',
- autocommit=True # 针对增 改 删自动二次确认
- )
-
- # 产生一个游标对象
- cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
-
- # 编写SQL语句
- username = input('username>>>:').strip()
- password = input('password>>>:').strip()
-
- sql = "select * from userinfo where name=%s and pwd=%s"
- cursor.execute(sql,(username,password))
- data = cursor.fetchall()
-
- if data:
-
- print(data)
- print('登录成功')
-
- else:
- print('用户名或密码错误')
-
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
SQL注入的原因 是由于特殊符号的组合会产生特殊的效果
实际生活中 尤其是在注册用户名的时候 会非常明显的提示你很多特殊符号不能用
原因也是一样的
结论:设计到敏感数据部分 不要自己拼接 交给现成的方法拼接即可
- # 原来是我们对sql进行字符串拼接
- # sql="select * from userinfo where name='%s' and password='%s'" %(user,pwd)
- # print(sql)
- # res=cursor.execute(sql)
-
- #改写为(execute帮我们做字符串拼接,我们无需且一定不能再为%s加引号了)
- sql="select * from userinfo where name=%s and password=%s" #!!!注意%s需要去掉引号,因为pymysql会自动为我们加上
- res=cursor.execute(sql,[user,pwd]) #pymysql模块自动帮我们解决sql注入的问题,只要我们按照pymysql的规矩来。
-
-
视图就是通过查询得到一张虚拟表,然后保存下来,下次直接使用即可
如果要频繁使用一张虚拟表,可以不用重复查询
MySQL语句
create view teacher2course as
select * from teacher inner join course on teacher.tid = course.teacher_id;
创建好了之后 验证它的存在navicat验证 cmd终端验证
最后文件验证 得出下面的结论 视图只有表结构数据还是来源于之前的表
delete from teacher2course where id=1;
特别注意
1、在硬盘中,视图只有表结构文件,没有表数据文件
2、视图通常是用于查询,尽量不要修改视图中的数据
MySQL语句
drop view teacher2course;
不会!视图是mysql的功能,如果你的项目里面大量的使用到了视图,那意味着你后期想要扩张某个功能的时候这个功能恰巧又需要对视图进行修改,意味着你需要先在mysql这边将视图先修改一下,然后再去应用程序中修改对应的sql语句,这就涉及到跨部门沟通的问题,所以通常不会使用视图,而是通过重新修改sql语句来扩展功能
在满足对某张表数据的增、删、改的情况下,自动触发的功能称之为触发器
触发器专门针对我们对某一张表数据增insert、删delete、改update的行为,这类行为一旦执行
就会触发触发器的执行,即自动运行另外一段sql代码 2、
语法结构
create trigger 触发器的名字 before/after insert/update/delete on 表名 for each row
begin
sql语句
end
- create trigger tri_after_insert_t1 after insert on 表名 for each row
-
- begin
-
- sql代码
-
- end
-
- create trigger tri_after_insert_t2 before insert on 表名 for each row
-
- begin
-
- sql代码
-
- end
- create trigger tri_after_delete_t1 after delete on 表名 for each row
-
- begin
- sql代码
- end
-
- create trigger tri_after_delete_t2 before delete on 表名 for each row
-
- begin
- sql代码
- end
- create trigger tri_after_update_t1 after update on 表名 for each row
-
- begin
- sql代码
- end
-
- create trigger tri_after_update_t2 before update on 表名 for each row
-
- begin
- sql代码
- end
需要注意 在书写sql代码的时候结束符是; 而整个触发器的结束也需要分号;
这就会出现语法冲突 需要我们临时修改结束符号
delimiter $$
delimiter ;
该语法只在当前窗口有效
- REATE TABLE cmd (
-
- id INT PRIMARY KEY auto_increment,
- USER CHAR (32),
- priv CHAR (10),
- cmd CHAR (64),
- sub_time datetime, #提交时间
- success enum ('yes', 'no') #0代表执行失败
- );
-
- CREATE TABLE errlog (
-
- id INT PRIMARY KEY auto_increment,
- err_cmd CHAR (64),
- err_time datetime
- );
-
- delimiter $$ # 将mysql默认的结束符由;换成$$
-
- create trigger tri_after_insert_cmd after insert on cmd for each row
-
- begin
-
- if NEW.success = 'no' then # 新记录都会被MySQL封装成NEW对象
- insert into errlog(err_cmd,err_time) values(NEW.cmd,NEW.sub_time);
- end if;
-
- end $$
-
- delimiter ; # 结束之后记得再改回来,不然后面结束符就都是$$了
-
- #往表cmd中插入记录,触发触发器,根据IF的条件决定是否插入错误日志
-
- INSERT INTO cmd (
- USER,
- priv,
- cmd,
- sub_time,
- success
- )
-
- VALUES
- ('egon','0755','ls -l /etc',NOW(),'yes'),
- ('egon','0755','cat /etc/passwd',NOW(),'no'),
- ('egon','0755','useradd xxx',NOW(),'no'),
- ('egon','0755','ps aux',NOW(),'yes');
-
-
- # 查询errlog表记录
- select * from errlog;
- # 删除触发器
- drop trigger tri_after_insert_cmd;
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
开启一个事务可以包含一些sql语句,这些sql语句要么同时成功
要么一个都别想成功,称之为事务的原子性
保证了对数据操作的数据安全性
MySQL事务是指一组SQL语句,这组语句要么全部执行,要么全部回滚。这种机制可以保证数据的一致性和完整性。
事务必须具有ACID属性,即原子性(Atomicity)、一致性(Consistency)、隔离性(Isolation)和持久性(Durability)。
原子性:事务是不可分割的工作单元,事务中的所有操作要么全部执行,要么全部不执行。
一致性:事务执行前后,数据的完整性约束没有被破坏。
隔离性:事务的执行不会受到其他事务的干扰。
持久性:事务一旦提交,对数据的修改是永久性的,即使出现系统故障也能够恢复。
MySQL支持多种事务隔离级别,包括读未提交(Read uncommitted)、读已提交(Read committed)、可重复读(Repeatable read)、串行化(Serializable)等。不同的隔离级别可以解决不同的并发问题。
ps案例:用交行的卡操作建行ATM机给工商的账户转钱,必须同时成功
start transaction; 显式地开启一个事务;
commit; 也可以使用 COMMIT WORK ,不过二者是等价的。COMMIT会提交事务,并使已对数据库进行的所有修改称为永久性的;
rollback; 有可以使用 ROLLBACK WORK ,不过二者是等价的。回滚会结束用户的事务,并撤销正在进行的所有未提交的修改;
-
- create table user(
- id int primary key auto_increment,
- name char(32),
- balance int
-
- );
-
- insert into user(name,balance)
- values
- ('jason',1000),
- ('egon',1000),
- ('tank',1000);
-
- # 修改数据之前先开启事务操作
- start transaction;
-
- # 修改操作
-
- update user set balance=900 where name='jason'; #买支付100元
- update user set balance=1010 where name='egon'; #中介拿走10元
- update user set balance=1090 where name='tank'; #卖家拿到90元
-
- # 回滚到上一个状态
-
- rollback;
-
- 结果
-
- ('jason',1000),
- ('egon',1000),
- ('tank',1000);
-
- 开启事务之后,只要没有执行commit操作,数据其实都没有真正刷新到硬盘
-
- commit;
-
- 结果
-
- ('jason',900),
- ('egon',1010),
- ('tank',1090);
-
-
- """开启事务检测操作是否完整,不完整主动回滚到上一个状态,如果完整就应该执行commit操作"""
-
-
-
- # 站在python代码的角度,应该实现的伪代码逻辑,
-
- try:
- # 少了开事务...
-
- update user set balance=900 where name='jason'; #买支付100元
- update user set balance=1010 where name='egon'; #中介拿走10元
- update user set balance=1090 where name='tank'; #卖家拿到90元
- except 异常:
-
- rollback;#回滚数据
-
- else:
- commit;#提交数据后不可修改
-
- # 那如何检测异常?
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
存储过程包含了一系列可执行的sql语句,存储过程存放于MySQL中,通过调用它的名字可以执行其内部的一堆sql,类似于python中的自定义函数
- delimiter $$
- create procedure p1()
- begin
- select * from user;
- end $$
- delimiter ;
-
- # 调用
- call p1()
应用程序:只需要开发应用程序的逻辑
mysql:编写好存储过程,以供应用程序调用
优点:开发效率,执行效率都高
缺点:考虑到人为因素、跨部门沟通等问题,会导致扩展性差
应用程序:除了开发应用程序的逻辑,还需要编写原生sql
优点:比方式1,扩展性高(非技术性的)
缺点:
1、开发效率,执行效率都不如方式1
2、编写原生sql太过于复杂,而且需要考虑到sql语句的优化问题
应用程序:开发应用程序的逻辑,不需要编写原生sql,基于别人编写好的框架来处理数据,ORM
优点:不用再编写纯生sql,这意味着开发效率比方式2高,同时兼容方式2扩展性高的好处
缺点:执行效率连方式2都比不过
- # 介绍形参特点 再写具体功能
-
- delimiter $$
-
- create procedure p2(
- in m int, # in表示这个参数必须只能是传入不能被返回出去
- in n int,
- out res int # out表示这个参数可以被返回出去
- )
- begin
- select tname from teacher where tid > m and tid < n;
- set res=0; # 用来标志存储过程是否执行
- end $$
- delimiter ;
-
-
- # 针对res需要先提前定义
- set @res=10; 定义
- select @res; 查看
- call p1(1,5,@res) 调用
- select @res 查看
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- # 大前提:存储过程在哪个库下面创建的只能在对应的库下面才能使用!!!
-
- # 1、直接在mysql中调用
- set @res=10 # res的值是用来判断存储过程是否被执行成功的依据,所以需要先定义一个变量@res存储10
- call p1(2,4,10); # 报错
- call p1(2,4,@res);
-
- # 查看结果
- select @res; # 执行成功,@res变量值发生了变化
-
- # 2、在python程序中调用
- pymysql链接mysql
- 产生的游表cursor.callproc('p1',(2,4,10)) # 内部原理:@_p1_0=2,@_p1_1=4,@_p1_2=10;
- cursor.execute('select @_p1_2;')
-
-
- # 3、存储过程与事务使用举例(了解)
- delimiter //
- create PROCEDURE p5(
- OUT p_return_code tinyint
- )
- BEGIN
- DECLARE exit handler for sqlexception
- BEGIN
- -- ERROR
- set p_return_code = 1;
- rollback;
- END;
-
-
- DECLARE exit handler for sqlwarning
- BEGIN
- -- WARNING
- set p_return_code = 2;
- rollback;
- END;
-
- START TRANSACTION;
- update user set balance=900 where id =1;
- update user123 set balance=1010 where id = 2;
- update user set balance=1090 where id =3;
- COMMIT;
-
- -- SUCCESS
- set p_return_code = 0; #0代表执行成功
-
-
- END //
- delimiter ;
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
注意与存储过程的区别,mysql内置的函数只能在sql语句中使用!
- CREATE TABLE blog (
- id INT PRIMARY KEY auto_increment,
- NAME CHAR (32),
- sub_time datetime
- );
-
- INSERT INTO blog (NAME, sub_time)
- VALUES
- ('第1篇','2015-03-01 11:31:21'),
- ('第2篇','2015-03-11 16:31:21'),
- ('第3篇','2016-07-01 10:21:31'),
- ('第4篇','2016-07-22 09:23:21'),
- ('第5篇','2016-07-23 10:11:11'),
- ('第6篇','2016-07-25 11:21:31'),
- ('第7篇','2017-03-01 15:33:21'),
- ('第8篇','2017-03-01 17:32:21'),
- ('第9篇','2017-03-01 18:31:21');
-
-
- +----+--------------------------------------+---------------------+
- | id | NAME | sub_time | month
- +----+--------------------------------------+---------------------+
- | 1 | 第1篇 | 2015-03-01 11:31:21 | 2015-03
- | 2 | 第2篇 | 2015-03-11 16:31:21 | 2015-03
- | 3 | 第3篇 | 2016-07-01 10:21:31 | 2016-07
- | 4 | 第4篇 | 2016-07-22 09:23:21 | 2016-07
- | 5 | 第5篇 | 2016-07-23 10:11:11 | 2016-07
- | 6 | 第6篇 | 2016-07-25 11:21:31 | 2016-07
- | 7 | 第7篇 | 2017-03-01 15:33:21 | 2017-03
- | 8 | 第8篇 | 2017-03-01 17:32:21 | 2017-03
- | 9 | 第9篇 | 2017-03-01 18:31:21 | 2017-03
- +----+--------------------------------------+---------------------+
-
- select count(*) from blog group by month;
-
- select date_format(sub_time,'%Y-%m'),count(id) from blog group by date_format(sub_time,'%Y-%m');
-
-
- 具体查看博客园
- https://blog.csdn.net/GG_Bruse/article/details/131484538
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- delimiter //
- CREATE PROCEDURE proc_if ()
- BEGIN
-
- declare i int default 0;
- if i = 1 THEN
- SELECT 1;
- ELSEIF i = 2 THEN
- SELECT 2;
- ELSE
- SELECT 7;
- END IF;
-
- END //
- delimiter ;
- delimiter //
- CREATE PROCEDURE proc_while ()
- BEGIN
-
- DECLARE num INT ;
- SET num = 0 ;
- WHILE num < 10 DO
- SELECT
- num ;
- SET num = num + 1 ;
- END WHILE ;
-
- END //
- delimiter ;
知识回顾:数据都是存在硬盘上的,那查询数据不可避免的需要进行IO操作
索引就是一种数据结构,类似于书的目录。意味着以后再查数据应该先找目录再找数据,而不是用翻页的方式查询数据
索引在MySQL中也叫做“键”,是存储引擎用于快速找到记录的一种数据结构。
primary key
unique key
index key
注意:上面三种key前两种除了有加速查询的效果之外还有额外的约束条件(primary key:非空且唯一,unique key:唯一),而index key没有任何约束功能只会帮你加速查询
本质都是:通过不断地缩小想要获取数据的范围来筛选出最终想要的结果,同时把随机的事件变成顺序的事件,也就是说,有了这种索引机制,我们可以总是用同一种查找方式来锁定数据。
在表中有大量数据的前提下,创建索引速度会很慢(建表的时候,如果明显需要索引,就提前加上)
# 以后实际添加索引的时候,尽量在空表的时候添加,在创建表的时候就添加索引,此时添加索引是最快的
# 如果表中数据已经有了,还需要添加索引,也可以,只不过创建索引的速度会很慢,不建议这样做
在索引创建完毕后,对表的查询性能会大幅度提升,但是写的性能会降低
# 但是,写的性能影响不是很大,因为在实际中,写的频率很少,大部分操作都是查询
# 如何添加索引?到底给哪些字段加索引呢?
'''没有固定答案,具体给哪个字段加索引,要看你实际的查询条件'''
select * from user where name='' and password='';
# 索引的使用其实是需要大量的工作经验,才能正确的判断出
'''不要一创建表就加索引,在一张表中,最多最多不要超过15个索引,索引越多,性能就会下降'''
# 如何数据量比较小,不需要加索引,100w一下一般不用加,mysql针对于1000w一下的数据,性能不会下降太多.
只有叶子结点存放真实数据,根和树枝节点存的仅仅是虚拟数据
查询次数由树的层级决定,层级越低次数越少
一个磁盘块儿的大小是一定的,那也就意味着能存的数据量是一定的。如何保证树的层级最低呢?一个磁盘块儿存放占用空间比较小的数据项
# 以后加索引的时候,尽量给字段中存的是数字的列加,我们使用主键查询速度很快
select * from user where name = ''
select * from user where id = '' # 主键查询的更快一些
思考
我们应该给我们一张表里面的什么字段字段建立索引能够降低树的层级高度
>>> 主键id字段
聚集索引其实指的就是表的主键,innodb引擎规定一张表中必须要有主键。先来回顾一下存储引擎。
myisam在建表的时候对应到硬盘有几个文件(三个)?
innodb在建表的时候对应到硬盘有几个文件(两个)?frm文件只存放表结构,不可能放索引,也就意味着innodb的索引跟数据都放在idb表数据文件中。
特点:叶子结点放的一条条完整的记录
辅助索引:查询数据的时候不可能都是用id作为筛选条件,也可能会用name,password等字段信息,那么这个时候就无法利用到聚集索引的加速查询效果。就需要给其他字段建立索引,这些索引就叫辅助索引
特点:叶子结点存放的是辅助索引字段对应的那条记录的主键的值(比如:按照name字段创建索引,那么叶子节点存放的是:{name对应的值:name所在的那条记录的主键值})
select name from user where name='jack';
上述语句叫覆盖索引:只在辅助索引的叶子节点中就已经找到了所有我们想要的数据
select age from user where name='jack';
上述语句叫非覆盖索引,虽然查询的时候命中了索引字段name,但是要查的是age字段,所以还需要利用主键才去查找
- #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<1000000)do
- insert into s1 values(i,'jason','male',concat('jason',i,'@oldboy'));
- set i=i+1;
- end while;
- END$$ #$$结束
- delimiter ; #重新声明分号为结束符号
-
- #3. 查看存储过程
- show create procedure auto_insert1\G
-
- #4. 调用存储过程
- call auto_insert1();
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- # 表没有任何索引的情况下
- select * from s1 where id=30000;
- # 避免打印带来的时间损耗
- select count(id) from s1 where id = 1000000;
- select count(id) from s1 where id = 1;
-
- # 给id做一个主键
- alter table s1 add primary key(id); # 速度很慢
-
- select count(id) from s1 where id = 1; # 速度相较于未建索引之前两者差着数量级
- select count(id) from s1 where name = 'jason' # 速度仍然很慢
-
-
- """
- 范围问题
- """
- # 并不是加了索引,以后查询的时候按照这个字段速度就一定快
- select count(id) from s1 where id > 1; # 速度相较于id = 1慢了很多
- select count(id) from s1 where id >1 and id < 3;
- select count(id) from s1 where id > 1 and id < 10000;
- select count(id) from s1 where id != 3;
-
- alter table s1 drop primary key; # 删除主键 单独再来研究name字段
- select count(id) from s1 where name = 'jason'; # 又慢了
-
- create index idx_name on s1(name); # 给s1表的name字段创建索引
- select count(id) from s1 where name = 'jason' # 仍然很慢!!!
- """
- 再来看b+树的原理,数据需要区分度比较高,而我们这张表全是jason,根本无法区分
- 那这个树其实就建成了“一根棍子”
- """
- select count(id) from s1 where name = 'xxx';
- # 这个会很快,我就是一根棍,第一个不匹配直接不需要再往下走了
- select count(id) from s1 where name like 'xxx';
- select count(id) from s1 where name like 'xxx%';
- select count(id) from s1 where name like '%xxx'; # 慢 最左匹配特性
-
- # 区分度低的字段不能建索引
- drop index idx_name on s1;
-
- # 给id字段建普通的索引
- create index idx_id on s1(id);
- select count(id) from s1 where id = 3; # 快了
- select count(id) from s1 where id*12 = 3; # 慢了 索引的字段一定不要参与计算
-
- drop index idx_id on s1;
- select count(id) from s1 where name='jason' and gender = 'male' and id = 3 and email = 'xxx';
- # 针对上面这种连续多个and的操作,mysql会从左到右先找区分度比较高的索引字段,先将整体范围降下来再去比较其他条件
- create index idx_name on s1(name);
- select count(id) from s1 where name='jason' and gender = 'male' and id = 3 and email = 'xxx'; # 并没有加速
-
- drop index idx_name on s1;
- # 给name,gender这种区分度不高的字段加上索引并不难加快查询速度
-
- create index idx_id on s1(id);
- select count(id) from s1 where name='jason' and gender = 'male' and id = 3 and email = 'xxx'; # 快了 先通过id已经讲数据快速锁定成了一条了
- select count(id) from s1 where name='jason' and gender = 'male' and id > 3 and email = 'xxx'; # 慢了 基于id查出来的数据仍然很多,然后还要去比较其他字段
-
- drop index idx_id on s1
-
- create index idx_email on s1(email);
- select count(id) from s1 where name='jason' and gender = 'male' and id > 3 and email = 'xxx'; # 快 通过email字段一剑封喉
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- select count(id) from s1 where name='jason' and gender = 'male' and id > 3 and email = 'xxx';
- # 如果上述四个字段区分度都很高,那给谁建都能加速查询
- # 给email加然而不用email字段
- select count(id) from s1 where name='jason' and gender = 'male' and id > 3;
- # 给name加然而不用name字段
- select count(id) from s1 where gender = 'male' and id > 3;
- # 给gender加然而不用gender字段
- select count(id) from s1 where id > 3;
-
- # 带来的问题是所有的字段都建了索引然而都没有用到,还需要花费四次建立的时间
-
- create index idx_all on s1(email,name,gender,id); # 最左匹配原则,区分度高的往左放
- select count(id) from s1 where name='jason' and gender = 'male' and id > 3 and email = 'xxx'; # 速度变快
-
END
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。