1031レビュー

pymysql

入門

Pythonの演算モジュールのmysql

インストール

pymysqlをインストールするPIP

接続

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) :获取指定数量,返回 列表套字典

CRUD

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='[email protected]';
                        
果遇到上述业务情况, 错误的做法:
    index ix_name (name),
    index ix_email(email)

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

インデックスのヒット

ないインデックスプラスより良いです。

説明します

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

    如果组合索引为: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来优化查询效率(加索引或者修改索引)

ティー

ティーアドレスの.log

指定された場所の記録へのすべての後続の操作

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

おすすめ

転載: www.cnblogs.com/fwzzz/p/11779554.html