当前位置:网站首页>想进阿里必须啃透的12道MySQL面试题
想进阿里必须啃透的12道MySQL面试题
2022-07-05 14:29:00 【InfoQ】
1. 能说下myisam 和 innodb的区别吗?
2. 说下mysql的索引有哪些吧,聚簇和非聚簇索引又是什么?
create table user(
id int(11) not null,
age int(11) not null,
primary key(id),
key(age)
);
3. 那你知道什么是覆盖索引和回表吗?
explain select * from user where age=1; //查询的name无法从索引数据获取
explain select id,age from user where age=1; //可以直接从索引获取
4. 锁的类型有哪些呢
5. 你能说下事务的基本特性和隔离级别吗?
6. 那你说说什么是幻读,什么是MVCC?
select * from user where id<=3;
update user set name='张三三' where id=1;
select * from user where id<=3 and create_version<=3 and (delete_version>3 or delete_version is null);
- 小明开启事务current_version=6查询名字为'王五'的记录,发现不存在。
- 小红开启事务current_version=7插入一条数据,结果是这样:
- 小明执行插入名字'王五'的记录,发现唯一索引冲突,无法插入,这就是幻读。
7. 那ACID靠什么保证的呢?
8. 那你知道什么是间隙锁吗?
begin;
select * from user where age=20 for update;
begin;
insert into user(age) values(10); #成功
insert into user(age) values(11); #失败
insert into user(age) values(20); #失败
insert into user(age) values(21); #失败
insert into user(age) values(30); #失败
(negative infinity,10],(10,20],(20,30],(30,positive infinity)
9. 那分表后的ID怎么保证唯一性的呢?
- 设定步长,比如1-1024张表我们设定1024的基础步长,这样主键落到不同的表就不会冲突了。
- 分布式ID,自己实现一套分布式ID生成算法或者使用开源的比如雪花算法这种
- 分表后不使用主键作为查询依据,而是每张表单独新增一个字段作为唯一主键使用,比如订单表订单号是唯一的,不管最终落在哪张表都基于订单号作为查询依据,更新也一样。
10. 你们数据量级多大?分库分表怎么做的?
11. 分表后非sharding_key的查询怎么处理呢?
- 可以做一个mapping表,比如这时候商家要查询订单列表怎么办呢?不带user_id查询的话你总不能扫全表吧?所以我们可以做一个映射关系表,保存商家和用户的关系,查询的时候先通过商家查询到用户列表,再通过user_id去查询。
- 打宽表,一般而言,商户端对数据实时性要求并不是很高,比如查询订单列表,可以把订单表同步到离线(实时)数仓,再基于数仓去做成一张宽表,再基于其他如es提供查询服务。
- 数据量不是很大的话,比如后台的一些查询之类的,也可以通过多线程扫表,然后再聚合结果的方式来做。或者异步的形式也是可以的。
List<Callable<List<User>>> taskList = Lists.newArrayList();
for (int shardingIndex = 0; shardingIndex < 1024; shardingIndex++) {
taskList.add(() -> (userMapper.getProcessingAccountList(shardingIndex)));
}
List<ThirdAccountInfo> list = null;
try {
list = taskExecutor.executeTask(taskList);
} catch (Exception e) {
//do something
}
public class TaskExecutor {
public <T> List<T> executeTask(Collection<? extends Callable<T>> tasks) throws Exception {
List<T> result = Lists.newArrayList();
List<Future<T>> futures = ExecutorUtil.invokeAll(tasks);
for (Future<T> future : futures) {
result.add(future.get());
}
return result;
}
}
12. 说说mysql主从同步怎么做的吧?
- master提交完事务后,写入binlog
- slave连接到master,获取binlog
- master创建dump线程,推送binglog到slave
- slave启动一个IO线程读取同步过来的master的binlog,记录到relay log中继日志中
- slave再开启一个sql线程读取relay log事件并在slave执行,完成同步
- slave记录自己的binglog
13. 那主从的延迟怎么解决呢?
- 针对特定的业务场景,读写请求都强制走主库
- 读请求走从库,如果没有数据,去主库做二次查询
边栏推荐
- R language ggplot2 visual bar graph: visualize the bar graph through the two-color gradient color theme, and add label text for each bar (geom_text function)
- Thymeleaf 使用后台自定义工具类处理文本
- Faire un clip vidéo auto - média deux fois, comment clip n'est pas considéré comme une infraction
- Pointer operation - C language
- Discussion on memset assignment
- Thymeleaf 常用函数
- Mingfeng medical sprint technology innovation board: annual revenue of 350million yuan, proposed to raise 624million yuan
- Countermeasures of enterprise supply chain management system in UCA Era
- Section - left closed right open
- Don't be unconvinced. Mobile phone function upgrade is strong
猜你喜欢
SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain
Thymeleaf th:with use of local variables
How to choose the appropriate certificate brand when applying for code signing certificate?
如何深入理解“有限状态机”的设计思想?
LeetCode_ 2 (add two numbers)
Why do mechanical engineers I know complain about low wages?
Intelligent supply chain collaboration system solution for daily chemical products industry: digital intelligent SCM supply chain, which is the "acceleration" of enterprise transformation
Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
微帧科技荣获全球云计算大会“云鼎奖”!
魅族新任董事長沈子瑜:創始人黃章先生將作為魅族科技產品戰略顧問
随机推荐
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the coef function to obtain the log odds ratio corresponding to eac
做自媒体视频二次剪辑,怎样剪辑不算侵权
Opengauss database source code analysis series articles -- detailed explanation of dense equivalent query technology (Part 2)
R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
LeetCode_ 69 (square root of x)
How can non-technical departments participate in Devops?
软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】
Use the word "new" to attract curious people
微帧科技荣获全球云计算大会“云鼎奖”!
SSL证书错误怎么办?浏览器常见SSL证书报错解决办法
分享 12 个最常用的正则表达式,能解决你大部分问题
[learning notes] connectivity and circuit of graph
R language uses the polR function of mass package to build an ordered multi classification logistic regression model, and uses the coef function to obtain the log odds ratio corresponding to each vari
Shenziyu, the new chairman of Meizu: Mr. Huang Zhang, the founder, will serve as the strategic adviser of Meizu's scientific and technological products
Postgresql 13 安装
动态规划
R language ggplot2 visualization: gganimate package is based on Transition_ The time function creates dynamic scatter animation (GIF) and uses shadow_ Mark function adds static scatter diagram as anim
Thymeleaf 模板的创建与使用
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million