Key points of MySQL interview (1)

Key points of MySQL interview (1)

Foreword

  • MySQL is a ** relational database management system, ** developed by the Swedish MySQL AB company and belongs to Oracle's products. MySQL is one of the most popular relational database management systems. In terms of WEB applications, MySQL is one of the best RDBMS (Relational Database Management System, relational database management system) application software.
  • MySQL is a relational database management system. Relational databases store data in different tables instead of putting all the data in a large warehouse, which increases speed and flexibility.
  • The SQL language used by MySQL is the most commonly used standardized language for accessing databases. The MySQL software adopts a dual-licensing policy, which is divided into a community version and a commercial version. Due to its small size, fast speed, and low total cost of ownership, especially the open source feature, the development of small and medium-sized websites generally chooses MySQL as the website database.

1. The three paradigms of the database

  • 1. The first normal form (1NF): the field is atomic and cannot be divided. (All relational database systems satisfy the first normal form. The fields in the database table are single-attribute and cannot be divided.)
  • 2. The second normal form (2NF) is established on the basis of the first normal form (1NF), that is, to satisfy the second normal form (2NF), the first normal form (1NF) must be satisfied first. Each instance or row in the database table must be uniquely distinguishable. You usually need to add a column to the table to store the unique identifier of each instance. This unique attribute column is called the primary key or primary key.
  • 3. To meet the third normal form (3NF) must first satisfy the second normal form (2NF). In short, the third normal form (3NF) requires that a database table does not contain non-primary key information already contained in other tables. > So the third normal form has the following characteristics: >> 1. Each column has only one value >> 2. Each row can be distinguished. >> 3. Each table does not contain non-primary key information that other tables already contain.

2. Experience in database optimization

  • 1. Use PreparedStatement, generally higher performance than Statement: a sql sent to the server to perform, involving steps: syntax check, semantic analysis, compilation, and cache.
  • 2. There are foreign key constraints that affect the performance of insertion and deletion. If the program can guarantee the integrity of the data, remove the foreign key when designing the database.
  • 3. Appropriate redundancy is allowed in the table, for example, the number of replies to topic posts and the last reply time, etc. 4. UNION ALL is much faster than UNION, so if you can confirm that the two combined result sets do not contain duplicate data and no sort If it is, then use UNIONALL. >> UNION and UNION ALL keywords combine two result sets into one, but both are different in terms of use and efficiency. > 1. Treatment of duplicate results: UNION will filter out duplicate records after table linking, and Union All will not remove duplicate records. > 2. Processing of sorting: Union will sort according to the order of the fields; UNION ALL simply returns the result after merging the two results.

3. Commonly used indexes

  • 1. Ordinary index: that is, create an index for the database table
  • 2. Unique index: Similar to the ordinary index, the difference is: the value of the MySQL database index column must be unique, but null values ​​are allowed
  • 3. Primary key index: It is a special unique index, which does not allow null values. Generally, the primary key index is created at the same time when the table is built
  • 4. Combined index: In order to further extract the efficiency of MySQL, we must consider the establishment of a combined index. That is, multiple fields in a database table are combined as a combined index.

4. Working mechanism of MySQL database index

  • The database index is a sorted data structure in the database management system to help quickly query and update the data in the database table. The implementation of index usually uses B tree and its variant B + tree

5. The basic command operation of MySQL

  • 1. Is MySQL running: run the service mysqlstatus command on Debian, and run the service mysqld status command on RedHat
  • 2. Start or stop the MySQL service: run the command service mysqld start to start the service; run the command service mysqld stop to stop the service
  • 3. Shell login to MySQL: Run the command mysql -u root -p
  • 4. List all databases: run the command show databases;
  • 5. Switch to a database and work on it: run the command use database name; to enter the database named database name
  • 6. List all tables in a database: show tables;
  • 7. Get the names and types of all Field objects in the table: describe table_name;

6. MySQL replication principle and process

  • The built-in replication function of Mysql is the foundation for building large, high-performance applications. Distribute Mysql's data to multiple systems. This distribution mechanism is achieved by copying the data of a Mysql host to other hosts (slaves) and re-executing it. * One server acts as the master server during replication, and one or more other servers act as slave servers.
  • The master server writes updates to the binary log file and maintains an index of the file to track the log cycle. These logs can record updates sent to the slave server. When a slave server connects to the master server, it informs the master server of the last successfully updated position read in the log.
  • The slave server receives any updates that have occurred since then, then blocks and waits for the master server to notify new updates. The process is as follows
    • 1. The main server records the update in a binary log file.
    • 2. The slave server copies the binary log of the master server to its own replay log.
    • 3. Redo the time in the relay log from the server and apply the update to its own database.

7. The replication types supported by MySQL

  1. Statement-based replication: SQL statements executed on the master server, and the same statements executed on the slave server. MySQL uses statement-based replication by default, which is more efficient. Once it is found that it is impossible to copy accurately, it will automatically select the line-based copy.
  2. Line-based copying: copy the changed content to the past instead of executing the command on the slave server. Supported since mysql5.0
  3. Mixed-type replication: By default, statement-based replication is used. Once statement-based replication cannot be accurately replicated, row-based replication is used.

8. The difference between MyISAM and InnoDB in MySQL

  1. Transaction support> MyISAM: Emphasis is on performance, each query is atomic, its execution speed is faster than InnoDB type, but does not provide transaction support. > InnoDB: Provides advanced database functions such as transaction support transactions and foreign keys. A transaction-safe (ACID compliant) type table with transactions, rollbacks, and crash recovery capabilities.
  2. InnoDB supports row-level locks, while MyISAM supports table-level locks. >> When users operate the myisam table, select, update, delete, and insert statements will automatically lock the table. If the locked table meets the insert concurrent condition, New data can be inserted at the end of the table.
  3. InnoDB supports MVCC, while MyISAM does not
  4. InnoDB supports foreign keys, while MyISAM does not
  5. Table primary key> MyISAM: Allow tables without any index and primary key to exist, and the index is the address of the stored row. > InnoDB: If no primary key or non-null unique index is set, a 6-byte primary key will be automatically generated (not visible to the user). The data is part of the primary index, and the additional index holds the value of the primary index.
  6. InnoDB does not support full-text indexing, while MyISAM does.
  7. Portability, backup and recovery> MyISAM: The data is stored in the form of files, so it will be very convenient in cross-platform data transfer. During backup and restore, you can operate on a table separately. > InnoDB: The free solution can be to copy data files, backup binlog, or use mysqldump, which is relatively painful when the amount of data reaches dozens of G
  8. Storage structure> MyISAM: Each MyISAM is stored as three files on the disk. The name of the first file starts with the name of the table, and the extension indicates the file type. .frm file storage table definition. The extension of the data file is .MYD (MYData). The extension of the index file is .MYI (MYIndex). > InnoDB: All tables are stored in the same data file (may also be multiple files, or independent table space files). The size of InnoDB tables is limited only by the size of the operating system files, generally 2GB.

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

  1. The difference between varchar and char: char is a fixed-length type, varchar is a variable-length type.
  2. The meaning of 50 in varchar (50): store up to 50 bytes
  3. Meaning of 20 in int (20): M in int (M) indicates the maximum display width for integer types. The maximum legal display width is 255.

10. The names of the four bookstore isolation levels supported by InnoDB in MySQL, and the difference between them?

  1. Read Uncommitted >> At this isolation level, all transactions can see the execution results of other uncommitted transactions. This isolation level is rarely used in practical applications because its performance is not much better than other levels. Reading uncommitted data is also called dirty read.
  2. Read Committed >> This is the default isolation level for most database systems (but not the MySQL default). It satisfies the simple definition of isolation: a transaction can only see the changes made by the committed transaction. This isolation level also supports so-called non-repeatable reads (Nonrepeatable Read), because other instances of the same transaction may have new commits during the instance processing, so the same select may return different results.
  3. Repeatable Read >> This is MySQL's default transaction isolation level. It ensures that multiple instances of the same transaction will see the same row of data when reading data concurrently. However, in theory, this will cause another thorny problem: PhantomRead. Simply put, phantom reading refers to when a user reads a range of data rows, another transaction inserts a new row in the range, and when the user reads the range of data rows again, there will be new "Phantom" line. The InnoDB and Falcon storage engines solve this problem through a multi-version concurrency control (MVCC, Multiversion Concurrency Control gap lock) mechanism. Note: In fact, multiple versions only solve the problem of non-repeatable reads, and the addition of gap locks (that is, the so-called concurrency control here) only solves the magic read problem.
  4. Serializable (Serializable) >> This is the highest isolation level. It enforces the ordering of transactions to make it impossible for them to conflict with each other, thereby solving the problem of phantom reading. In short, it is to add a shared lock on each line of data read. At this level, a large number of timeouts and lock contention may result.

11. The uppercase field in the table, and field X will not be updated frequently, mainly for reading, what is the advantage of splitting this field into a subtable?

  • If there are large field (text, blob) types in the field, and there are not many accesses to these fields, then putting them together becomes a disadvantage. The record storage of the MYSQL database is stored in rows, and the data block size is fixed (16K). The smaller each record, the more records stored in the same block. At this point, the large fields should be removed, so that when dealing with most small field queries, efficiency can be improved. When a large field needs to be queried, the related query at this time is inevitable, but it is also worth it. After splitting, the UPDAE of the field will UPDATE multiple tables

12. What is the implementation of the row lock of the InnoDB engine in MySQL

  • InnoDB row locking is achieved by locking the index items on the index. This is different from Oracle. The latter is achieved by locking the corresponding data row in the data block. InnoDB's implementation of row locks means that: only when data is retrieved by index conditions, InnoDB uses row-level locks; otherwise, InnoDB will use table locks!

13. What are the global parameters that control memory allocation in MySQL?

  1. Key buffer size:> * key buffer size specifies the size of the index buffer, which determines the speed of index processing, especially the speed of index reading. By checking the status values ​​Key read requests and Key reads, you can know whether the key buffer size setting is reasonable. The ratio of key reads / key read requests should be as low as possible, at least 1: 100, 1: 1000 is better (the above status values ​​can be obtained using SHOW STATUS LIKE ' key read%'). > * key buffer size only works for MyISAM tables. Even if you do not use MyISAM tables, but the internal temporary disk table is MyISAM table, you should use this value. You can use the check status value created tmp disk tables for details. For machines with 1G memory, if you don't use MyISAM table, the recommended value is 16M (8-64M)> * key buffer size Setting notes >>> 1. Single keyThe size of the buffer cannot exceed 4G. If the setting exceeds 4G, you may encounter the following 3 bugs: >>>>> http://bugs.mysql.com/bug.php?id=29446 >>>>> http : //bugs.mysql.com/bug.php? id = 29419 >>>>> http://bugs.mysql.com/bug.php?id=5731 >>> 2. It is recommended to set the key buffer to physical memory 1/4 (for MyISAM engine), or even 30% ~ 40% of physical memory, if the key buffer size is set too large, the system will frequently page, reducing system performance. Because MySQL uses the operating system's cache to cache data, we have to reserve enough memory for the system; in many cases the data is much larger than the index. >>> 3. If the performance of the machine is superior, you can set up multiple key buffers to allow different key buffers to cache special indexes.
  2. innodb buffer pool_size> indicates the size of the buffer pool bytes, InnoDB cache table and index data memory area. The default value of mysql is 128M. The maximum value depends on your CPU architecture. On 32-bit operating systems, the maximum value is 4294967295 (2 ^ 32-1). On 64-bit operating systems, the maximum value is 18446744073709551615 (2 ^ 64-1). > In 32-bit operating systems, the maximum size of the CPU and operating system is lower than the set maximum. If the size of the set buffer pool is greater than 1G, set the value of innodb buffer pool instances to be greater than 1.> * Data read and write is very fast in memory, innodb buffer pool size reduces the read and write to the disk. Only after the data is submitted or the checkpoint conditions are met, the memory data is flushed to the disk at one time. However, the memory is also used by the operating system or other processes of the database. Generally, the bufferpool size is set to 3/4 to 4/5 of the total memory. If not set properly, memory usage may be wasted or used too much. For busy servers, the buffer pool will be divided into multiple instances to improve system concurrency and reduce contention between read and write caches between threads. The size of the buffer pool is first affected by innodb buffer pool_instances, of course, the impact is smaller.
  3. query cache size> When mysql receives a query of select type, mysql will hash the query to get a hash value, and then use the hash value to match the query cache, if there is no match, then this The hash value is stored in a hash list, and the result set of the query is stored in the cache. Each hash node of the linked list that stores the hash value stores the address of the corresponding query result set in the cache, as well as some of the queries involved. Relevant information of the table; if the same query is matched by the hash value, the corresponding query result set in the cache is directly returned to the client. If any data in any table of mysql changes, it will notify the query cache that all the query caches related to the table are invalid and release the occupied memory address. >Advantages and disadvantages of query cache >> 1. Resource consumption caused by hash calculation of query statement and hash search. mysql will perform a hash calculation on each received query of the select type and find out whether the cache of the query exists. Although the efficiency of hash calculation and search is high enough, the consumption of a query can be ignored, but once it is involved High concurrency, when there are thousands of queries, the overhead caused by hash calculation and search is taken seriously; >> 2. The problem of query cache failure. If the table changes frequently, the failure rate of query cache will be very high. Table changes not only refer to the changes in the data in the table, but also include any changes in structure or index; >> 3. For different SQL but the same result set query will be cached, which will cause the transitional consumption of memory resources. The difference of sql character case, space or comment, the cache is considered to be different sql (because their hash value will be different); >> 4. Unreasonable setting of related parameters will cause a lot of memory fragmentation, related parameter settings will Introduce later.
  4. read buffer size> is the size of MySQL read buffer. Requests for a sequential scan of the table will allocate a read buffer, and MySQL will allocate a memory buffer for it. The read buffer size variable controls the size of this buffer. If the sequential scan request for the table is very frequent, and you think that the frequent scan is too slow, you can improve its performance by increasing the variable value and the size of the memory buffer.

14. There is only one field of type varchar (N) in a table, utf8 encoding, what is the maximum value of N?

  • Because each character of utf8 takes up to 3 bytes. The length of the MySQL definition line cannot exceed 65535, so the maximum value of N is calculated as: (65535-1-2) / 3. The reason for subtracting 1 is that the actual storage starts from the second byte, and the reason for subtracting 2 is because the actual character length is to be stored in the list length, and divided by 3 is because of the utf8 limitation: each character takes up to 3 bytes .

15. Advantages and disadvantages of (select *) and (select all fields)

  1. (Select *) To parse the data dictionary, (select all fields) do not need
  2. The result output order, (select *) is the same as the column order of the table creation, (select all fields) in the order of the specified fields.
  3. Rename the table field, (select *) does not need to be modified, (select all fields) needs to be changed
  4. (Select all fields) can be indexed for optimization, (select *) cannot be optimized
  5. (Select all fields) is more readable than (select *)

16. Similarities and differences between having clause and where

  1. Syntactically: where uses the column names in the table, while using select the result alias
  2. Affect the result range: where reads the number of rows of data from the table, having returns the number of rows of the client
  3. Index: where can use index, having can not use index, can only operate on temporary result set
  4. The aggregate function cannot be used after where, and having is used exclusively.

17. Insert when MySQL record does not exist, update when record exists

INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEYUPDATE c=c+1;

18. Select syntax for insert and update in MySQL

insert into student (stuid,stuname,deptid) 
select 10,'zhangsan',3
from student where stuid > 8; 
update student a inner join student b 
on b.stuID=10 seta. stuname=concat(b.stuname, b.stuID) 
where a.stuID=10 ;
Published 140 original articles · 49 praises · 10,000+ views

Guess you like

Origin blog.csdn.net/double_happy111/article/details/105517958