pymysql的增删改查、索引

1.pymysql增删改mysql

    必定要有commit()sql

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()
View Code

2.pymysql查询数据库

    fetchone()缓存

    fetchmany(size)ide

    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()
View Code
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()
View Code

3.索引性能

  做用:约束+加速查询fetch

    普通索引:create index ix_name on 表名(字段名); spa

      做用:加速查询code

    惟一索引: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;

相关文章
相关标签/搜索