zoukankan      html  css  js  c++  java
  • 1031 回顾

    pymysql

    介绍

    python操作mysql的模块

    安装

    pip install pymysql

    连接

    import
    conn = pymysql
    conn = pymysql.connect(host= 主机名,user = 用户名, password = 密码,database = 数据库名)
        
    cursor = conn.cursor()
        // 返回的是元组中套元组
    cursor = conn.cursor(cursor=pymysql.cursors.dictcursor)
    	// 返回的是列表中套字典
    

    执行

    执行sql语句

    cursor.execupt(sql)
    

    fetchcall()	:获取多个,返回 列表套字典
    
    fetchone()	:获取一个,返回 字典
    
    fetchmany(size)	:获取指定数量,返回 列表套字典
    

    增删改

    sql=''
    cursor.execute(sql,(1,2,3))
    	// 添加1个
    cursor.executemany(sql,[(1,2,3),(1,2,3)])
    	// 添加多个
    conn.commit()
        //必须加commit()
    

    sql注入

    原因

    太相信用户输入的数据

    解决的方式

    cursor.execute(sql,(user,pwd))
    

    csrf攻击

    索引

    索引的作用

    提高查询的效率

    类比:字典中的目录

    字典的目录,先查询到章,再到小结

    底层采用的数据结构

    B+树

    索引本质

    本质上就是一个特殊的文件,只不过这个特殊的文件底层的数据结构是B+树

    索引的分类

    主键索引

    作用

    加快查询速度 + 不能重复 + 不能为空
    

    增加

    第一种方法: (************************)
    	create table user(
    		id int auto_increment primary key//每表必有主键自增id
    		)
        注意: auto_increment依赖 primary key,只删除主键不行
    
    第二种方法:
    	alter table 表名 add change 段名 段名 int auto_increment primary key;
    
    

    删除

    首先要删除auto_increment自增,否则主键删除不掉
    	alter table 表名 add change 段名 段名 int primary key;
    
    然后再删除
    alter table 表名 drop primary key;
    
    

    场景

    一般都是加在 id 这一列
    技术是服务于业务的
    
    

    唯一索引

    作用

    加快查询速度 + 不能重复
    
    

    增加

    第一种方法
    	create table user (
    		id int auto_increment primary key,
    		phone in not null default 0,
    		unique 索引名ix_ph (字段名phone)
    	)
    	
    第二种方法
    	alter table 表名 unique index 索引名(字段名);
    	
    第三种方法
    	create unique index 索引名 on user (字段名);
    	
    
    

    删除索引

    alter table 表名 drop index 索引名;
    
    

    场景

    应用在唯一值的时候,根据自己的业务确定
    
    

    联合唯一索引

    作用

    加快查询速度 + 不能重复
        
    insert into (a,b) values (1,2)
    	// insert into (a,b) values (1,2) 联合添加相同的值不能添加
        // insert into (a,b) values (1,3) 添加一个相同的可以
    
    

    增加

    第一种方法
    	create table user (
    		id int auto_increment primary key,
    		phone in not null default 0,
    		unique 索引名ix_ph (字段名phone,字段名name)
    	)charset utf8;
    	
    第二种方法
    	alter table 表名 unique index 索引名(字段名,字段名);
    	
    第三种方法
    	create unique index 索引名 on user (字段名,字段名);
    	
    
    

    删除索引

    alter table 表名 drop index 索引名;
    
    

    场景

    根据项目或者业务方的需求,灵活的加上联合唯一索引
    
    

    普通索引

    作用

    加速查找
    
    

    增加

    第一种方法
    	create table user (
    		id int auto_increment primary key,
    		phone in not null default 0,
    		index 索引名(字段名)
    	)
    
    第二种方法
    	alter table 表名 add index 索引名 (字段名);
    	
    第三种方法
    	create index 索引名 on 表名(字段名);
    	
    
    

    删除

    alter table 表名 drop index 索引名;
    
    

    联合(组合)索引

    index (name,age)
    加快查询速度,可以重复
    
    

    什么时候会创建联合索引?

    根据公司的业务场景, 在最常用的几列上添加索引
        select * from user where name='zekai' and email='zekai@qq.com';
    						
    果遇到上述业务情况, 错误的做法:
        index ix_name (name),
        index ix_email(email)
    
    正确的做法:
    	index ix_name_email(name, email)
    

    索引命中

    索引并不是加的越多越好.

    explain

    explain 来分析索引命中与否 + G 格式化输出信息

    不会命中索引的情况下

    a. 不能在SQl语句中,进行四则运算, 会降低SQL的查询效率
    				
    b. 使用函数
        select * from tb1 where reverse(email) = 'zekai';
    
    c. 类型不一致
        如果列是字符串类型,传入条件是必须用引号引起来,不然...
        select * from tb1 where email = 999;
        #排序条件为索引,则select字段必须也是索引字段,否则无法命中
    
    d. order by
        select name from s1 order by email desc;
        当根据索引排序时候,select查询的字段如果不是索引,则速度仍然很慢
    
        select email from s1 order by email desc;
        特别的:如果对主键排序,则还是速度很快:
        select * from tb1 order by nid desc;
    
    e. count(1)或count(列)代替count(*)在mysql中没有差别了
    
    f. 组合索引最左前缀
    	如果组合索引为:ix_name_email (name,email) ************
    					
    	where name='zekai' and email='xxxx'       -- 命中索引
    
        where name='zekai'   -- 命中索引
        where email='zekai@qq.com'                -- 未命中索引
    
        如果组合索引为:ix_name_email_age (name, email, age):
    
        where name='zekai' and email='xxx' and age=12;  ---- 命中索引
        where name='zekai' and age=12;              ---- 命中索引
      
    
    

    慢日志

    sql语句执行的时间限制

    查询

    show variables like '%slow%';
    
    

    设置查询的时间

    show variables like '%long%';
    
    

    排查慢sql的原因

    1. 将慢sql记录到日志中
    
    2. 获取慢sql,根据慢sql来优化查询效率(加索引或者修改索引)
    

    tee

    tee 地址.log

    将接下来所有的操作记录到指定的位置

    tee F:.log
    输入的所有sql语句都将被记录
    
    
  • 相关阅读:
    Java设计模式—单例模式
    Java集合框架
    Java进程和线程
    Java IO
    Java异常类
    Java面向对象—抽象类和接口
    Java面向对象—多态
    Java面向对象—继承
    Java面向对象
    Java基础语法
  • 原文地址:https://www.cnblogs.com/fwzzz/p/11779554.html
Copyright © 2011-2022 走看看