zoukankan      html  css  js  c++  java
  • 1031 pycharm操作mysql,索引,慢日志查询

    pycharm操作mysql

    fetchmany(size) # 指定条数
    ferchall() #所有
    ferchone() #一条

    sql注入问题

    `` or 1=1 #`
    可以不用密码就可以成功登入

    产生原因

    因为过于相信用户输入的内容,根本没有做任何的检验

    解决办法

    sql = "select * from user where name=%s and password=%s"
    cursor.execute(sql,(user,pwd))
    

    连接

    连接数据库的参数

    conn = pymysql.connect(host='localhost',user='用户名',password='密码',database='库名',charset='utf8')
    #cursor = conn.coursor() 默认返回的是元组类型
    coursor = conn.cursor(sursor=pymysql.cursors.DictCursor) ##返回的是字典类型
    

    fetchall() : 取出所有的数据,返回的是列表套字典
    fetchone() : 取出一条数据,返回的是字典
    fetchmany(size) : 取出size条数据,返回的是列表套字典

    sql = "insert into user (name,password) values (%s,%s)"
    #cursor.execute(sql,('x','y')) 新增一条数据
    data = [
    	('x1','y1'),
    	('x2','y2'),
    	('x3','y3'),
    ]
    coursor.executemany(sql,data) #新增多条数据
    conn.commit() #表示:提交conn,不提交无法添加数据
    

    sql = "update user set name=%s where id=%s"
    cursor.execute(sql,('新用户名',id))
    conn.commit() #提交conn
    cursor.close() #关闭cursor
    conn.close() #关闭close
    

    sql = "delete from user where id=%s"
    cursor.execute(sql,('用户名',id))
    conn.commit() #提交
    cursor.close() #关闭
    conn.close() #关闭
    

    索引

    索引的作用

    使用索引就是为了提高查询效率

    索引的本质

    一个特殊的文件

    索引的原理

    B+树

    索引的种类

    主键索引

    加速查找,不能重复,不能为空

    唯一索引

    加速查找,不能重复

    联合唯一索引

    unique(name,email)

    普通索引

    加速查找

    联合索引

    index(name,email)

    索引的创建

    主键索引

    新增主键索引

    #方法一(建表时创索引)
    create table 表名(
    	id int auto_increment,
    	primary key (id)
    )charset utf8;
    #方法二(已建表创索引)
    alter table 表名 change id id int auto_increment primary key;
    #方法三(已建表创索引)
    alter table 表名 add primary key;
    

    删除主键索引

    alter table 表名 drop primary key;

    唯一索引

    新增

    # 一(建表时创索引)
    create table 表名(
    	id int auto_increment primary key,
    	字段名 varchar(32) not null default '',
    	unique 索引名(字段名)
    )charset utf8;
    # 二(已建表创索引)
    create unique index 索引名 on 表名(字段名);
    # 三(已建表创索引)
    alter table 表名 add unique index 索引名 (字段名);
    

    普通索引

    新增

    # 一(建表时创索引)
    create table 表名(
    	id int auto_increment primary key,
    	name varchar(32) not null default '',
    	index 索引名 (字段名)
    )charset utf8;
    # 二(已建表创索引)
    create index 索引名 on 表名(字段名);
    # 三(已建表创索引)
    alter table 表名 add index 索引名(字段名);
    

    删除

    alter table 表名 drop index 索引名;

    索引的优缺点

    通过观察*.ibd文件可知:
    优点:索引加快了查询速度
    缺点:添加索引之后,文件也变大了,更占硬盘空间了

    不会命中索引的情况

    不能在SQL语句中,进行四则运算,会降低SQL的查询效率
    
    使用函数,会降低SQL的查询效率
    
    类型不一致,如果列是字符串类型,传入条件也必须是字符串类型,否则会降低查询效率
    
    当根据索引排序时候,select查询的字段如果不是索引,则速度仍然很慢
    	特殊情况:对主键排序,速度还是快的
    
    count(*)会降低查询效率;改为count(1)或count(列)就不会影响了
    
    组合索引最左前缀
    	根据业务场景,在最常用的几个列上添加索引
    	
    	select * from user where name='x' and email='y';
    	
    	如果遇上上述业务,错误做法:
    		index ix_name(name),
    		index ix_email(email)
    		
    	正确做法:
    		index ix_name_email(name,email)
    		
    	如果组合索引为:ix_name_email(name,email)
    	
    		where name='x' and email='y' #命中索引
    		where name='x' #命中索引
    		where email='y' #未命中索引
    
    explain					
    	mysql> explain select * from user where name='zekai' and email='zekai@qq.com'G
    *************** 1. row ***************
    				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   使用到了索引
    

    慢查询日志

    查看慢SQL的相关变量

    +-------------------------+-------------------------------------------+
    | Variable_name           | Value                                     |
    +-------------------------+-------------------------------------------+
    |log_slow_admin_statements| OFF                                       |
    |log_slow_slave_statements| OFF                                       |
    |slow_launch_time         | 2                                         |
    |slow_query_log           | OFF ##默认关闭慢SQl查询日志,on               |
    |slow_query_log_file      | D:mysqldataDESKT.log ###记录存储的盘地址  |
    +-------------------------+-------------------------------------------+
    

    配置慢SQL的变量

    set global 变量名 = 值;
    set global slow_query_log = on;
    set global slow_query_log_file = "D:/mysql/data/slow.log";
    ser global long_query_time = 1;

  • 相关阅读:
    Android 中常用代码片段
    查看oracle中的中文所占字节数
    order by 中 使用decode
    Oracle select 中case 的使用以及使用decode替换case
    pyqt5 'QWidget' object has no attribute 'setCentralWidget'(转)
    程序员之路:python3+PyQt5+pycharm桌面GUI开发(转)
    QT5入门之23 -QT串口编程(转)
    xpath-helper: 谷歌浏览器安装xpath helper 插件
    mysql给root开启远程访问权限
    Vmware无法获取快照信息 锁定文件失败
  • 原文地址:https://www.cnblogs.com/793564949liu/p/11774111.html
Copyright © 2011-2022 走看看