当前位置:网站首页>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
边栏推荐
- CPU设计实战-第四章实践任务二用阻塞技术解决相关引发的冲突
- Mongdb learning notes
- go学习 ------jwt的相关知识
- 两个BI开发,3000多张报表?如何做的到?
- MongDB学习笔记
- Detailed explanation of usememo, memo, useref and other relevant hooks
- 一键更改多个文件名字
- PHP high concurrency and large traffic solution (PHP interview theory question)
- Easyocr character recognition
- 超级哇塞的快排,你值得学会!
猜你喜欢
Photoshop plug-in action related concepts actionlist actiondescriptor actionlist action execution load call delete PS plug-in development
P6183 [USACO10MAR] The Rock Game S
Huawei Hubble incarnation hard technology IPO harvester
1330:【例8.3】最少步数
Change multiple file names with one click
P1451 求细胞数量/1329:【例8.2】细胞
Ctfshow web entry command execution
729. 我的日程安排表 I :「模拟」&「线段树(动态开点)」&「分块 + 位运算(分桶)」
No one consults when doing research and does not communicate with students. UNC assistant professor has a two-year history of teaching struggle
Talk about your understanding of microservices (PHP interview theory question)
随机推荐
MongDB学习笔记
What are CSRF, XSS, SQL injection, DDoS attack and timing attack respectively and how to prevent them (PHP interview theory question)
[C question set] of Ⅷ
Does maxcompute have SQL that can query the current storage capacity (KB) of the table?
Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!
你童年的快乐,都是被它承包了
Fr exercise topic - simple question
Ctfshow web entry explosion
Ecotone technology has passed ISO27001 and iso21434 safety management system certification
CPU design related notes
百亿按摩仪蓝海,难出巨头
两个BI开发,3000多张报表?如何做的到?
选择排序和冒泡排序
There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba
NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
useMemo,memo,useRef等相关hooks详解
【NVMe2.0b 14-9】NVMe SR-IOV
【招聘岗位】软件工程师(全栈)- 公共安全方向
MySQL之CRUD
Un week - end heureux