1031 Review

pymysql

Introduction

python operation module mysql

installation

pip install pymysql

connection

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

carried out

Implementation of sql statement

cursor.execupt(sql)

check

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

CRUD

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

sql injection

the reason

Too believe that the data entered by the user

Way to solve

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

csrf attack

index

The role of the index

Improve the efficiency of query

Analogy: the dictionary catalog

Dictionary catalog, the first query to the chapter, then summary

Using the underlying data structure

B + Tree

Index essence

It is essentially a special file, but the underlying file special data structure is a B + tree

Category Index

Primary key index

effect

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

increase

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

第二种方法:
    alter table 表名 add change 段名 段名 int auto_increment primary key;

delete

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

然后再删除
alter table 表名 drop primary key;

Scenes

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

The only index

effect

加快查询速度 + 不能重复

increase

第一种方法
    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 (字段名);
    

Delete Index

alter table 表名 drop index 索引名;

Scenes

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

United unique index

effect

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

increase

第一种方法
    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 (字段名,字段名);
    

Delete Index

alter table 表名 drop index 索引名;

Scenes

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

General index

effect

加速查找

increase

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

第二种方法
    alter table 表名 add index 索引名 (字段名);
    
第三种方法
    create index 索引名 on 表名(字段名);
    

delete

alter table 表名 drop index 索引名;

United (combined) Index

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

When will create a joint index?

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

正确的做法:
    index ix_name_email(name, email)

Index hit

Not the index plus the better.

explain

Analysis index hit or miss to explain + \ G formatted output

Under the circumstances the index will not hit

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='[email protected]'                -- 未命中索引

    如果组合索引为:ix_name_email_age (name, email, age):

    where name='zekai' and email='xxx' and age=12;  ---- 命中索引
    where name='zekai' and age=12;              ---- 命中索引
  

Slow log

Sql statement execution time limit

Inquire

show variables like '%slow%';

Setting time of the query

show variables like '%long%';

The reason the investigation of slow sql

1. 将慢sql记录到日志中

2. 获取慢sql,根据慢sql来优化查询效率(加索引或者修改索引)

tee

tee address .log

All the subsequent operations to a specified location records

tee F:\.log
输入的所有sql语句都将被记录

Guess you like

Origin www.cnblogs.com/fwzzz/p/11779554.html