py知识(每日更新) 8.5

前情回顾

创建表 create table
# 查询数据
    单表 : select distinct 字段 from 表
        where 条件 group by 字段 having 过滤 order by 字段 limit n,m
    # 多表
        # 连表
            # 内连接
                select * from 表1,表2 where 连接条件
                select * from 表1 inner join 表2 on 连接条件
            # 左外连接
                select * from 表1 left join 表2 on 连接条件
            # 右外连接
                select * from 表1 right join 表2 on 连接条件
            # 全外连接
                select * from 表1 left join 表2 on 连接条件
                union
                select * from 表1 right join 表2 on 连接条件
        # 子查询
             select * from 表 where 条件 = (子查询)
             select * from (子表) as t where 条件 = xxx
             select (子查询出来的单行单列的内容),xxx from 表
# 删
    delete from 表 where 条件
    delete from 表 where 字段 = (子查询)
# 增
    insert into 表(字段,字段,字段) values (值,值,值),(值,值,值)
# 改
    update 表 set 字段=值,字段=值 where 条件
    update 表 set 字段=值,字段=值 where 字段 = (子查询)

1.求各科最高分成绩
select course_id,max(num) from score group by course_id
select t.course_id,c.cname,t.max_num from course c inner join
            (select course_id,max(num) max_num from score group by course_id) as t
            on c.cid = t.course_id;

2.求李平老师所教课程的最高分
select c.cid,max(num) from course c,teacher t,score s
where s.course_id = c.cid and c.teacher_id = t.tid
and t.tname = '李平老师' group by c.cid;

先获取李平老师教的课程
select * from teacher t inner join course c on c.teacher_id = t.tid;

select cid,cname from teacher t inner join course c on c.teacher_id = t.tid
where tname='李平老师';

select t.cid,t.cname,max(num) as max_num from score s right join
(select cid,cname from teacher t inner join course c on c.teacher_id = t.tid
where tname='李平老师') as t on s.course_id = t.cid group by t.cid;

数据库索引

索引原理
    innodb索引
        聚集索引 只有一个主键
        辅助索引 除了主键之外所有的索引都是辅助索引
        回表: 只查询一个索引并不能解决查询中的问题,还需要到具体的表中去获取正行数据
    myisam索引
        辅助索引 除了主键之外所有的索引都是辅助索引

索引的种类
    priamry key 的创建自带索引效果       非空 + 唯一 + 聚集索引
    unique 唯一约束的创建也自带索引效果   唯一 + 辅助索引
    index  普通的索引                   辅助索引

# 创建索引
    create index ind_name on 表(name);
# 删除索引
    drop index ind_name on 表;

# 索引的优缺点
    优点 : 查找速度快
    缺点 : 浪费空间,拖慢写的速度
        不要在程序中创建无用的索引


# 创建了索引之后的效率大幅度提高
# 文件所占的硬盘资源也大幅度提高

# 正确使用索引
    # 1.所查询的列不是创建了索引的列
    # 2.在条件中不能带运算或者函数,必须是"字段 = 值"
    # 3.如果创建索引的列的内容重复率高也不能有效利用索引
        重复率不超过10%的列比较适合做索引
    # 4.数据对应的范围如果太大的话,也不能有效利用索引
        between and > < >= <= != not in
    # 5.like如果把%放在最前面也不能命中索引
    # 6.多条件的情况
        and 只要有一个条件列是索引列就可以命中索引
        or  只有所有的条件列都是索引才能命中索引
    # 7.联合索引
        在多个条件相连的情况下,使用联合索引的效率要高于使用单字段的索引
        where a=xx and b=xxx;
        对a和b都创建索引 - 联合索引
        create index ind_mix on s1(id,email)

        1.创建索引的顺序id,email 条件中从哪一个字段开始出现了范围,索引就失效了
        select * from s1 where id=1000000 and email like 'eva10000%'  命中索引
        select count(*) from s1 where id > 2000000 and email = 'eva2000000'  不能命中索引

        2.联合索引在使用的时候遵循最左前缀原则
        select count(*) from s1 where email = 'eva2000000@oldboy';

        3.联合索引中只有使用and能生效,使用or失效
字段 能够尽量的固定长度 就固定长度
    varchar

其他内容

# mysql 神器 explain
    查看sql语句的执行计划
    explain select * from s1 where id < 1000000;
    是否命中了索引,命中的索引的类型

# 两个名词
    覆盖索引 using index
        select count(id) from 表;
        select id from 表 where id <20;
    # 索引合并
        创建的时候是分开创建的
        用的时候临时和在一起了
        using union 表示索引合并

# 知道mysql可以开启慢日志
    慢日志是通过配置文件开启
    如果数据库在你手里 你自己开
    如果不在你手里 你也可以要求DBA帮你开

# 7表联查速度慢怎么办?
    1.表结构
        尽量用固定长度的数据类型代替可变长数据类型
        把固定长度的字段放在前面
    # 2.数据的角度上来说
        如果表中的数据越多 查询效率越慢
            # 列多 : 垂直分表
            # 行多 : 水平分表
    # 3.从sql的角度来说
        1.尽量把条件写的细致点儿 where条件就多做筛选
        2.多表尽量连表代替子查询
        3.创建有效的索引,而规避无效的索引
    # 4.配置角度上来说
        开启慢日志查询 确认具体的有问题的sql
    # 5.数据库
        读写分离
            解决数据库读的瓶颈

# 数据表\库的导入导出
    # 备份表 :homwork库中的所有表和数据
        mysqldump -uroot -p123 homework > D:\xxx\xxxx\a.sql
        # 备份单表
        mysqldump -uroot -p123 homework course > D:\xxx\xxxx\a.sql
    # 备份库 :
        mysqldump -uroot -p123 --databases homework > D:\xxx\xxxx\db.sql

    # 恢复数据:
        进入mysql 切换到要恢复数据的库下面
        sourse D:\xxx\xxxx\a.sql

# 开启事务,给数据加锁
    begin;
    select id from t1 where name = 'alex' for update;
    update t1 set id = 2 where name = 'alex';
    commit;

猜你喜欢

转载自www.cnblogs.com/lyoko1996/p/11328920.html
8.5