pymysql的增删改查、索引
1.pymysql增删改
一定要有commit()
import pymysql
username = input("请输入用户名:")
pwd = input("请输入密码:")
conn = pymysql.connect(
host="localhost",
port=3306,
database="db2",
user="root",
password="",
charset="utf8"
)
sql = "insert into userinfo(name,pwd) values(%s,%s)"
cur = conn.cursor()
res = cur.execute(sql,[username,pwd])
print(res)
conn.commit() 一定要加,否则数据库中不会保存,只会在缓存中保存。如果之前没有加这句,提交了一次,虽然没有提交上,但是id的自增确保留下来了
cur.close()
conn.close()
2.pymysql查询
fetchone()
fetchmany(size)
fetchall()
import pymysql conn = pymysql.connect(
host="localhost",
port=3306,
database="db2",
user="root",
password="",
charset="utf8"
)
sql = "select * from userinfo"
cur = conn.cursor(cursor=pymysql.cursors.DictCursor)#会以字典的形式返回查询信息 res = cur.execute(sql)
print(res) # ret = cur.fetchone()
# print(ret)
# ret = cur.fetchone()
# print(ret)
# ret = cur.fetchone()
# print(ret)
# ret = cur.fetchone()
# print(ret)
# ret = cur.fetchone()
# print(ret)
# ret = cur.fetchone()#单次查询如果超出范围会返回none
# print(ret) # ret = cur.fetchmany(6)#多条查询超过范围显示所有信息
# print(ret) ret = cur.fetchall()
print(ret)
conn.commit()
cur.close()
conn.close()
import pymysql conn = pymysql.connect(
host="localhost",
port=3306,
database="db2",
user="root",
password="",
charset="utf8"
)
sql = "select * from userinfo"
cur = conn.cursor(cursor=pymysql.cursors.DictCursor)#会以字典的形式返回查询信息 res = cur.execute(sql)
print(res) ret = cur.fetchone()
print(ret)
#relative 相对于原来的位置,如果正数向下移动 反之亦然
#absolute 相对于起始位置,正数向下移动
cur.scroll(-1,mode="relative")
ret = cur.fetchone()
print(ret) conn.commit()
cur.close()
conn.close()
3.索引
作用:约束+加速查询
普通索引:create index ix_name on 表名(字段名);
作用:加速查询
唯一索引:create unique index un_name on 表名(字段名);
作用:约束+加速查询
主键索引:设置主键
覆盖索引:在索引文件中直接获取数据
例如:select name from big_data where name = "zhang50000"; 对name字段设置了索引
索引合并:把多个单列索引一起使用
select * from big_data where name = "zhang13131" and id = 13131;
联合索引:
联合普通索引
联合唯一索引
联合主键索引
最左前缀:
如果使用组合索引,name email 组合索引之后,查询
1.name和email 使用索引
2.name 使用索引
3.email 不使用索引
对于同时搜索n个条件时,组合索引的性能好于多个单列索引
组合索引的性能>索引合并的性能
对于索引:
1.创建索引:
注意事项:
1.避免使用select *
2.count(1)或count(列)代替count(*)
3.创建表时尽量使用char代替varchar
4.表的字段顺序固定长度的字段优先
5.组合索引代替多个单列索引(经常使用多个条件查询)
6.尽量使用短索引(create index ix_title on tb(title(16)));特殊的数据类型text
7.使用连接(join)代替子查询
8.连表时注意条件类型需要一致
9.索引散列(重复少)不适于用索引,例如性别
2.命中索引
3.正确使用索引
注意事项:
like "%xx"
select * from userinfo where name like "%al";
使用函数:
select * from userinfo where reverse(name) = "zhang333";
or
select * from userinfo where id = 1 or email = "zhang122@qq.com";
类型不一致:
select * from userinfo where name = 999;
!=:
select count(*) from userinfo where name != "zhang";
特别的:如果是主键,则还是会走索引
>:
select * from userinfo where name > "zhang";
特别的:如果是主键或索引是整数类型,则还是会走索引
select * from userinfo where id > 123;
select * from userinfo where num > 123;
order by:
select * from userinfo order by name desc;
当根据索引排序时,选择的映射如果不是索引,则不走索引
特别的:如果对主键排序则还是会走索引
select * from userinfo order by nid desc;
组合索引最左前缀:
然后组合索引为:(name,email);
name and email 使用索引
name 使用索引
email 不使用索引
对于创建索引,它是真实存在的,占用硬盘空间,尽量不要使用索引
select * from big_data where id > 2000010 limit 10;
select * from (select * from big_data where id > 2000020 limit 30) as A order by id desc limit 10;
pymysql的增删改查、索引的更多相关文章
- pymysql 增删改 查 索引
pymysql 模块的使用 pip install pymysql username = input ("请输入用户") pwd = input ("请输入密码" ...
- MongoDB学习笔记,基础+增删改查+索引+聚合...
一 基础了解 对应关系 -> https://docs.mongodb.com/manual/reference/sql-comparison/ database -> database ...
- ES 17 - (底层原理) Elasticsearch增删改查索引数据的过程
目录 1 增删改document的流程 1.1 协调节点 - Coordinating Node 1.2 增删改document的流程 2 查询document的流程 1 增删改document的流程 ...
- mysql python pymysql模块 增删改查 插入数据 介绍 commit() execute() executemany() 函数
import pymysql mysql_host = '192.168.0.106' port = 3306 mysql_user = 'root' mysql_pwd = ' encoding = ...
- 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' ...
- 5月11日 python学习总结 子查询、pymysql模块增删改查、防止sql注入问题
一.子查询 子查询:把一个查询语句用括号括起来,当做另外一条查询语句的条件去用,称为子查询 select emp.name from emp inner join dep on emp.dep_id ...
- Solr增删改查索引
一.添加索引,提交文档 1.如图,我的xml文档有predicate.object字段,这些在Solr配置文档里没有,所以xml文档提交不了 2.在F:\solr-4.10.0\example\sol ...
随机推荐
- RabbitMQ官方教程五 Topic(GOLANG语言实现)
在上一教程中,我们改进了日志记录系统. 我们没有使用只能进行虚拟广播的fanout交换器,而是使用直接交换器,并有可能选择性地接收日志. 尽管使用直接交换改进了我们的系统,但它仍然存在局限性-它不能基 ...
- Python机器学习基础教程-第1章-鸢尾花的例子KNN
前言 本系列教程基本就是摘抄<Python机器学习基础教程>中的例子内容. 为了便于跟踪和学习,本系列教程在Github上提供了jupyter notebook 版本: Github仓库: ...
- 使用第三方Java类库jaudiotagger完成Flac音频文件metadata(元数据)的读和修改
最近需要使用Java Swing做个读写Flac格式音频文件的小GUI工具,虽然Mp3tag完全可以完成Flac的读写(编辑)任务,但是为了简化工作流程(编辑Flac信息后调用其它系统的接口完成部分信 ...
- SSH连接服务器时,长时间不操作就会断开的解决方案
最近在配置服务器相关内容时候,不同的事情导致长时间不操作,页面就断开了连接,不能操作,只能关闭窗口,最后通过以下命令解决. SSH连接linux时,长时间不操作就断开的解决方案: 1.修改/etc/s ...
- eNSP下配置Trunk接口实现跨交换机传递数据
当Trunk端口发送数据帧的时候,当所发送帧的VLAN ID与端口的PVID不同是,检查是否允许该VLAN通过,若允许的话,直接透传,不允许就直接丢弃:当该帧的VLAN ID与端口的PVID相同时,则 ...
- js里typeof和instanceof和箭头表达式要注意的地方,以及其他
如果学过类似C#这样的语言,然后定义两个类class Mu{}和class Ku{},那么显然typeof Mu != typeof Ku的,但是在js里则不是这样,对于Mu和Ku的对象进行typeo ...
- 2019牛客暑期多校训练营(第四场)A meeting(dfs或dp,dp待更新)
示例1: 输入: 4 21 23 13 42 4 输出:2 说明: They can meet at place 1 or 3. 题意:从K个点到达不联通图某个点需要的最短时间,这个最短时间是这K个人 ...
- Sping Aop日志实现(五)--使用Mybatis注解保存日志
- 案例(2)-- 线程不安全对象(SimpleDateFormat)
问题描述: 1.系统偶发性抛出异常:java.lang.NumberFormatException: multiple points ,追溯源头抛出的类为:SimpleDateFormat 问题的定位 ...
- processon使用教程
原文地址:https://www.cnblogs.com/yangliheng/p/6082250.html 一.引言 作为一名IT从业者,不仅要有扎实的知识储备,出色的业务能力,还需要具备一定的软实 ...