zoukankan      html  css  js  c++  java
  • MySQL:数据库5

    1.存储引擎

    innodb与MyIASM存储引擎的区别:
      1.innodb 是mysql5.5版本以后的默认存储引擎, 而MyISAM是5.5版本以前的默认存储引擎.
      2.innodb 支持事物,而MyISAM不支持事物
      3.innodb 支持行级锁.而MyIASM 它支持的是并发的表级锁.
      4.innodb 支持外键, 而MyIASM 不支持外键
      5.innodb与MyIASM存储引擎都采用B+TREE存储数据, 但是innodb的索引与数据存储在一个文件中,这种方式我们称之为聚合索引.
        而MyIASM则会单独创建一个索引文件,也就是说,数据与索引是分离开的
      6.在效率方面MyISAM比innodb高,但是在性能方面innodb要好一点.
    2.索引
      1.普通索引 加速查询
      创建:
      create table t1(
      id int not null,
      name varchar(50),
      index idx_id (id)
      )
      通过命令创建
      CREATE index idx_name on t1(name);
      查看索引
      show index from t1;
      删除索引
      drop index ide_id on t1;


      2.唯一索引 加速查询 和 唯一约束(可含一个null 值)
      create table tb2(
      id int not null auto_increment primary key,
      name varchar(50) not null,
      age int not null,
      unique index idx_age (age)
      )

      create unique index idx_age on tb2(age);

      3.主键索引 加速查询 和 唯一约束(不可含null)

      alter table tb3 add primary key(id);

      alter table tb3 drop primary key;

      4.组合索引
      create unique index idx_age on tb2(age,name);

    3. 聚合索引和辅助索引
    总结二者区别:
      相同的是:不管是聚集索引还是辅助索引,其内部都是B+树的形式,即高度是平衡的,叶子结点存放着所有的数据。
      不同的是:聚集索引叶子结点存放的是一整行的信息,而辅助索引叶子结点存放的是单个索引列信息.

    4如何正确使用索引
      #1. 范围查询(>、>=、<、<=、!= 、between...and)
        #1. = 等号
        select count(*) from userinfo where id = 1000 -- 执行索引,索引效率高

        #2. > >= < <= between...and 区间查询
        select count(*) from userinfo where id <100; -- 执行索引,区间范围越小,索引效率越高

        select count(*) from userinfo where id >100; -- 执行索引,区间范围越大,索引效率越低

        select count(*) from userinfo where id between 10 and 500000; -- 执行索引,区间范围越大,索引效率越低

        #3. != 不等于
        select count(*) from userinfo where id != 1000; -- 索引范围大,索引效率低


      #2.like '%xx%'
      #为 name 字段添加索引
      create index idx_name on userinfo(name);

      select count(*) from userinfo where name like '%xxxx%'; -- 全模糊查询,索引效率低
      select count(*) from userinfo where name like '%xxxx'; -- 以什么结尾模糊查询,索引效率低

      #例外: 当like使用以什么开头会索引使用率高
      select * from userinfo where name like 'xxxx%';

      #3. or
      select count(*) from userinfo where id = 12334 or email ='xxxx'; -- email不是索引字段,索引此查询全表扫描

      #例外:当or条件中有未建立索引的列才失效,以下会走索引
      select count(*) from userinfo where id = 12334 or name = 'alex3'; -- id 和 name 都为索引字段时, or条件也会执行索引

      #4.使用函数
      select count(*) from userinfo where reverse(name) = '5xela'; -- name索引字段,使用函数时,索引失效

      #例外:索引字段对应的值可以使用函数,我们可以改为一下形式
      select count(*) from userinfo where name = reverse('5xela');

      #5.类型不一致
      #如果列是字符串类型,传入条件是必须用引号引起来,不然...
      select count(*) from userinfo where name = 454;

      #类型一致
      select count(*) from userinfo where name = '454';

      #6.order by
      #排序条件为索引,则select字段必须也是索引字段,否则无法命中
      select email from userinfo ORDER BY name DESC; -- 无法命中索引

      select name from userinfo ORDER BY name DESC; -- 命中索引

      #特别的:如果对主键排序,则还是速度很快:
      select id from userinfo order by id desc;

    5.组合索引

    组合索引: 是指对表上的多个列组合起来做一个索引.

    最左匹配原则: 从左往右依次使用生效,如果中间某个索引没有使用,那么断点前面的索引部分起作用,断点后面的索引没有起作用;

    select * from mytable where a=3 and b=5 and c=4;
      #abc三个索引都在where条件里面用到了,而且都发挥了作用

    select * from mytable where c=4 and b=6 and a=3;
      #这条语句列出来只想说明 mysql没有那么笨,where里面的条件顺序在查询之前会被mysql自动优化,效果跟上一句一样

    select * from mytable where a=3 and c=7;
      #a用到索引,b没有用,所以c是没有用到索引效果的

    select * from mytable where a=3 and b>7 and c=3;
      #a用到了,b也用到了,c没有用到,这个地方b是范围值,也算断点,只不过自身用到了索引

    select * from mytable where b=3 and c=4;
      #因为a索引没有使用,所以这里 bc都没有用上索引效果

    select * from mytable where a>4 and b=7 and c=9;
      #a用到了 b没有使用,c没有使用

    select * from mytable where a=3 order by b;
      #a用到了索引,b在结果排序中也用到了索引的效果

    select * from mytable where a=3 order by c;
      #a用到了索引,但是这个地方c没有发挥排序效果,因为中间断点了

    select * from mytable where b=3 order by a;
      #b没有用到索引,排序中a也没有发挥索引效果

    6.注意事项
      1. 避免使用select *
      2. 其他数据库中使用count(1)或count(列) 代替 count(*),而mysql数据库中count(*)经过优化后,效率与前两种基本一样.
      3. 创建表时尽量时 char 代替 varchar
      4. 表的字段顺序固定长度的字段优先
      5. 组合索引代替多个单列索引(经常使用多个条件查询时)
      6. 使用连接(JOIN)来代替子查询(Sub-Queries)
      7. 不要有超过4个以上的表连接(JOIN)
      8. 优先执行那些能够大量减少结果的连接。
      9. 连表时注意条件类型需一致
      10.索引散列值不适合建索引,例:性别不适合

    7.查询计划
    预估查询的结果,不太精准
    type : 查询计划的连接类型, 有多个参数,先从最佳类型到最差类型介绍

    性能: null > system/const > eq_ref > ref > ref_or_null > index_merge > range > index > all


    8.慢日志查询
    将mysql服务器中影响数据库性能的相关SQL语句记录到日志文件,
    通过对这些特殊的SQL语句分析,改进以达到提高数据库性能的目的。

    #.查询慢日志配置信息 :
    show variables like '%query%';
    #.修改配置信息
    set global slow_query_log = on;

    # 显示参数  
    show variables like '%log_queries_not_using_indexes';
    # 开启状态
    set global log_queries_not_using_indexes = on;


    #查看慢日志记录的方式
    show variables like '%log_output%';

    #设置慢日志在文件和表中同时记录
    set global log_output='FILE,TABLE';

    #查询时间超过10秒就会记录到慢查询日志中
    select sleep(3) FROM user ;

    #查看表中的日志
    select * from mysql.slow_log;

  • 相关阅读:
    C# 网络斗地主源码开源
    windows 10
    JAVA Eclipse Incorrect line ending found carriage return 怎么办
    JAVA Eclipse中如何简易的实现消息机制
    JAVA Eclipse中的Android程序如何使用线程
    JAVA Eclipse如何重新设置工作空间workspace
    JAVA Eclipse如何重命名包
    JAVA Eclipse如何修改Android程序名称
    JAVA Eclipse如何设置编程环境字体
    JAVA Eclipse如何设置点击按钮切换图片
  • 原文地址:https://www.cnblogs.com/kakawith/p/8505037.html
Copyright © 2011-2022 走看看