(Reprinted) [MySQL] 20 classic interview questions, all answered correctly with a monthly salary of 10k+

Part2: Classic topics

 

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 if I want the SQL backed up in mysqldump to have only one insert....value() in a 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 changed 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)
can store up to 50 characters, varchar(50) and (200) occupy 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 20 in int (20)
refers to the length of the displayed character
but parameters are 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 does mysql design this
way is meaningless to most applications, but only specifies the number of characters used by some tools; int(1 ) is the same as int(20) for storage and calculation;

4. I asked the innodb transaction and log implementation
(1), how many kinds of logs there are;
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
transactions :
read uncommitted (RU)
read committed (RC)
repeatable read (RR)
serial

(3), how transactions are implemented through logs Yes, 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 "pre-write 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), applicable scenarios;
(3), combined with the first One question, the pros and cons of each log format in replication.
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 problem.)
Disadvantage: Since only the executed statements are recorded, in order for these statements to run correctly on the slave, it is also necessary to record some relevant information when each statement is executed to ensure that all statements can be obtained on the slave and in the 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)
while INSERT ...SELECT Will generate more row-level locks than RBR
2.Row: do not record the context-related information of the sql statement, only save 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 stored procedures, or functions, and trigger calls and triggers that cannot be copied correctly in some specific cases.
Disadvantages: all executed statements will be recorded in each line when they are recorded in the log. 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, some functions, the statement cannot complete the master-slave replication operation, 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 all processes No status changes for more than a second (kill)
Check the timeout log or error log (after several years of development, generally queries and large-scale insertions will cause cpu and i/o to rise,,, of course, it is not ruled out that the network status is suddenly interrupted, resulting in a request that the server only accepts To the half, such as where clause or pagination clause is not sent, of course, it is experienced once)

7. SQL optimization
(1), the meaning of various items explained;
select_type 
indicates the type of each select clause in the query
type
indicates how MySQL finds the required row in the table, also known as "access type"
possible_keys 
indicates which index MySQL can use to find the row in the table. If there is an index on the field involved in the query, the index will be listed. However, it may not be queried. Use
key
to display the index actually used by MySQL in the query. If no index is used, it will be displayed as NULL
key_len
indicates the number of bytes used in the index. The length of the index used in the query can be calculated through this column.
ref
indicates the above The connection matching conditions of the table, that is, which columns or constants are used to find the value on the index column. 
Extra
contains extra information that is not suitable for display in other columns but is very important

(2), the meaning of the profile, and the usage scenario;
the query to SQL will How long does it take to execute, and see the CPU/Memory usage, how much time Systemlock and Table lock spend during execution, etc.

8. Backup plan, implementation principle of mysqldump and xtranbackup
(1), backup plan;
each company here is different , don't talk about that kind of 1 hour 1 full preparation or something
(2) Backup and recovery time;
this is related to the speed of the machine, especially the hard disk. The following list is for reference only
: 2 minutes for 20G (mysqldump)
30 minutes for 80G (mysqldump) 30 minutes for
111G (mysqldump)
288G 3 hours (xtra)
3T 4 hours (xtra)
logical import time is generally more than 5 times the backup time

(3), the implementation principle of xtrabackup
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 if I want the SQL backed up in mysqldump to have only one insert....value() in a 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 table `helei`
--

LOCK TABLES `helei` WRITE;
/*!40000 ALTER TABLE `helei` DISABLE KEYS */;
INSERT INTO `helei ` VALUES (1,32,37,38,'2016-10-18 06:19:24','sususususususususususu');
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 within the fastest time
, dsh

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), related to IO parameter;
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.

  • The first one: If there are many read operations, look at the ratio. In short, if it is a user list table, or if the data ratio is relatively fixed, such as a product list, it can be opened, provided that these libraries are relatively concentrated, and the data in the database The practice is relatively small.
  • The second one: When we "deceive", for example, when we are bidding for pressure testing and open the query cache, we can still receive the effect of a surge in qps. Of course, the front-end connection pool is configured the same. In most cases, if there is a lot of writing and not many visits, then don’t open it. For example, on social networking sites, 10% of people generate content, and the remaining 90% are consuming. If it is a qq message, or chat, it is terrible.
  • The third: it doesn’t matter if small websites or those without high concurrency, under high concurrency, you will see a lot of qcache lock waiting, so in general, under high concurrency, it is not recommended to open query cache

 


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

13. Have you done the master-slave consistency check? If so, how did you do it? If not, what are you going 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?
There are many development specifications on the Internet. You can look at the summary for yourself

. The update is mainly based on reading. Please
(1), do you choose to split it 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 the 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 realized) by adding? 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 based on 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?

For the answer, see: http://suifu.blog.51cto.com/9167728/1830651

Open question: It is said to be
a 600 million table a and a 300 million table b of Tencent. Through the external tid association, how can you be the fastest Query the 200 data records from 50000th to 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 TID is not continuous, then you need to use a covering index. TID is either a primary key or a secondary index, and the B table ID also needs to have an index.
select * from b , (select tid from a limit 50000,200) a where b.id = a .tid;

 

Reprint link: https://blog.csdn.net/u013252072/article/details/52912385

Guess you like

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