当前位置:网站首页>Common MySQL interview questions (1) (written MySQL interview questions)
Common MySQL interview questions (1) (written MySQL interview questions)
2022-07-05 15:05:00 【Back end regular developers】
List of articles
- 1. Optimization you can think of MYSQL Database method
- 2. The application has a lot of data , Mainly for users , There are few update operations , Then the storage engine uses MyISAM Good or not InnoDB good , Why? ?
- 3. Write an infinite classification table structure , There is no limit to the number of tables ( Table field name and description are ok , No scripts are required )
- 4. surface user, Field USER_ ID,USER_ NAME,BIRTHDAY
- 5. One sheet adopted Innodb Of User surface , among id Primary key ,name For general index , Try to analyze the data structure of the index , The following two statements ( Both return a record ) What are the differences in the retrieval process ?
- 6. There is a statistical website for the needs of independent visitors , More than one million traffic , Such as IP For the identity , You can view the real-time of the day or specify a certain day IP Count ( Need to be heavy ), Only use MySQL To achieve , that :
- 7.mySql Execution order of
1. Optimization you can think of MYSQL Database method
- Avoid using select *, The query is specific to the field
- The design data sheet is reasonable , Reduce linked table queries
- Build a reasonable index
- Read write separation of database
- Use redis Cache to reduce the query pressure of the database
2. The application has a lot of data , Mainly for users , There are few update operations , Then the storage engine uses MyISAM Good or not InnoDB good , Why? ?
Use MyISAM, because MyISAM Has a higher insert 、 Query speed .
- Storage structure :MyISAM Store three files on disk . and InnoDB All tables are stored in the same data file , It's usually 2GB
- Transaction support :MyISAM No transaction support .InnoDB Provide transaction support transaction .
- Watch lock difference :MyISAM Only table level locks are supported .InnoDB Support transaction and row level locks .
- Full-text index :MyISAM Support FULLTEXT Full-text index of type ( Not applicable to Chinese , So want to use sphinx Full text indexing engine ).InnoDB I won't support it .
- The specific number of rows in the table :MyISAM Holds the total number of rows in a table , Inquire about count(*) Soon .InnoDB The total number of rows in a table is not saved , You have to recalculate .
3. Write an infinite classification table structure , There is no limit to the number of tables ( Table field name and description are ok , No scripts are required )
id、pid、name、orders、create_time、update_time
4. surface user, Field USER_ ID,USER_ NAME,BIRTHDAY
USER_ID(int10) | USER_NAME(varchar100) | BIRTHDAY(varchar100) |
---|---|---|
1 | Name01 | 10 September 1980 |
2 | Name01 | 1988-02-09 |
3 | Name03 | 1112564781 |
4 | Name04 | 2/3/1983 |
- Write sql The script takes out two pieces of data and presses USER_ID Reverse order
SELECT * FROM user
ORDER BY user_id DESC LIMIT 2;
- Write sql The script can randomly fetch a piece of data
SELECT * FROM
user
ORDER BY RAND() LIMIT 1;SELECT * FROM user AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(user_id) FROM
user
)-(SELECT MIN(user_id) FROM user))+(SELECT MIN(user_id) FROM user)) AS user_id) AS t2 WHERE t1.user_id >= t2. user_id ORDER BY t1. user_id LIMIT 1;SELECT * FROM user WHERE user_id >= ((SELECT MAX(user_id) FROM user)-(SELECT MIN(user_id) FROM user)) * RAND() + (SELECT MIN(user_id) FROM user) LIMIT 1
- Write sql The data extracted by the script USER_NAME Can't have the same name
SELECT DISTINCT user_name FROM `user`;
( Remove keywords distinct. Note that this keyword must be next to select keyword .)
- Write sql The data extracted by the script BIRTHDAY Greater than 1983-01-01
answer ( Incomplete ):
SELECT * FROM `user` WHERE (birthday LIKE '%-%-%' AND STR_TO_DATE(birthday, '%Y-%m-%d') > '1983-01-01') OR
(birthday LIKE '%/%/%' AND STR_TO_DATE(birthday, '%d/%m/%Y') > '1983-01-01');
5. One sheet adopted Innodb Of User surface , among id Primary key ,name For general index , Try to analyze the data structure of the index , The following two statements ( Both return a record ) What are the differences in the retrieval process ?
Sql 1: SELECT id, name, address FROM User WHERE name
"smith’
Sql 2: SELECT id, name, address FROM User WHERE id = 1;
sql2 Higher performance .
because name For general index , Secondary index , because Innodb The data of the secondary index in stores the value of the clustered index , Therefore, when using auxiliary index for query , When the data is found , You also need to use the primary key to query the clustered index again , Therefore, the index will be queried twice , and sql2 You only need to query once .
6. There is a statistical website for the needs of independent visitors , More than one million traffic , Such as IP For the identity , You can view the real-time of the day or specify a certain day IP Count ( Need to be heavy ), Only use MySQL To achieve , that :
- How do you design tables and indexes ?( written words 、sql All possible , The scheme is as efficient as possible )
IP Address translation to plastic storage , Sub table by date , With (IP, date ) As a joint index .
- How to store data , How to query the real-time data of that day and that day ?( Write sql sentence )
redis Queue cache -> MySQL Bulk warehousing
Inquiry day :
select count(distinct ip) as ip_num from log_{yyyymmdd} where TO_DAYS(add_time)=TO_DAYS(now());
Query a day :
select count(distinct ip) as ip_num from visit_log_{yyyymmdd} where TO_DAYS(add_time) = {yyyy-mm-dd};
7.mySql Execution order of
mysql perform sql The order is from From Start , The following is the sequence of execution
FROM table1 left join table2 on take table1 and table2 The data in produces the Cartesian product , Generate Temp1
JOIN table2 So first of all, make sure that the table , And then determine the correlation conditions
ON table1.column = table2.columu Determine the binding conditions of the table from Temp1 Generate intermediate tables Temp2
WHERE On the middle watch Temp2 The results are filtered Generate intermediate tables Temp3
GROUP BY On the middle watch Temp3 Grouping , Generate intermediate tables Temp4
HAVING Aggregate the grouped records Generate intermediate tables Temp5
SELECT On the middle watch Temp5 Do column filtering , Generate intermediate tables Temp6
DISTINCT On the middle watch Temp6 Deduplication , Generate intermediate tables Temp7
ORDER BY Yes Temp7 Sort data in , Generate intermediate tables Temp8
LIMIT On the middle watch Temp8 paging , Generate intermediate tables Temp9
边栏推荐
- 安装配置Jenkins
- Stop B makes short videos, learns Tiktok to die, learns YouTube to live?
- Ecotone technology has passed ISO27001 and iso21434 safety management system certification
- [C question set] of Ⅷ
- 12 MySQL interview questions that you must chew through to enter Alibaba
- There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba
- qt creater断点调试程序详解
- Select sort and bubble sort
- 社区团购撤城“后遗症”
- 选择排序和冒泡排序
猜你喜欢
华为哈勃化身硬科技IPO收割机
Your childhood happiness was contracted by it
黑马程序员-软件测试-10阶段2-linux和数据库-44-57为什么学习数据库,数据库分类关系型数据库的说明Navicat操作数据的说明,Navicat操作数据库连接说明,Navicat的基本使用,
Implement a blog system -- using template engine technology
[detailed explanation of Huawei machine test] character statistics and rearrangement
机器学习笔记 - 灰狼优化
Under the crisis of enterprise development, is digital transformation the future savior of enterprises
sql server学习笔记
Two Bi development, more than 3000 reports? How to do it?
Thymeleaf uses background custom tool classes to process text
随机推荐
sql server char nchar varchar和nvarchar的区别
Under the crisis of enterprise development, is digital transformation the future savior of enterprises
Two Bi development, more than 3000 reports? How to do it?
Ecotone technology has passed ISO27001 and iso21434 safety management system certification
社区团购撤城“后遗症”
12 MySQL interview questions that you must chew through to enter Alibaba
机器学习笔记 - 灰狼优化
Ctfshow web entry explosion
FR练习题目---综合题
如何将电脑复制的内容粘贴进MobaXterm?如何复制粘贴
[recruitment position] infrastructure software developer
Easyocr character recognition
Brief introduction of machine learning framework
No one consults when doing research and does not communicate with students. UNC assistant professor has a two-year history of teaching struggle
Un week - end heureux
GPS original coordinates to Baidu map coordinates (pure C code)
Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
Thymeleaf uses background custom tool classes to process text
Ctfshow web entry command execution
ICML 2022 | 探索语言模型的最佳架构和训练方法