当前位置:网站首页>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

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)
1Name0110 September 1980
2Name011988-02-09
3Name031112564781
4Name042/3/1983
  1. 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;

  1. 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

  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 .)

  1. 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 :

  1. 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 .

  1. 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

  1. FROM table1 left join table2 on take table1 and table2 The data in produces the Cartesian product , Generate Temp1

  2. JOIN table2 So first of all, make sure that the table , And then determine the correlation conditions

  3. ON table1.column = table2.columu Determine the binding conditions of the table from Temp1 Generate intermediate tables Temp2

  4. WHERE On the middle watch Temp2 The results are filtered Generate intermediate tables Temp3

  5. GROUP BY On the middle watch Temp3 Grouping , Generate intermediate tables Temp4

  6. HAVING Aggregate the grouped records Generate intermediate tables Temp5

  7. SELECT On the middle watch Temp5 Do column filtering , Generate intermediate tables Temp6

  8. DISTINCT On the middle watch Temp6 Deduplication , Generate intermediate tables Temp7

  9. ORDER BY Yes Temp7 Sort data in , Generate intermediate tables Temp8

  10. LIMIT On the middle watch Temp8 paging , Generate intermediate tables Temp9

原网站

版权声明
本文为[Back end regular developers]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140519024268.html