1.pymysql增刪改mysql
必定要有commit()sql
import pymysql username = input("請輸入用戶名:") pwd = input("請輸入密碼:") conn = pymysql.connect( host="localhost", port=3306, database="db2", user="root", password="", charset="utf8" ) sql = "insert into userinfo(name,pwd) values(%s,%s)" cur = conn.cursor() res = cur.execute(sql,[username,pwd]) print(res) conn.commit() 必定要加,不然數據庫中不會保存,只會在緩存中保存。若是以前沒有加這句,提交了一次,雖然沒有提交上,可是id的自增確保留下來了 cur.close() conn.close()
2.pymysql查詢數據庫
fetchone()緩存
fetchmany(size)ide
fetchall()函數
import pymysql conn = pymysql.connect( host="localhost", port=3306, database="db2", user="root", password="", charset="utf8" ) sql = "select * from userinfo" cur = conn.cursor(cursor=pymysql.cursors.DictCursor)#會以字典的形式返回查詢信息 res = cur.execute(sql) print(res) # ret = cur.fetchone() # print(ret) # ret = cur.fetchone() # print(ret) # ret = cur.fetchone() # print(ret) # ret = cur.fetchone() # print(ret) # ret = cur.fetchone() # print(ret) # ret = cur.fetchone()#單次查詢若是超出範圍會返回none # print(ret) # ret = cur.fetchmany(6)#多條查詢超過範圍顯示全部信息 # print(ret) ret = cur.fetchall() print(ret) conn.commit() cur.close() conn.close()
import pymysql conn = pymysql.connect( host="localhost", port=3306, database="db2", user="root", password="", charset="utf8" ) sql = "select * from userinfo" cur = conn.cursor(cursor=pymysql.cursors.DictCursor)#會以字典的形式返回查詢信息 res = cur.execute(sql) print(res) ret = cur.fetchone() print(ret) #relative 相對於原來的位置,若是正數向下移動 反之亦然 #absolute 相對於起始位置,正數向下移動 cur.scroll(-1,mode="relative") ret = cur.fetchone() print(ret) conn.commit() cur.close() conn.close()
3.索引性能
做用:約束+加速查詢fetch
普通索引:create index ix_name on 表名(字段名); spa
做用:加速查詢code
惟一索引:create unique index un_name on 表名(字段名);
做用:約束+加速查詢
主鍵索引:設置主鍵
覆蓋索引:在索引文件中直接獲取數據
例如:select name from big_data where name = "zhang50000"; 對name字段設置了索引
索引合併:把多個單列索引一塊兒使用
select * from big_data where name = "zhang13131" and id = 13131;
聯合索引:
聯合普通索引
聯合惟一索引
聯合主鍵索引
最左前綴:
若是使用組合索引,name email 組合索引以後,查詢
1.name和email 使用索引
2.name 使用索引
3.email 不使用索引
對於同時搜索n個條件時,組合索引的性能好於多個單列索引
組合索引的性能>索引合併的性能
對於索引:
1.建立索引:
注意事項:
1.避免使用select *
2.count(1)或count(列)代替count(*)
3.建立表時儘可能使用char代替varchar
4.表的字段順序固定長度的字段優先
5.組合索引代替多個單列索引(常常使用多個條件查詢)
6.儘可能使用短索引(create index ix_title on tb(title(16)));特殊的數據類型text
7.使用鏈接(join)代替子查詢
8.連表時注意條件類型須要一致
9.索引散列(重複少)不適於用索引,例如性別
2.命中索引
3.正確使用索引
注意事項:
like "%xx"
select * from userinfo where name like "%al";
使用函數:
select * from userinfo where reverse(name) = "zhang333";
or
select * from userinfo where id = 1 or email = "zhang122@qq.com";
類型不一致:
select * from userinfo where name = 999;
!=:
select count(*) from userinfo where name != "zhang";
特別的:若是是主鍵,則仍是會走索引
>:
select * from userinfo where name > "zhang";
特別的:若是是主鍵或索引是整數類型,則仍是會走索引
select * from userinfo where id > 123;
select * from userinfo where num > 123;
order by:
select * from userinfo order by name desc;
當根據索引排序時,選擇的映射若是不是索引,則不走索引
特別的:若是對主鍵排序則仍是會走索引
select * from userinfo order by nid desc;
組合索引最左前綴:
而後組合索引爲:(name,email);
name and email 使用索引
name 使用索引
email 不使用索引
對於建立索引,它是真實存在的,佔用硬盤空間,儘可能不要使用索引
select * from big_data where id > 2000010 limit 10;
select * from (select * from big_data where id > 2000020 limit 30) as A order by id desc limit 10;