MySQL classic interview questions (with answers)

Original link: http://bbs.51cto.com/thread-1470880-1.html

classic topic

1. MySQL replication principle and process

The basic principle process, 3 threads and the association between them;

2. The difference between myisam and innodb in MySQL, at least 5 points

(1), ask 5 different points;

(2), 4 major features of the innodb engine

(3) Which of the two selectcount(*) is faster and why

3. The difference between varchar and char in MySQL and the meaning of 50 in varchar(50)

(1), the difference between varchar and char

(2), the meaning of 50 in varchar(50)

(3), the meaning of 20 in int (20)

(4) Why is mysql so designed?

4. I asked about the implementation of transactions and logs in innodb

(1) How many kinds of logs are there?

(2), 4 isolation levels of things

(3) How transactions are implemented through logs, the more in-depth the better.

5. Asked about several log entry formats and differences of MySQL binlog

(1) The types and distinctions of the log format of binlog

(2), applicable scenarios;

(3) Combined with the first question, the advantages and disadvantages of each log format in replication.

6. I asked him what to do if the CPU of the MySQL database soared to 500%?

(1) If you have no experience, you may not ask;

(2), experienced, ask them to deal with ideas.

7, sql optimization

(1) The meaning of various items explained;

(2), the meaning of profile and usage scenarios;

8. The implementation principle of backup plan, mysqldump and xtranbackup

(1), backup plan;

(2) Backup and recovery time;

(3), the realization principle of xtrabackup

9. What should I do if I want the SQL backed up in mysqldump to have only one insert...value() per line? What if the backup needs to bring the master's replication point information?

10, 500 db, restart within the fastest time

.

11. Optimization of read and write parameters of innodb

(1), read parameters

(2), write parameters;

(3), parameters related to IO;

(4) Cache parameters and applicable scenarios of cache.

12. How do you monitor your database? How do you query your slow logs?

.

13. Have you done the master-slave consistency check? If so, how did you do it? If not, what do you plan to do?

14. Does your database support emoji expressions? If not, how to operate?

.

15. How do you maintain the data dictionary of the database?

16. Do you have development specifications, and if so, how are they implemented?

17. There is a large field X (for example: text type) in the table, and the field X will not be updated frequently, and it is mainly read.

(1), do you choose to split into sub-tables, or continue to put them together;

(2), write the reasons for your choice.

18. What is the row lock of the InnoDB engine in MySQL accomplished (or implemented)? Why is it like this?

.

19. How to restore only a certain database and a certain table from the full database backup generated by mysqldump?

Open question: It is said to be from Tencent

A 600 million table a, a 300 million table b, through external tid association, how can you quickly query the 200 data records from the 50000th to the 50200th that meet the conditions.

Part4: Answers

1. MySQL replication principle and process

The basic principle process, 3 threads and the association between them;

  1. Main: binlog thread - record all the statements that change the database data and put them in the binlog on the master;

  2. Slave: io thread - after using start slave, it is responsible for pulling the binlog content from the master and putting it into its own relay log;

  3. From: sql execution thread - execute the statement in the relay log;

2. The difference between myisam and innodb in MySQL, at least 5 points

(1), ask 5 different points;

1>.InnoDB supports things while MyISAM does not

2>.InnoDB supports row-level locks, while MyISAM supports table-level locks

3>.InnoDB supports MVCC, while MyISAM does not

4>.InnoDB supports foreign keys, while MyISAM does not

5>.InnoDB does not support full-text indexing, while MyISAM does.

(2), 4 major features of the innodb engine

Insert buffer (insert buffer), double write (double write), adaptive hash index (ahi), read ahead (read ahead)

(3) Which of the two selectcount(*) is faster and why

myisam is faster because myisam internally maintains a counter that can be called directly.

3. The difference between varchar and char in MySQL and the meaning of 50 in varchar(50)
(1), the difference between varchar and char
Char is a fixed-length type, and varchar is a variable-length type
(2 ), the meaning of 50 in varchar(50)
Up to 50 characters can be stored, varchar(50) and (200) take up the same space for storing hello, but the latter will consume more memory when sorting, because order by col uses fixed_length to calculate The length of col (the same is true for the memory engine)
(3), the meaning
of refers to the length of the displayed character,
but the parameter must be added. It will display 00000000001 ~~~00000000010. When the number of characters exceeds 11, it will only display 11 digits. If you do not add the parameter that makes it less than 11 digits, it will not add 0 in front of it.
20 Indicates that the maximum display width is 20, but it still occupies 4 bytes of storage, and the storage range remains unchanged;
(4) Why is mysql so designed? It is meaningless
to most applications, but only specifies the number of characters used by some tools to display; int(1 ) and int(20) are stored and calculated the same;

4. I asked the innodb transaction and log implementation
(1), how many kinds of logs are there;
error log: record error information, and also record some warning information or correct information.
Query Log: Records information about all requests made to the database, regardless of whether these requests were executed correctly.
Slow query log: Set a threshold and record all SQL statements whose running time exceeds this value to the log file of slow query.
Binary log: Logs all operations that perform changes to the database.
Relay log:
Transaction log:

(2), 4 isolation levels of things
isolation level
read uncommitted (RU)
read committed (RC)
repeatable read (RR)
serial

(3) How transactions are implemented through logs, the more in-depth the better.
The transaction log is implemented by the storage engine log buffer (Innodb log buffer) of redo and innodb. When a transaction is started, the lsn (log sequence number) number of the transaction will be recorded; when the transaction is executed, it will be stored in InnoDB The transaction log is inserted into
the log cache of the engine's log; when the transaction is committed, the log buffer of the storage engine must be written to the disk (controlled by innodb_flush_log_at_trx_commit), that is, the log needs to be written before data is written. This method is called "write-ahead log method"

5. Asked about several log entry formats of MySQL binlog and their differences
(1), the types and distinctions of binlog log formats
(2), and applicable scenarios;
(3) Combined with the first question, each log format is replicated advantages and disadvantages in.
Statement: Each sql that modifies data will be recorded in binlog.
Advantages: It is not necessary to record the changes of each line, which reduces the amount of binlog logs, saves IO, and improves performance. (How much performance and log volume can be saved compared to row, this depends on the SQL situation of the application. Normally, the volume of logs generated by modifying or inserting the same record in row format is less than the volume of logs generated by Statement, but considering that if the conditional update Operations, delete the entire table, alter table and other operations, the ROW format will generate a large number of logs, so when considering whether to use the ROW format log, you should follow the actual situation of the application, how much the amount of logs generated will increase, and the resulting IO performance issues.)
Disadvantages: Since only the executed statements are recorded, in order for these statements to run correctly on the slave, it is necessary to record some relevant information of each statement when it is executed to ensure that all statements can be obtained and stored on the slave. The same result when the master side executes. In addition, mysql replication, like some specific functions, the slave can be consistent with the master, there will be many related problems (such as sleep() function, last_insert_id(), and user-defined functions (udf) will have problems).
Use the following Statements of functions also cannot be copied:
* LOAD_FILE()
* UUID()
* USER()
* FOUND_ROWS()
* SYSDATE() (unless the --sysdate-is-now option was enabled at startup)
At the same time, INSERT ... SELECT will generate more row-level locks than RBR.
2.Row: The context-related information of the sql statement is not recorded, and only which record is modified.
Advantages: The binlog does not need to record the context-related information of the executed SQL statement, and only needs to record what the record has been modified into. Therefore, the log content of rowlevel will clearly record the details of each row of data modification. And there will be no problem that the stored procedure, or function, and trigger calls and triggers cannot be copied correctly in some specific cases.
Disadvantage : When all executed statements are recorded in the log, they will be recorded in each line. Modify to record, which may generate a large amount of log content, such as an update statement, modify multiple records, then each modification in the binlog will have a record, which will cause a large amount of binlog logs, especially when executing alter table and the like When the statement is executed, each record is changed due to the modification of the table structure, then each record of the table will be recorded in the log.
3.Mixedlevel: It is a mixed use of the above two levels. The general statement modification uses the statment format to save the binlog. For example, for some functions, the statement cannot complete the master-slave replication operation, then the binlog is saved in the row format, and MySQL will be executed according to each execution. The specific sql statement is used to distinguish the log form of the record, that is, choose one between Statement and Row. The new version of MySQL squadron row level mode has also been optimized, not all modifications will be recorded in row level, For example, when the table structure is changed, it will be recorded in the statement mode. As for statements that modify data such as update or delete, the changes of all rows will still be recorded.

6. I asked him what to do if the CPU of the MySQL database soared to 500%?
(1) If you have no experience, you can leave it alone;
(2) If you have experience, ask them how to deal with it.
List all processes show processlist Observe that all processes have no status change for more than a second (kill them)
Check the timeout log or error log (have been developing for several years, usually queries and large batches of inserts will cause cpu and i/o to rise, ,, of course, it does not rule out that the network status is suddenly broken, causing a request server to only receive half of it, such as where clause or paging clause is not sent, of course, a pit experience)

7, sql optimization
(1), the meaning of various items explained;
select_type
indicates the type of each select clause in the query
type
indicates the way MySQL finds the desired row in the table, also known as "access type"
possible_keys
indicates MySQL Which index can be used to find rows in the table. If there is an index on the field involved in the query, the index will be listed, but it may not be queried. Use the
key
to display the index actually used by MySQL in the query. If no index is used, Displayed as NULL
key_len
indicates the number of bytes used in the index, through which the length of the index used in the query can be calculated
ref
indicates the join matching condition of the above table, that is, which columns or constants are used to find the value on the index column
Extra
contains Important extra information that doesn't fit in other columns

(2), the meaning of profile and usage scenarios;
query how long SQL will take to execute, and see the CPU/Memory usage, how long Systemlock and Table lock take during execution, etc.

8. Backup plan, implementation principle of mysqldump and xtranbackup
(1), backup plan;
each company here is different, don’t say that 1 hour, 1 full backup or something
(2), backup and recovery time;
here is the same as the machine , especially the speed of the hard disk is related, the following lists a
few for reference only ) The logical import time is generally more than 5 times the backup time




(3) Implementation principle
of InnoDB will maintain a redo log file, which can also be called a transaction log file. The transaction log stores every record modification of InnoDB table data. When InnoDB starts, InnoDB examines the data files and transaction log and performs two steps: it applies (rolls forward) the committed transaction log to the data file, and rolls back the modified but uncommitted data.

9. What should I do if I want the SQL backed up in mysqldump to have only one insert...value() per line? What if the backup needs to bring the master's replication point information?
–skip-extended-insert
[root@helei-zhuanshu ~]# mysqldump -uroot -p helei –skip-extended-insert
Enter password:
KEY idx_c1( c1),
KEY idx_c2( c2)
) ENGINE=InnoDB AUTO_INCREMENT=51 DEFAULT CHARSET=latin1;
/ !40101 SET character_set_client = @saved_cs_client /;

– Dumping data for tablehelei

LOCK TABLES helei WRITE;
/!40000 ALTER TABLE helei DISABLE KEYS /;
INSERT INTO helei VALUES (1,32,37,38,’2016-10-18 06:19:24’,’susususususususususususu’);
INSERT INTO helei VALUES (2,37,46,21,’2016-10-18 06:19:24’,’susususususu’);
INSERT INTO helei VALUES (3,21,5,14,’2016-10-18 06:19:24’,’susu’);


10. 500 db, restart puppet and dsh within the fastest time

11. Innodb read and write parameters optimization
(1), read parameters
global buffer pool and local buffer;

(2), write parameters;
innodb_flush_log_at_trx_commit
innodb_buffer_pool_size

(3), IO-related parameters;
innodb_write_io_threads = 8
innodb_read_io_threads = 8
innodb_thread_concurrency = 0

(4) Cache parameters and applicable scenarios of cache.
query cache/query_cache_type
Not all tables are suitable for query cache. The main reason for the invalidation of the query cache is that the corresponding table has changed.

第一个:读操作多的话看看比例,简单来说,如果是用户清单表,或者说是数据比例比较固定,比如说商品列表,是可以打开的,前提是这些库比较集中,数据库中的实务比较小。
第二个:我们“行骗”的时候,比如说我们竞标的时候压测,把query cache打开,还是能收到qps激增的效果,当然前提示前端的连接池什么的都配置一样。大部分情况下如果写入的居多,访问量并不多,那么就不要打开,例如社交网站的,10%的人产生内容,其余的90%都在消费,打开还是效果很好的,但是你如果是qq消息,或者聊天,那就很要命。
第三个:小网站或者没有高并发的无所谓,高并发下,会看到 很多 qcache 锁 等待,所以一般高并发下,不建议打开query cache

12. How do you monitor your database? How do you query your slow logs?
There are many monitoring tools, such as zabbix, lepus, I use lepus here

13. Have you done the master-slave consistency check? If so, how did you do it? If not, what do you plan to do?
There are various tools for master-slave consistency check, such as checksum, mysqldiff, pt-table-checksum, etc.

14. Does your database support emoji expressions? If not, how to operate?
If it is utf8 character set, you need to upgrade to utf8_mb4 to support

15. How do you maintain the data dictionary of the database?
This maintenance method is different for everyone. I usually annotate it directly in the production library, and use the tool to export it to excel for easy circulation.

16. Do you have development specifications? If so, how to implement them
? Yes, there are many development specifications on the Internet. You can take a look and summarize them yourself.

17. There is a large field X (for example: text type) in the table, and the field X will not be updated frequently, and it is mainly read. May I ask
(1), do you choose to split into sub-tables, or continue to put them together;
(2), Write down the reasons for your choice.
Answer: Problems caused by splitting: connection consumption + storage split space; problems that may not be caused by splitting: query performance;
if the space problem caused by splitting can be tolerated, it is best to split the primary key of the table that is often queried. Put together (partition) sequential IO on the physical structure, reduce connection consumption, and finally this is a text column plus a full-text index to offset connection consumption as much as possible.
If you can tolerate the query performance loss caused by not splitting: the above The solution will definitely have problems under certain extreme conditions, so not dismantling is the best choice

18. What is the row lock of the InnoDB engine in MySQL accomplished (or implemented)? Why is it like this?
Answer: InnoDB completes row locks based on indexes.
Example : select * from tab_with_index where id = 1 for update;
for update can complete row locks according to conditions, and id is a column with an index key.
If id is not an index key, then InnoDB The table lock will be completed, and concurrency will be impossible to talk about

.

19. How to restore only a certain database and a certain table from the full database backup generated by mysqldump?

See the answer: http://suifu.blog.51cto.com/9167728/1830651

Open question: It is said that Tencent has
a table a with a value of 600 million and a table b with a value of 300 million. Through the external tid association, how can you quickly query the 200 data records from the 50000th to the 50200th that meet the conditions.
1. If the TID of table A is self-increasing and continuous, the ID of table B is the index
select * from a,b where a.tid = b.id and a.tid>500000 limit 200;

2. If the TID of table A is not continuous, then you need to use a covering index. TID is either a primary key or an auxiliary index, and the ID of table B also needs to have an index.
select * from b , (select tid from a limit 50000,200) a where b.id = a .tid;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325899140&siteId=291194637