mysql管理工具navicatformysql的索引、頻繁查找的列創建索引、時間、DBA工作、分頁


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='alex@qq.com';
                        
                    組合索引效率 > 索引合並 
                        組合索引
                            - (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 = 'seven@live.com';
                
                
                特別的:當or條件中有未建立索引的列才失效,以下會走索引
                        select * from tb1 where nid = 1 or name = 'seven';
                        select * from tb1 where nid = 1 or name = 'seven@live.com' 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 變量名 =- 配置文件
                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
        - 用類和對象對數據庫操作
        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM