This is probably the most detailed ever-tier manufacturers Mysql face questions Xiangjie

1, MySQL replication principle and process

The basic principle of the process, as well as association between the three threads;

Main: binlog thread - to record all changes the phrase database data into the binlog on the master;

From: io thread - After using start slave, responsible for pulling binlog contents from the master, into his relay log in;

From: sql thread of execution - execution relay log statements;

2, with the difference in MySQL innodb myisam least 5:00

(1), Q different 5:00;

1> .InnoDB support of things, but MyISAM does not support things

2> .InnoDB supports row-level locking, and support MyISAM table-level locking

3> .InnoDB support MVCC, but MyISAM does not support

4> .InnoDB support foreign keys, and MyISAM does not support

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

4 major feature (2), innodb engine

Insert buffer (insert buffer), the secondary write (double write), adaptive hash index (AHI), the pre-read (read ahead)

(3), 2 person selectcount (*) Which is faster, and why

myisam faster, because the internal myisam maintains a counter, can be directly retrieved.

Meaning 3, the difference in MySQL varchar and char, and varchar (50) 50 representatives

(1) the difference, varchar and the char

char is a type of fixed length, varchar is a type of variable length

(2), (50) the meaning of VARCHAR 50

Hold up to 50 characters, varchar (50) and (200) storage space occupied by the same hello, when sorting the latter consumes more memory, since the order by col col calculated using fixed_length length (memory same engine)

The meaning (3), int (20) in 20

It refers to the length of the character display

But to add a parameter, a maximum of 255, for example, it is the id of the number of rows, insert 10 records data, it displays 00000000001 ~ 00,000,000,010, when the median of more than 11 characters, it also shows only 11, if you adding that it is not less than 11 parameters on the front plus 0, it does not add 0 in front

20 represents the maximum width of the display 20, but still 4 bytes of storage, the same memory range;

(4), mysql why so design

For most applications there is no meaning, only provides some of the tools used to display the number of characters; int (1) and int (20) storage and computation are the same;

4, asked the implementation of the transaction and log innodb

(1), how many logs;

Error Log: record the error message, also recorded warning messages or correct information.

Query Log: Record all the information to a database request, regardless of whether these requests were performed correctly.

Slow query log: set a threshold value, will run longer than the value of all SQL statements are recorded to a log file slow query.

Binary Log: Record all changes to the database operations.

Relay log:

Transaction Log:

(2), four kinds of things isolation levels

Isolation Levels

Uncommitted Read (RU)

Read Committed (RC)

Repeatable Read (RR)

Serial

(3) the transaction log is how to achieve, the more you say, the better.

The transaction log is achieved through redo and innodb storage engine log buffer (Innodb log buffer), when beginning a transaction, records the transaction lsn (log sequence number) number; when a transaction is executed, will go InnoDB storage log log cache engine which is inserted into the transaction log; when the transaction commits, the storage engine must log buffer is written to disk (controlled by innodb_flush_log_at_trx_commit), that is, before writing the data, you need to write the log. This is called "write-ahead log mode"

5, MySQL binlog several log entry format, and the difference

Statement: Each sql will modify the data recorded in the binlog.

Advantages: the variation of each row need not be recorded, reducing the amount of log binlog, the IO savings, improved performance. (Row number as compared to the amount of energy saving performance logs, depending on the SQL application, the normal edit or insert the same amount of log record format generated row is less than the amount of logs Statement also generated, but if considering the update conditions with operation, as well as delete the entire table, alter table and other operations, ROW format will produce a large log, and therefore should be considered according to the actual situation with the application, log the amount they produce will increase the number of whether to use the ROW format of the log, and brought IO performance problems.)

Cons: Since only execute the statement, these statements in order to run properly on the slave, each statement must also record some information in the course of implementation, in order to ensure that all statements get executed in the master and slave in the end, when the same record the result of. In addition mysql replication, as some specific functions function, slave and the master can be consistent there will be many related issues (such as sleep () function, last_insert_id (), as well as user-defined functions (udf) problem occurs).

The following functions can not be copied statement:

LOAD_FILE()

UUID()

USER()

FOUND_ROWS()

SYSDATE () (unless startup is enabled --sysdate-is-now option)

And it will produce more than RBR row-level locking in INSERT ... SELECT

2.Row: no records sql statement to context-sensitive information, which saves only the modified records.

Pros: context-sensitive information can not be recorded in the binlog sql statement is executed, only need to record that a record is modified into anything. So log content rowlevel record will be very clear details of each line of data modifications under. And the process will not be stored under certain specific circumstances, or function, and the problems trigger the call and triggers can not be reproduced correctly

Cons: All statements executed when logged when all will modify each line to record the record, this may generate a lot of log content, such as an update statement to modify multiple records in each of the binlog changes will be recorded, so causing binlog log will have a significant amount, especially when performing such alter table statements, due modify table structure, each record is changed, then the table each record will be logged .

3.Mixedlevel: is a mixture of two or more kinds of level, using the general statement modifies the binlog statment save format, such as some functions, statement can not be completed from the master copy operation, the row format is used to save binlog, MySQL will be performed according to each one specific sql statement to distinguish between the log form of treatment records, that is, choose one between the statement and Row. the new version of MySQL squadron row level mode has also been optimized, not all changes will be recorded in a row level, like when it came to the table structure changes will be recorded in a statement mode. As for statements such as update or delete data modification, or it will record changes for all rows.

6, MySQL database cpu soared to 500%, then how can he deal with?

1, list all processes show processlist, observe all processes, multi seconds without a change of state (to kill)

2, view logs, or a timeout error log (for a few years to develop, is usually inserted into the inquiry as well as large quantities can cause cpu and i / o rise, of course, does not rule out the network status snapped ,, only lead to a request received by the server half, where such clause or clauses did not send paging and, of course once the pit experience)

7, sql various optimization methods

(1), explain the significance of the item out of all kinds;

select_type

It represents the type of each query select clause

type

MySQL find a way to express the desired line, also known as "access type" in the table

possible_keys

It indicates which indexes MySQL could use to find if there is an index, the index will be listed on the Field row, the query involved in the table, but not necessarily be queried using

key

MySQL query displays the index in actual use, if not use the index, shown as NULL

key_len

Indicates the number of bytes in the index, the index can be used in the query is calculated by the column length

ref

Connecting the matching conditions are shown in Table, i.e. constant, or column which is used to find the value of the index column

Extra

Unsuitable for display but additional information is very important in the other columns

(2) the significance of the profile and usage scenarios;

SQL query to how much time will be executed and to see CPU / Memory usage, the implementation process Systemlock, Table lock spend much time and so on

8. The principle backup plan, mysqldump and the xtranbackup

(1), a backup plan;

Where each company is different, you do not say what kind of 1 hour and 1 full backup on the line

(2), backup and recovery time;

Here with the machine, especially the rate of the hard disk of a relationship, the following are a few for reference only.

20G for 2 minutes (the mysqldump)

80G for 30 minutes (the mysqldump)

111G for 30 minutes (the mysqldump)

288G for 3 hours (Xtra)

3T 4 hours (Xtra)

Logic introduction time is generally more than 5 times the backup time

(3), xtrabackup implementation principle

InnoDB internally maintains a redo log file, we can also be called the transaction log file. The transaction log records are stored for each InnoDB table data changes. When InnoDB start, InnoDB checks the data files and transaction logs, and perform two steps: it is applied (roll forward) has been submitted to the transaction log file data, and modify data but had not submitted rollback.

9, mysqldump out of sql backup, if I want to sql file, only one row insert ... .value (), then how do? If you need to bring a backup copy of the information point master how to do?

--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','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 Taiwan db, in the fastest time of restart

puppet,dsh

11, read and write parameters optimization innodb

(1), read parameter

global buffer pool以及 local buffer;

(2), the writing parameters;

innodb_flush_log_at_trx_commit

innodb_buffer_pool_size

(3), a parameter associated with the IO;

innodb_write_io_threads = 8

innodb_read_io_threads = 8

innodb_thread_concurrency = 0

(4), cache parameters and applicable scene cache.

query cache/query_cache_type

Not all tables are suitable for use query cache. Reason query cache failure is mainly the corresponding table has been changed at

The first one: read multi-operation, then take a look at the proportion, in simple terms, if a user list table, or the ratio of data is relatively fixed, such as merchandise list is open, provided that these libraries are concentrated, database practice is relatively small.

Second: we "cheat" when, for example, we bid when the pressure test, open the query cache, or to receive qps surge in effect, of course, the front end of the connection pool before suggesting what are configured the same. In most cases, if written in the majority, not much traffic, then do not open, such as social networking sites, 10 percent of people-generated content, the remaining 90% in consumption, open or very good results, but you If qq message, or chat, it is very terrible.

The third: a small website or not does not matter high concurrency, high concurrency, will see a lot qcache lock wait, so under normal high concurrency, not recommended to open the query cache

12, how do you monitor your database? Your slow query log is how?

There are many monitoring tools, such as zabbix, lepus, I'm here is lepus

13, if you did check the consistency of master and slave, and if so, how to do, and if not, how are you going to do?

From a variety of consistency check master tools such as checksum, mysqldiff, pt-table-checksum, etc.

14, your database supports emoji expression, if not, how to operate?

If utf8 character set, then only need to be upgraded to support utf8_mb4

15, how do you maintain the data dictionary database?

We maintain this approach is different, I usually annotate directly on the production database, exporting to excel with tools to facilitate circulation.

16, the table has a large field of X (for example: text type), and the X field is not frequently updated, to read the main, what

Problems caused by demolition: + connection consumes storage space split; do not split potential problems: query performance;

1, if the space problem can tolerate splitting caused, it is preferably removed frequently queried and the primary key table is placed on the physical structure together (partition) sequence of the IO, connected to reduce consumption, which is the last column together with a text on a full-text index to try to offset the consumption connection

2. If you can not tolerate query performance loss caused by the split of words: the above program there will be problems in certain extreme conditions, the split is not the best choice

17, row locks in MySQL InnoDB engine is completed by adding on what (or realization) of? Why is it like this?

InnoDB is based on the index to complete the row lock

例: select * from tab_with_index where id = 1 for update;

for update can be done according to the conditions locks row and column index id is bond,

If id is not indexed bond will be completed then the InnoDB table lock ,, concurrency will be impossible

18, open questions: is said to be Tencent

A 600 million table a, a 300 million table b, by associating outside tid, how do you check out the 200 fastest data records to meet the conditions of section 50000 to 50200 in the first.

1, if the growth is self-TID Table A, and is continuous, ID B as an index table

select * from a,b where a.tid = b.id and a.tid>500000 limit 200;

2, if the TID A table is not continuous, then need to use a covering index .TID either a primary key or a secondary indexes, B Index table ID is also needed.

select * from b , (select tid from a limit 50000,200) a where b.id = a .tid;

19. What is a stored procedure? What are the advantages and disadvantages?

Stored procedures are prebuilt SQL statements.

1, more straightforward to understand: the process can be a stored record set, which is a code block by a number of T-SQL statements, such as T-SQL statement codes as a method to achieve some of the features (single table or multiple tables CRUD), and then give the block a name, call him on the line when use this function.

2, a stored procedure is precompiled code blocks, the efficiency is relatively high, a large number of alternative stored procedure T_SQL statements, network traffic can be reduced to improve the communication rate, data security can be ensured to a certain extent

20. What indexes are? What is the role and the advantages and disadvantages?

1, the index value is a database table or a plurality of columns to sort the structure is to help obtain data MySQL efficient data structure

2, the index is to speed up the retrieval data table method. Index database is similar to the index of the book. In the book, the index allows the user does not have to read a complete book to find the information they need quickly. In the database, the index database program also allows to quickly find the data in the table, without having to scan the entire database.

MySQL database several basic types of indexes: General index, the only index, primary key indexes, full-text indexing

1, the index quicker retrieval database

2, the index slows insert, delete, modify, and other maintenance tasks

3, the only index to ensure the uniqueness of each row of data

4, by using an index can be used to hide in the process of optimizing query, improve system performance

5, the index needs to account for the physical and data space

21. What is a transaction?

Transaction (Transaction) is the basic unit of concurrency control. The so-called transaction, it is a sequence of operations, these operations are either executed or not executed, it is an indivisible unit of work. A transaction is a database maintaining data consistency of the unit, at the end of each transaction, can maintain data consistency.

24, what optimistic and pessimistic locking of the database is?

Concurrency Control task database management system (DBMS) is isolated and ensuring uniformity and unity without destroying the transaction database when multiple transactions simultaneously access the same data in the database. Optimistic concurrency control (optimistic locking) and pessimistic concurrency control (pessimistic locking) concurrency control techniques are mainly used.

Pessimistic locking: assume concurrency violation occurs, the shield may violate data integrity of all operations

Optimistic locking: Suppose concurrency conflicts will not occur, if only to check data integrity violation when a commit operation.

22, the query will be able to use the index to improve query performance do? why

Typically, the index query data faster than a full table scan. But we also must be noted that price.

1, the index need space to store, but also require regular maintenance, whenever a record changes in an index or column in the table is modified, the index itself can be modified. This means that each record INSERT, DELETE, UPDATE will this pay more than 4,5 times the disk I / O. because the index requires additional storage space and processing, but will make those unnecessary indexes slow query response time. queries may not be able to use the index to improve query performance, range queries index (INDEX RANGE SCAN) in two situations:

2, a range-based retrieval, the query results typically set less than 30% of the number recorded in the table

3, retrieval based on non-unique index

23, to talk briefly drop, delete and truncate the region

SQL in the drop, delete, truncate have expressed deleted, but there are some differences three

1, delete and delete only truncate table does not delete the data structure of the table

2, the speed, in general: drop> truncate> delete

3, delete statement is dml, this action will put rollback segement, the only take effect after the transaction commits;

4, if there is a corresponding trigger, when the execution will be triggered. Truncate, drop is ddl, operating with immediate effect, the original data is not put rollback segment can not be rolled back. Operation does not trigger the trigger.

24, drop, delete and truncate, respectively, under what scenario to use?

1, a table is no longer required when using drop

2, to remove some rows of data when using delete, and bring the where clause

3, reserved table and delete all data when using truncate

25, super-key, what the candidate keys, primary keys, foreign keys are?

1, super bond: in relation uniquely identifies the tuple attribute set called super key relationships mode. A property may be used as a super key, the plurality of attributes can be grouped together as a super key. Super key contains the primary key and a candidate key.

2, candidate keys: the key is a minimum over, i.e., does not exceed the key redundancy elements.

3, the primary key: a database table columns or attributes of data to be a combination of unique and complete identification of the stored data objects. A data column can have only one primary key and the primary key value can not be deleted, i.e., not a null value (Null).

4, the foreign key: in the presence of a primary key in another table called the foreign key of the table.

26. What is a view? And the use of the scene view of what?

1, the view is a virtual table, has the same functionality as a physical table. Can be increased to view, change, operation, there is usually attempted to a table or a subset of the plurality of rows or columns of the table. Changes to the view do not affect the basic table. It allows us to obtain data more easily, compared to multi-table queries.

2, only the exposed portions of the field to the visitor, so we built a virtual table, is the view.

3, data queries from different tables, queries and wishes in a uniform manner queries, so you can create a view, the query results together multiple tables, the query only needs to get data directly from the view, regardless of the data comes from differences arising table

27, talk about the three paradigms.

The first paradigm (1NF): database table fields are a single property, can not be divided. This property consists of a single basic types, including integer, real number, character, logic type, date type. The second paradigm (2NF): database table no non-key field dependent on any one part of the function key field candidates (partial functional dependency means that there is some combination of keywords in the field of non-key field decision ), i.e. all non-key fields are totally dependent on any set of candidate keys. The third paradigm (3NF): on the basis of the second paradigm, if there is no data in the table the non-dependent key field transfer function according to any one candidate keyword section is in line with the third paradigm. The so-called transfer function dependent, means that if there is "A → B → C" determines the relationship between the transfer function depends on C A. Non-key fields → X → non-key field Key Fields y: Thus, the third normal database tables should not exist as dependencies

Welcome to work one to five years of Java Java engineer friends to join high-level architecture: 948 368 769

Java architecture to provide free learning materials within the group (which has high availability, high concurrency, high performance and distributed, Jvm performance tuning, Spring Source,

More knowledge of MyBatis, Netty, Redis, Kafka, Mysql, Zookeeper, Tomcat, Docker, Dubbo, Nginx, etc. Information Architecture)

Rational use of every minute of their own time to enhance their learning, do not use the "no time" to hide his ideological laziness! Young, hard fight, give an account of their own future!

 

 

 

Guess you like

Origin blog.csdn.net/qq_42784210/article/details/88952710