mysql数据库(day6)索引,ORM框架

参考博客:
http://www.cnblogs.com/wupeiqi/articles/5713323.html
http://www.cnblogs.com/wupeiqi/articles/5716963.html
内容回顾:

参考博客:
    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
            - 关闭游标
            - 关闭连接
View Code

 

 

索引参考:www.cnblogs.com/wupeiqi/articles/5713323.html

索引优化参考

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

今日内容:
    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 创建额外文件(某种格式存储)
                    
                mysql索引种类(某种格式存储):
                    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' -like不要用(如果数据量比较大的时候)
                select * from tb1 where email like '%cn';
                
                
            - 使用函数(使用函数也会不走索引,可以在python中实现)
                select * from tb1 where reverse(email) = 'wupeiqi';
                
                
            - or (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                -- 不使用索引
            其他注意事项
            避免使用select *
            - count(1)或count(列) 代替 count(*)
            - 创建表时尽量时 char 代替 varchar
            索引散列值(重复少)不适合建索引,例:性别不适合
View Code

 

执行计划

       执行计划:让mysql预估执行操作(一般正确)
            查询时的快慢排列(type列)
            all < index < range < index_merge < ref_or_null < ref < eq_ref < system/const
            id,email建索引的两列
            拿到sql先看一下执行计划,如果是all全表扫描就慢慢的开始调
            
            慢:
                select * from userinfo3 where name='alex'
                
                explain select * from userinfo3 where name='alex'
                type: ALL(全表扫描)
                    select * from userinfo3 limit 1; limit全表扫描除外
            快:
                select * from userinfo3 where email='alex'
                type: const(走索引)
View Code

 

posted @ 2018-01-14 11:47  wanchenxi  阅读(135)  评论(0编辑  收藏  举报