MySQL进阶:存储结构和索引

-- 存储引擎

-- 查看建表语句  默认存储引擎:ENGINE=InnoDB
show create table account;

-- 展示当前数据库支持的存储引擎
show engines;

-- 创建 my_myisam 表,存储引擎为myisam
create table my_myisam(
    id int primary key auto_increment comment'自增主键',
    name varchar(30) not null
) engine = MYISAM;

-- 创建 my_memory 表,存储引擎为memory
create table my_memory(
    id int primary key auto_increment comment'自增主键',
    name varchar(30) not null
) engine = memory;

show create table my_memory;

-- 更改表的存储引擎
alter table my_memory engine = innodb;

drop table my_memory;
drop table my_myisam;


-- 1.default engine innodb(默认存储引擎)
/*
 特点:


 */


-- 2.myisam存储引擎
/*
 特点:

 */

use test;

select * from employee;
-- 索引

-- 展示指定表的索引
show index from employee;

-- 创建指定表指定列的索引
create index employee_IDX on employee (age desc );
create index emp_idx on employee(salary);

show index from employee;


-- 删除索引
drop index employee_IDX on test.employee;
drop index emp_idx on employee;
show index from employee;
drop index employee_IDX on employee;

-- 修改索引
alter table employee rename index employee_IDX to emp_idx;

--

-- ①可以大大加快数据的检索速度,这也是创建索引的最主要原因。
-- ②可以加速表和表之间的连接,特别是在实现数据的参考完整性方面特别有意义。
-- ③在使用分组和排序子句进行数据检索时,同样可以显著减少查询中分组和排序的时间。

use test;

-- 显示各常用语句的使用次数
show global status  like 'com_______';

select * from  employee;


-- 性能分析: explain
-- 注意possible key是可能用到的索引,key是实际用到的索引
explain select * from employee;
explain select * from employee where id = 7;   -- 10ms,没有设置索引,默认添加主键索引
explain select * from employee where name = '王天逸';   -- 23ms


-- 索引使用法则:最左前缀(对于联合索引)
select * from employee;
create index emp_age_de_sa_index on employee(age,depart,salary);   -- 在表employee上创建联合索引
show index from employee;
-- 接下来关注key_len索引长度
select * from employee where age = 31 and depart = '宣传部' and salary >=10000;
explain select * from employee where age = 31 and depart = '宣传部' and salary >=10000;  -- 132
explain select * from employee where age = 31 and depart = '宣传部';   -- 127
explain select * from employee where age = 31;    -- 5
explain select * from employee where depart = '宣传部' and salary >=10000;  -- null, 而且key和possible key也是null说明没有用到索引

/*
如果联合索引设置在了A,B,C,(....)等字段上,(注意,这个顺序是指字段在表里的顺序),
那么使用select 语句的时候在where条件中必须用到A,否则在执行时所有字段上的索引会失效。
如果使用了A,没有用到B字段的条件限制,那么B,以及以后的C,D...字段上的索引都会失效。
如果使用了A,B没有C,规律以此类推。
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快阁东西倚晚晴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值