关系型数据库--必知必会知多少

洲行

关注

阅读 37

2022-05-03

目录

关系型数据库主要:

如何设计一个关系型数据库:

什么样的信息能成为索引

索引的数据结构

二叉查找树:

对半搜索

B Tree规则:

B+树是B树的变体,其定义基本与B数相同,除了:

B+Tree更适合用来做存储索引:

Hash索引:

BitMap索引是个神奇(oracle):

密集索引和稀疏索引的区别

InnoDB:

为什么要使用索引

什么样的信息能成为索引

索引的数据结构

mysql其他衍生

如何定位并优化慢查询sql

联合索引的最左匹配原则的成因

索引是建立的越多越好吗

锁模块

MyISAM与InnoDB关于锁方面的区别是什么

MyISAM适合的场景:

InnoDB适合的场景:

数据库锁的分类

数据库事务的四大特性ACID

事务并发访问引起的问题以及如何避免

InnoDB可重复读隔离级别下如何避免幻读

当前读和快照读

RC、RR级别下的InnoDB的非阻塞读如何实现

next-key锁(行锁+gap锁)

对主键索引或者唯一索引会用Gap锁吗

Gap锁用在非唯一索引或者不走索引的当前读中

关键语法

group by

having 


关系型数据库主要:

如何设计一个关系型数据库:

什么样的信息能成为索引

索引的数据结构

二叉查找树:

对半搜索

B Tree规则:

B+树是B树的变体,其定义基本与B数相同,除了:

B+Tree更适合用来做存储索引:

Hash索引:

BitMap索引是个神奇(oracle):

密集索引和稀疏索引的区别

InnoDB:

为什么要使用索引

什么样的信息能成为索引

索引的数据结构

mysql其他衍生

如何定位并优化慢查询sql

1.根据慢日志定位慢查询sql
2.使用explain等工具分析sql
3.修改sql或者尽量让sql走索引
​
show variables like '%quer%';查询慢sql配置
show status like '%slow_queries%'; 查询慢sql条数
​
set global show_query_log=on;
set global long_query_time=1;
​
Explain关键字段:
1.type:
system>const>eq_ref>ref>fulltext>ref_or_null>index_merge>unique_subquery>index_subquery>range>index>all
2.extra:
filesort外部索引排序
temporary临时表
​
explain select name from person_info_large order by name desc;
explain select account from person_info_large order by account desc;
select account from person_info_large order by account desc;
alter table person_info_large add index idx_name(name);
select count(id) from person_info_large;
select count(id) from person_info_large force index(primary);

联合索引的最左匹配原则的成因

索引是建立的越多越好吗

锁模块

MyISAM与InnoDB关于锁方面的区别是什么

1.MyISAM默认用的是表级锁,不支持行级锁
2.InnoDB默认用的是行级锁,也支持表级锁
​
select count(*) from person_info_large;
select count(*) from person_info_myisam;
select *from person_info_myisam where id between 1 and 2000000;
​
lock tables person_info_myisam read |write;
unlock tables;
update person_info_large set title ="test" where id =1;
show variables like 'autocommit';
set autocommit =0;#关闭自动提交
update person_info_large set title="test4" where id =4;
commit;
​
共享锁与排它锁
先读后写

MyISAM适合的场景:

InnoDB适合的场景:

数据库锁的分类

1.按锁的粒度划分,可分为表级锁、行级锁、页级锁
2.按锁级别划分,可分为共享锁、排它锁
3.按加锁方式划分,可分为自动锁、显式锁
4.按操作划分,可分为DML锁、DDL锁
5.按使用方式划分,可分为乐观锁、悲观锁
​
show variables like 'autocommit';
select version from test_innodb where id =2;
update test_innodb set money =123,version =0+1 where version=0 and id =2;

数据库事务的四大特性ACID

事务并发访问引起的问题以及如何避免

1.更新丢失--mysql所有事务隔离级别在数据库层面上均可避免
2.脏读--READ-COMMITTED事务隔离级别以上可避免
​
select @@tx_isolation;
set session transaction isolation level read uncommitted;
start transaction;
update account_innodb set balance =1000-100 where id =1;
select *from account_innodb where id =1;
rollback;
set session transaction isolation level read committed;#oracle默认级别
​
3.不可重复读--REPEATABLE-READ事务隔离级别以上可避免
select @@tx_isolation;
set session transaction isolation level repeatable read;
​
4.幻读--SERIALIZABLE事务隔离级别可避免
select @@tx_isolation;
set session transaction isolation level serializable read;

InnoDB可重复读隔离级别下如何避免幻读

当前读和快照读

1.当前读:select...lock in share mode,select...for update
2.当前读:update,delete,insert
3.快照读:不加锁的非阻塞读,select
select @@tx_isolation;
set session transaction isolation level repeatable read;
start transaction;
select * form account_innodb where id =2;
select * form account_innodb where id =2 lock in share mode;
commit;

RC、RR级别下的InnoDB的非阻塞读如何实现

next-key锁(行锁+gap锁)

对主键索引或者唯一索引会用Gap锁吗

Gap锁用在非唯一索引或者不走索引的当前读中

关键语法

group by

1.满足“SELECT子句中的列名必须为分组列或列函数”
2.列函数对于group by子句定义的每个组各返回一个结果
​
查询所有同学的学号、选课数、总成绩
select student_id,count(course_id),sum(score) from score group by student_id;
​
查询所有同学的学号、姓名、选课数、总成绩
select s.student_id,stu.name,count(s.course_id),sum(s.score)
from
  score s,
  student stu
where
s.student_id=stu.student_id
group by s.student_id;

having

1.通常与group by子句一起使用
2.where过滤行,having过滤组
3.出现在同一sql的顺序:where>group by>having
​
查询平均成绩大于60分的同学的学号和平均成绩
select student_id,avg(score)
from score
group by student_id
having avg(score)>60;
​
查询没有学全所有课的同学的学号、姓名
select stu.student_id,stu.name
from
student stu,
score s
where stu.student_id=s.student_id
group by s.student_id
having count(*)<
(
select count(*) from course
)

精彩评论(0)

0 0 举报