1031 pycharm operation mysql, indexes, slow query log

pycharm operation mysql

fetchmany (size) # specify the Number
ferchall () # All
ferchone () # a

sql injection problems

` or 1=1 #
You can succeed without a password can log in

cause

Because too believe that user input, do not do any testing

Solution

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

connection

Parameter database connection

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

check

fetchall (): Remove all the data, returns a list of sets of dictionary
fetchone (): Remove a data dictionary is returned
fetchmany (size): Remove the size of data, it returns a list of sets of dictionary

increase

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,不提交无法添加数据

change

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

delete

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

index

The role of the index

It is to use the index to improve query efficiency

The nature of the index

A special file

Principle index

B + Tree

Type index

Primary key index

Find accelerate, can not be repeated, can not be empty

The only index

Find accelerate, can not be repeated

United unique index

unique(name,email)

General index

Find accelerate

Joint index

index(name,email)

Creating an index

Primary key index

New primary key index

#方法一(建表时创索引)
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;

Drop Primary Index

alter table 表名 drop primary key; ### 唯一索引 #### 新增 ```python # 一(建表时创索引) 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 索引名 (字段名); ``` ### 普通索引 #### 新增 ```python # 一(建表时创索引) 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 table name drop index index name; ### 索引的优缺点 通过观察* .ibd` file shows:
Advantages: Index accelerate query speed
Cons: After adding the index file is also larger, more hard disk space the account

It will not hit the index case

不能在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='[email protected]'\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   使用到了索引

Slow query log

View slow SQL-related variables

+-------------------------+-------------------------------------------+
| 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:\mysql\data\DESKT.log ###记录存储的盘地址  |
+-------------------------+-------------------------------------------+

Slow SQL configuration variables

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;

Guess you like

Origin www.cnblogs.com/793564949liu/p/11774111.html