zoukankan      html  css  js  c++  java
  • MySQL-索引

    索引的工作原理及其种类
    一是增加了数据库的存储空间,二是在插入和修改数据时要花费较多的时间(因为索引也要随之变动)。
    索引的实现通常使用B树及其变种B+树。
    二叉树 索引文件 效率 log2N
    检索10次,2的10次方,1024条记录
    查看数据文件
    .frm:表的结构
    .myd:表的数据
    .myi:索引的文件
    通过explain分析执行低效SQL的执行计划
    通过show profile分析SQL
    (1)首先先看MySQL是否支持show profile
    select @@have_profiling;
    (2)如果profiling是关闭的,可以通过set语句在session级别开启profiling
    set profiling=1;
    (3)执行完毕后,可以通过show profile语句查看当前SQL的queryID
    (4)通过show profile for query querID.


    查看是否有索引?
    show index from user1;
    show keys from employee;
     explain select * from employee where id =1G
    索引分类
    主键索引
    alter table 表名 add primary key (索引针对的列)
    唯一索引
    唯一的索引所在的列可以为NULL值。
    唯一的索引所在的列不能为 空字符串
    create unique index 索引名称 on表名(列名)
    普通索引
    先创建表,然后再创建普通索引
    create index 索引名称 on 表名(列)
    全文索引
    fulltext索引,用于全文搜索,只有myisam表类型支持,
    可以从CHAR,VARCHAR和text列中创建,整个列都会被编入索引
    不支持对部分列编索引。
    create table articles(
    id int unsigned auto_increment not null primary key,
    title varchar(200),
    body  text,
    fulltext(title,body)
    )engine=myisam default charset=utf8;

    select * from articles where match(title,body) against('a');




    索引的删除
    alter table 表名 drop index 索引名称

    CREATE TABLE IF NOT EXISTS employee(
    id SMALLINT UNSIGNED ,
    username VARCHAR(20) NOT NULL,
    depId TINYINT UNSIGNED
    )ENGINE=INNODB;

  • 相关阅读:
    【刷题】HDU 6183 Color it
    【刷题】HDU 3435 A new Graph Game
    【刷题】HDU 1853 Cyclic Tour
    【刷题】BZOJ 5418 [Noi2018]屠龙勇士
    【刷题】BZOJ 5415 [Noi2018]归程
    【刷题】BZOJ 4636 蒟蒻的数列
    【刷题】BZOJ 4443 [Scoi2015]小凸玩矩阵
    vue_过滤器: 对要显示的数据进行特定格式化后再显示
    vue_过渡_动画
    vue_实例_组件的生命周期
  • 原文地址:https://www.cnblogs.com/3ddan/p/10361706.html
Copyright © 2011-2022 走看看