pycharm操作のmysql
fetchmany(サイズ)#番号を指定
ferchallを()#すべての
ferchone()#Aの
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(サイズ):データのサイズを削除し、それは辞書のセットのリストを返します。
増加
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 +ツリー
タイプインデックス
主キー索引
、加速を繰り返すことができない、空にすることはできません検索
インデックスのみ
、加速検索繰り返すことはできません
ユナイテッド一意のインデックス
ユニーク(名前、メールアドレス)
一般的な指標
加速検索
共同インデックス
インデックス(名前、メールアドレス)
インデックスを作成します
主キー索引
新しい主キー索引
#方法一(建表时创索引)
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; ### 唯一索引 #### 新增 ```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 索引名(字段名); ``` #### 删除
テーブルのテーブル名ドロップインデックスインデックス名を変更; ### 索引的优缺点 通过观察
* .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='[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 使用到了索引
スロークエリログ
遅い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:\mysql\data\DESKT.log ###记录存储的盘地址 |
+-------------------------+-------------------------------------------+
遅いSQL構成変数
設定されたグローバル变量名=值。
上=グローバルslow_query_logを設定します。
設定されたグローバルslow_query_log_file = "D:/mysql/data/slow.log";
SERグローバルlong_query_time = 1;