MySql point summary of the interview development technology

1, why must set a primary key? 

 A: Because the case you do not set a primary key, innodb will help you generate a hidden column as the primary key increment. So you, anyway, have to generate a primary key, then you might as well own designated a primary key, in some cases, you can spend an explicit primary key index to improve query efficiency!

 

2, you use the primary key is incremented or UUID? 

 A: sure A increment ah. innodb primary key of a clustered index. If the primary key is the increment, then insert a new record each time, the recording will be sequentially added to the current position of the subsequent index node, when a filled, it will automatically open up a new page. If not increment primary keys, it might insert in the middle, it will lead to split pages, generate a lot of table fragmentation! .

  The above sentence can not read right, a big vernacular is: a good performance increment insert!
  Further, a test table attached to you, the table name is used with uuid uuid as the primary key. You look at the performance gap to know:

                

  , When the UUID is primary key when inserted as FIG longer, and more space!

  Amount, we must not forget that when you answer increment primary key, think about "auto-increment primary keys run out how to do?

 

3, primary keys, why not have a business meaning recommend? 

 A: There are two reasons

   (1) because of the possibility of any listed business meaning has changed, the primary key once to bring business meaning, then it is possible to change the primary key happen. Once the primary key change occurs, the data storage location on the disk change occurs, it may lead to page splitting, the creation of space debris.

  2) the primary key with a business meaning, not necessarily in order of increment. It will result in the insertion order of the data, it does not guarantee data inserted behind the main front is larger than a certain key data. If it does, the data is inserted behind the main key than previous small, it may lead to page splitting, the creation of space debris.

4, do not indicate why the field enumeration enum type? 

 A: represents the field enumeration at work, usually with tinyint type. Why not enum type it? The following two reasons

   Low (1) ENUM type ORDER BY operations efficiency, require additional operations
   (2) if the value of the enumeration values, there is a trap
    for example, the following table structure

CREATE TABLE test (foobar ENUM('0', '1', '2'));

  At this point, you execute the statement   

mysql> INSERT INTO test VALUES (1);

    Check out the results

         

    The results generated a pit father.
    Insert statement should look like so written, is inserted 1

mysql> INSERT INTO test VALUES (`1`);

 

5. What type of monetary field use? 

 A: If the monetary unit is divided, you can use Int type. If you insist on using yuan, with Decimal . Never A float and double , because the float and double stored in binary, so there is some error.

  Figuratively, you build a following column

CREATE TABLE `t` (
  `price` float(10,2) DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8

  Then insert a column to the price data 1,234,567.23, you will find displays of data becomes 1,234,567.25, precision inaccurate!

 

6. What type of field time with? 

 A: This problem no fixed answer, should be combined with their own project background to answer! The reason it clear on the line!   

  (. 1) varchar , type varchar if the time deposit, the advantage that the visual display. But also a lot of local pit. For example, insert the data no parity, one day you might just find a piece of data is the data of 2,013,111, is that representatives January 11, 2013, or November 1, 2013?

  Second, the time to do comparison operation, you need to use STR_TO_DATE other functions to convert it to the type of time, you will find that writing is not so hit the index. A large amount of data, is a pit!

  (2) timestamp , the type is an integer of four bytes, the time it is expressed to 1970-01-01 08:00:01 2038-01-1911: 14: 07 2038 a later time, can not be stored with a timestamp type.
    But it has an advantage, timestamp type is with time zone information. Once your system time zone changes, for example, you change the time zone

SET TIME_ZONE = "america/new_york";

   You will find that the value of the project in their own field changes will occur. This feature can be used to make a number of large international projects, application across time zones, with special attention!

  (. 3) datetime , datetime store 8 bytes, it is stored in the time range ~ 9999-12-31 00:00:00 to 1000-01-01 23:59:59.
   Clearly, greater storage time. But it is the pit where his memory is the absolute value of the time, with no time zone information. If you change the time zone database, the value is not changed yourself!

  (4) bigint , also 8 bytes that maintains a timestamp, the timestamp represents the range than most, is to maintain their own, is not easy.

7, why not direct high-capacity storage pictures, audio, video and other content? 

 A: We in practical applications, are used to store files with HDFS. Then mysql, only keep the storage path of the file. mysql has been used to design two types of fields to store large files, namely text and
blob types. However, we in the production, the basic need of these two types!
  There are two main reasons are as follows:

   (1) Mysql memory temporary table does not support TEXT, BLOB data types such as large, if the query contains such data, when sorting and other operations, you can not use memory temporary tables, you must use a temporary disk tables. Resulting in slow query efficiency
   (2) binlog too much content. Because you data content is relatively large, it will cause a binlog more content. As we all know, master-slave synchronization is synchronized, too binlog by binlog, it will lead to the master-slave synchronization problems efficiency! 
 Therefore, we do not recommend the use of text and blob types!

 

8, why should the field is defined as NOT NULL? 

 A: (1) poor performance index

  Mysql difficult to optimize the query column references may be empty, it will make the index, the index value statistics and more complex. Empty column may require more storage space, but also requires an internal mysql special handling. After the empty column may be indexed, each record requires an additional byte, but also cause index MYisam index becomes fixed size of variable size. - from "high performance mysql, Second Edition"
   (2) the query will be some unexpected results

    To give an example, everyone would understand. Suppose, the following table structure  

create table table_2 (
     `id` INT (11) NOT NULL,
    name varchar(20) NOT NULL
)

  Such table data is

  You execute the statement  

select count(name) from table_2;

  You will find that the result is 2, but in fact there are four data! Similar inquiries, in fact there are many, to name a few.
  Remember, because there are null columns, there will be a lot of unexpected results, wasting development time to troubleshoot Bug.

 Also refer to this understanding: https://www.cnblogs.com/myseries/p/11264178.html

 

 

Information Source: https: //www.itcodemonkey.com/article/15662.html

Guess you like

Origin www.cnblogs.com/myseries/p/11355495.html