1. 程式人生 > >8..5數據庫(5)

8..5數據庫(5)

結構 5.7 ria 主鍵 explain x64 查找 前綴 python

數據庫內容有點小懶,把筆記直接copy

這兩天周末,還有cf周年慶,放松一下下,啦啦啦

2018-8-5 20:16:27

繼續看數據庫,還有兩天內容就看完了!覺得數據庫

只要了解就好啦,然後愉快地進入Django

這是數據庫索引連接

http://www.cnblogs.com/wupeiqi/articles/5716963.html

這個裏面有個Memcached 我是看我那本python網絡編程看到的 緩存 吧

http://www.cnblogs.com/wupeiqi/articles/5132791.html

s4day62
參考博客:
    http://www.cnblogs.com/wupeiqi/articles/5713323.html
    http:
//www.cnblogs.com/wupeiqi/articles/5716963.html 內容回顧: 1. 數據庫是什麽 2. MySQL安裝 3. 用戶授權 4. 數據庫操作 - 數據表 - 數據類型 - 是否可以為空 - 自增 - 主鍵 - 外鍵 - 唯一索引 數據行 增 刪 改 查 排序: order by desc
/asc 分組:group by 條件:where 連表: left join right join inner join 臨時表: 通配符 分頁:limit 組合: union 視圖(虛擬) 觸發器 函數 select xx(f) 存儲過程
- 遊標 - 事務 - 結果集+ “返回值” pymysql - 連接 connect(...) - 操作(遊標) - 增刪改 -> commit - 查 -> fetchone,fetchall - SQL註入 - 調用存儲過程: callproc(p1,參數) select @_存儲過程名稱_0 - 關閉遊標 - 關閉連接 今日內容: 1. 索引 作用: - 約束 - 加速查找 索引: - 主鍵索引:加速查找 + 不能為空 + 不能重復 - 普通索引:加速查找 - 唯一索引:加速查找 + 不能重復 - 聯合索引(多列): - 聯合主鍵索引 - 聯合唯一索引 - 聯合普通索引 加速查找: 快: select * from tb where name=asdf select * from tb where id=999 假設: id name email ... ... .. 無索引:從前到後依次查找 索引: id 創建額外文件(某種格式存儲) name 創建額外文件(某種格式存儲) email 創建額外文件(某種格式存儲) create index ix_name on userinfo3(email); name email 創建額外文件(某種格式存儲) 索引種類(某種格式存儲): hash索引: 單值快 範圍 btree索引: btree索引 二叉樹 ========》 結果:快 《======== 建立索引: - a. 額外的文件保存特殊的數據結構、 - b. 查詢快;插入更新刪除慢 - c. 命中索引 select * from userinfo3 where email=asdf; select * from userinfo3 where email like asdf; 慢 ... 主鍵索引: 普通索引: - create index 索引名稱 on 表名(列名,) - drop index 索引名稱 on 表名 唯一索引: - create unique index 索引名稱 on 表名(列名) - drop unique index 索引名稱 on 表名 組合索引(最左前綴匹配): - create unique index 索引名稱 on 表名(列名,列名) - drop unique index 索引名稱 on 表名 - create index ix_name_email on userinfo3(name,email,) - 最左前綴匹配 select * from userinfo3 where name=alex; select * from userinfo3 where name=alex and email=asdf; select * from userinfo3 where email=[email protected]; 組合索引效率 > 索引合並 組合索引 - (name,email,) select * from userinfo3 where name=alex and email=asdf; select * from userinfo3 where name=alex; 索引合並: - name - email select * from userinfo3 where name=alex and email=asdf; select * from userinfo3 where name=alex; select * from userinfo3 where email=alex; 名詞: 覆蓋索引: - 在索引文件中直接獲取數據 索引合並: - 把多個單列索引合並使用 2. 頻繁查找的列創建索引 - 創建索引 - 命中索引 ***** - like %xx select * from tb1 where email like %cn; - 使用函數 select * from tb1 where reverse(email) = wupeiqi; - or select * from tb1 where nid = 1 or name = [email protected]; 特別的:當or條件中有未建立索引的列才失效,以下會走索引 select * from tb1 where nid = 1 or name = seven; select * from tb1 where nid = 1 or name = [email protected] and email = alex - 類型不一致 如果列是字符串類型,傳入條件是必須用引號引起來,不然... select * from tb1 where email = 999; - != select * from tb1 where email != alex 特別的:如果是主鍵,則還是會走索引 select * from tb1 where nid != 123 - > select * from tb1 where email > alex 特別的:如果是主鍵或索引是整數類型,則還是會走索引 select * from tb1 where nid > 123 select * from tb1 where num > 123 - order by select name from tb1 order by email desc; 當根據索引排序時候,選擇的映射如果不是索引,則不走索引 特別的:如果對主鍵排序,則還是走索引: select * from tb1 order by nid desc; - 組合索引最左前綴 如果組合索引為:(name,email) name and email -- 使用索引 name -- 使用索引 email -- 不使用索引 3. 時間 執行計劃:讓mysql預估執行操作(一般正確) all < index < range < index_merge < ref_or_null < ref < eq_ref < system/const id,email 慢: select * from userinfo3 where name=alex explain select * from userinfo3 where name=alex type: ALL(全表掃描) select * from userinfo3 limit 1; 快: select * from userinfo3 where email=alex type: const(走索引) 4. DBA工作 慢日誌 - 執行時間 > 10 - 未命中索引 - 日誌文件路徑 配置: - 內存 show variables like %query% set global 變量名 =- 配置文件 # 告訴mysql 配置文件在哪 下面的是默認的在mysql跟目錄裏面 mysqld --defaults-file=E:\wupeiqi\mysql-5.7.16-winx64\mysql-5.7.16-winx64\my-default.ini my.conf內容: slow_query_log = ON slow_query_log_file = D:/.... 註意:修改配置文件之後,需要重啟服務 5. ******分頁******* a. select * from userinfo3 limit 20,10; b. - 不讓看 - 索引表中掃: select * from userinfo3 where id in(select id from userinfo3 limit 200000,10) - 方案: 記錄當前頁最大或最小ID 1. 頁面只有上一頁,下一頁 # max_id # min_id 下一頁: select * from userinfo3 where id > max_id limit 10; 上一頁: select * from userinfo3 where id < min_id order by id desc limit 10; 2. 上一頁 192 193 [196] 197 198 199 下一頁 select * from userinfo3 where id in ( select id from (select id from userinfo3 where id > max_id limit 30) as N order by N.id desc limit 10 ) c. *****閆龍*****: id不連續,所以無法直接使用id範圍進行查找 2. ORM框架- SQLAlchemy - 用類和對象對數據庫操作

在指定目錄下寫配置文件, my.conf

技術分享圖片

8..5數據庫(5)