四、pymysql模块、索引和慢查询
目录
一、pymysql模块
(一)如何使用
- fetchall():获取所有数据,返回的是列表套字典
- fetchone():获取一条数据,返回的是字典
- fetchmany(size):获取size条数据,返回的是列表套字典
- commit:当sql中有参数时,必须要使用conn.commit()
- executemany:新增多条数据
import pymysql
conn = pymysql.connect(host='localhost',user='root',password='wickyo',database 'test',charset='utf8') # 连接数据库
# 创建一个游标对象cursor
# cursor = conn.cursor() # 默认返回的值是元组类型
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 返回的是字典对象
# 1. 查
sql = 'select * from userinfo'
cursor.execute(sql)
res = cursor.fetchall() # 获取所有的数据,返回的是列表套字典
res = cursor.fetchone() # 获取一条数据,返回的是字典类型
res = cursor.fetchmany(12) # 获取12条数据,返回的是列表套字典
print(res)
# 2. 增
## (1)新增一条数据
sql = "insert into user (name, password) values (%s, %s)"
cursor.execute(sql, ('xxx', 'qwe')) ###
## (2)新增多条数据 executemany
data = [
('zekai1', 'qwe'),
('zekai2', 'qwe1'),
('zekai3', 'qwe2'),
('zekai4', 'qwe3'),
]
cursor.executemany(sql, data)
conn.commit() # 必须要加这一句
print(cursor.lastrowid) # 打印最后一行id
# 3. 修
sql = "update user set name=%s where id=%s"
cursor.execute(sql, ('dgsahdsa', 2))
conn.commit()
# 4. 删
sql = "delete from user where id=%s"
cursor.execute(sql, ('dgsahdsa', 2))
conn.commit()
cursor.close() # 关闭cursor对象
conn.close() #关闭数据库里对象
(二)sql注入问题
利用mysql的#会注释的漏洞,避开密码验证
当输入的用户名为
xxx' or 1=1 #时,下面的例子中sql会变成'select * from user where name = xxx' or 1=1 # and password=%s',#后面的内容会被注释掉,会导致sql注入问题原因
没有做任何检验限制
解决办法
将参数传给execute,利用模块本身进行一个校验
import pymysql
user = input('请输入用户名:')
password = input('请输入密码:')
conn = pymysql.connect(host='localhost',user='root',password='wickyo',databse= 'test',charst='utf8')
cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)
# sql1 = "select * from user where name='%s' and password='%s'" % (user, pwd)
# cursor.execute(sql1)
sql2 = 'select * from user where name = %s and password=%s'
cursor.execute(sql2,(user,pwd))
res = cursor.fetchall()
print(res)
cursor.close()
conn.close()
if res:
print('登陆成功')
else:
print('登录失败')
二、索引
优缺点:
优点:加快查询速度
缺点:占用大量磁盘空间
索引的本质:是一个特殊的文件
索引的底层原理:B+树
(一)主键索引
- 加速查找
- 不能重复
- 不能为空
(1)增
# 1. 创建时
create table xxx(
id int auto_increment,
primary(id)
)
# 2. 通过modify修改字段
alter table xxx modify id int auto_increment primary key;
# 3. 通过add修改字段
alter table xxx add primary key(id);
(2)删
alter table t1 drop primary key
(二)唯一索引
unique(字段名)
- 加速查找
- 不能重复
(1)增
# 1. 创建表时
craete table t2(
id int auto_increment primary key,
name varchar(32) not null default'',
unique u_name(name)
)charset utf8;
# 2.创建表结构
create unique index 索引名 on 表名 (字段名;)
# 例子
create unique index ix_name on t2(name);
# 3. 修改表结构
alter table 表名 add unique index 唯一索引名(字段名)
#例子
alter table t2 add unique index ix_name (name)
(2)删
alter table 表名 drop index 唯一索引名;
# 例子
alter table t2 drop index u_name;
(三)普通索引
index(字段名),加速查找
(1)增
# 1. 创建表时
create table t3 (
id int auto_incremnt primary key,
name varchar (32) not null default '',
index u_name (name))
# 2. 创建表结构
create index ix_name on t3(name);
# 3. 修改表结构
alter table t3 add index ix_name (name)
(2)删
alter table t3 drop index ix_name;
(四)联合索引
联合主键索引
primary key (id,name)联合唯一索引
unique(id,name)联合普通索引
index(id,name)
(五)不会命中索引的情况
四则运算
不能在SQL语句中,进行四则运算,会降低SQLd的查询效率
使用函数
select * from t1 where reverse(email)= 'wick';类型不一致
如果列是字符串类型,传入条件必须用引号括起来
select * from t1 where email = 999;order by
select name from s1 order by email;当根据索引排序时,如果select查询的字段不是索引,效率也较低
- 如果对主键排序,速度还是很快
select * from t1 order by id ;
- 如果对主键排序,速度还是很快
count
count(*)相较于count(列名)或count(1),结果一昂,前者效率低组合索引最左前缀
select * from user where name = 'wick'and email = 'wick@qq.com';上述场景想要添加索引加快速度,错误中做法:
index ix_name(name)index ix_email(email)正确的做法:
index ix_name_email(name,email)where name = 'wick' and email= 'wick@11.com'此种情况索引命中where name='wick'此种情况也命中where email = 'wick@qq.com'此种情况未命中
注意:
index (a,b,c,d) where a=2 and b=3 and c=1 and d=1; #命中 where a=2 and c=3 and d=4 # 命中 where b =2 and c= 3 and d=3 # 未命中
(六)explain
explain select * from user where name = 'wick' and email = 'wick@qq.com'\G
# id: 1
# select_type: SIMPLE
# table: user
# partitions: NULL
# type: ref 索引指向 all
# possible_keys: ix_name_email 可能用到的索引
# key: ix_name_email 确实用到的索引
# key_len: 214 索引长度
# ref: const,const
# rows: 1 扫描的长度
# filtered: 100.00
# Extra: Using index 使用到了索引
(七)索引覆盖
被查询的列,数据能从索引中取得,而不用通过行定位符 row-locator 再到 row 上获取,即“被查询列要被所建的索引覆盖”,这能够加速查询速度。
select id from t1 where id = 2000;
三、慢查询日志
# 1. 查看慢SQL的相关变量
show variables like '%slow%';
# 2. 查看long相关的变量
show variables like '%long%';
# 3. 配置慢相关的的变量
# set global 变量名=值
# 设置慢查询日志开关
set global slow_query_log = on;
# 设置慢查询日志存储地址
set global slow_query_log_file= 'D:/mysql/data/myslow.log';
# 设置慢查询匹配时间
set global long_query_time = 1
四、pymysql模块、索引和慢查询的更多相关文章
- mysql python pymysql模块 增删改查 查询 fetchone
import pymysql mysql_host = '192.168.0.106' port = 3306 mysql_user = 'root' mysql_pwd = ' encoding = ...
- mysql python pymysql模块 增删改查 查询 字典游标显示
我们看到取得结果是一个元祖,但是不知道是哪个字段的,如果字段多的时候,就比较麻烦 ''' (1, 'mike', '123') (2, 'jack', '456') ''' 用字典显示查询的结果,也可 ...
- mysql python pymysql模块 增删改查 查询 fetchmany fetchall函数
查询的fetchmany fetchall函数 import pymysql mysql_host = '192.168.0.106' port = 3306 mysql_user = 'root' ...
- pymysql 模块 使用目录
mysql python pymysql模块 基本使用 mysql python pymysql模块 增删改查 插入数据 介绍 commit() execute() executemany() 函数 ...
- 第八章| 3. MyAQL数据库|Navicat工具与pymysql模块 | 内置功能 | 索引原理
1.Navicat工具与pymysql模块 在生产环境中操作MySQL数据库还是推荐使用命令行工具mysql,但在我们自己开发测试时,可以使用可视化工具Navicat,以图形界面的形式操作MySQL数 ...
- 多表查询、可视化工具、pymysql模块
create table dep( id int primary key auto_increment, name varchar(16), work varchar(16) ); create ta ...
- python 全栈开发,Day63(子查询,MySQl创建用户和授权,可视化工具Navicat的使用,pymysql模块的使用)
昨日内容回顾 外键的变种三种关系: 多对一: 左表的多 对右表一 成立 左边的一 对右表多 不成立 foreign key(从表的id) refreences 主表的(id) 多对多 建立第三张表(f ...
- MySQL:记录的增删改查、单表查询、约束条件、多表查询、连表、子查询、pymysql模块、MySQL内置功能
数据操作 插入数据(记录): 用insert: 补充:插入查询结果: insert into 表名(字段1,字段2,...字段n) select (字段1,字段2,...字段n) where ...; ...
- MySQL多表查询,Navicat使用,pymysql模块,sql注入问题
一.多表查询 #建表 create table dep( id int, name varchar(20) ); create table emp( id int primary key auto_i ...
随机推荐
- Python学习-is和==区别, encode和decode
一.is 和 == 介绍 1. is 比较的是两个对象的内存地址是否相同,它们是不是同一个对象. 2. == 比较的是两个对象的内容是否相同. 在使用is前,先介绍Python的一个内置函数id( ...
- windows下虚拟环境virtualenv的简单操作
使用豆瓣源安装(推荐) [推荐] python3.X安装和pip安装方法 pip install -i https://pypi.douban.com/simple XXX 1.安装virtualen ...
- JS中数据类型转换
JS中数据类型转换汇总 JS中的数据类型分为 [基本数据类型] 数字 number 字符串 string 布尔 boolean 空 null 未定义 undefined [引用数据类型] 对象 obj ...
- Spring Boot Thymeleaf 实现国际化
开发传统Java WEB工程时,我们可以使用JSP页面模板语言,但是在SpringBoot中已经不推荐使用了.SpringBoot支持如下页面模板语言 Thymeleaf FreeMarker Vel ...
- C语言入门-指针
终于到了精髓的地方了,这确实有点懵,总感觉这太麻烦了,而且写着也不爽,还是怀念py或者java,但也没办法,还是要继续学下去. 一.运算符& scanf("%d" , &a ...
- ABP vNext 不使用工作单元为什么会抛出异常
一.问题 该问题经常出现在 ABP vNext 框架当中,要复现该问题十分简单,只需要你注入一个 IRepository<T,TKey> 仓储,在任意一个地方调用 IRepository& ...
- Eclipse导入别人项目爆红叉
1.导入项目之前,请确认工作空间编码已设置为utf-8:window->Preferences->General->Wrokspace->Text file encoding- ...
- Linux下格式化恢复USB启动优盘
问题描述:优盘制作成启动盘安装操作系统,但是后边使用时发现无法格式化,提示 This partition cannot be modified because it contains a partit ...
- 利用Helm简化Kubernetes应用部署(2)
目录 定义Charts 使用Helm部署Demo Helm常用操作命令 定义Charts 回到之前的“charts”目录,我们依次进行解读并进行简单的修改. Chart.yaml 配置示例: a ...
- PHP中sha1()函数和md5()函数的绕过
相信大家都知道,sha1函数和md5都是哈希编码的一种,在PHP中,这两种编码是存在绕过漏洞的. PHP在处理哈希字符串时,会利用”!=”或”==”来对哈希值进行比较,它把每一个以”0E”开头的哈希值 ...