当前位置:网站首页>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
边栏推荐
- 【数组和进阶指针经典笔试题12道】这些题,满足你对数组和指针的所有幻想,come on !
- Stop B makes short videos, learns Tiktok to die, learns YouTube to live?
- GPS原始坐标转百度地图坐标(纯C代码)
- How to solve the problem of garbled code when installing dependency through NPM or yarn
- Interpretation of Apache linkage parameters in computing middleware
- Machine learning notes - gray wolf optimization
- Cartoon: programmers don't repair computers!
- 想问下大家伙,有无是从腾讯云MYSQL同步到其他地方的呀?腾讯云MySQL存到COS上的binlog
- MongDB学习笔记
- Magic methods and usage in PHP (PHP interview theory questions)
猜你喜欢
PyTorch二分类时BCELoss,CrossEntropyLoss,Sigmoid等的选择和使用
Huawei Hubble incarnation hard technology IPO harvester
CODING DevSecOps 助力金融企业跑出数字加速度
There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba
Interpretation of Apache linkage parameters in computing middleware
选择排序和冒泡排序
【NVMe2.0b 14-9】NVMe SR-IOV
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
爱可可AI前沿推介(7.5)
How to paste the contents copied by the computer into mobaxterm? How to copy and paste
随机推荐
[detailed explanation of Huawei machine test] happy weekend
Calculate weight and comprehensive score by R entropy weight method
Cartoon: programmers don't repair computers!
Two Bi development, more than 3000 reports? How to do it?
Where is the operation of convertible bond renewal? Is it safer and more reliable to open an account
Long list optimized virtual scrolling
maxcompute有没有能查询 表当前存储容量的大小(kb) 的sql?
CPU design related notes
当代人的水焦虑:好水究竟在哪里?
How can I quickly check whether there is an error after FreeSurfer runs Recon all—— Core command tail redirection
Ctfshow web entry explosion
There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba
useMemo,memo,useRef等相关hooks详解
CPU design practice - Chapter 4 practice task 3 use pre delivery technology to solve conflicts caused by related issues
R 熵权法计算权重及综合得分
Coding devsecops helps financial enterprises run out of digital acceleration
What are the domestic formal futures company platforms in 2022? How about founder metaphase? Is it safe and reliable?
危机重重下的企业发展,数字化转型到底是不是企业未来救星
1330: [example 8.3] minimum steps
How to open an account of qiniu securities? Is it safe to open an account?