Interviewer: Tell me about MySQL table design should pay attention to what?

作者:孤独烟 出处: http://rjzheng.cnblogs.com/

Overview

MySQL a bit due to the recent review of the contents to see a good article, reproduced share. Read, in fact, be able to avoid a lot of the pit. And many problems are the real deal will be asked to interview the!

Specifically the following issues:

  1. Why must set a primary key?
  2. You or UUID primary key is self-growth?
  3. Why not have a primary key recommendation business meaning?
  4. Why do not represent an enum field enum type?
  5. What type of currency fields use?
  6. What time field type?
  7. Why not just store pictures, audio, video and other large-volume content?
  8. Why fields to be defined as NOT NULL?

text

Question 1: Why must set a primary key?

Answer: 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!

Question 2: The primary key is incremented or UUID use?

Answer: A self-affirmation by 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, you 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 keys, think about the increment primary keys run out how to do?
PS: This question, if you can tell the UUID reasonable grounds required.

Question 3: Why is not recommended to have a primary key business meaning?

Answer: 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) with a primary key business meaning and not necessarily the order of the self-energizing. It will result in the insertion order of the data, 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.

Question 4: Why enumerated field without representation enum type?

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

  • (1) Low ENUM type ORDER BY operations efficiency, requires additional operations;
  • (2) If the value of the enumeration values, there is a trap.

For example, the table structure is as follows:

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`);

Question 5: What type of monetary field use?

Answer: If the monetary unit is divided, you can use Int type. If you insist on using yuan, with Decimal. Never A float and double, float, and double as storage in binary, so there is some error.
Figuratively, you build a column as follows:

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!

Question 6: What type of time field use?

Answer: no fixed answer to this question, it 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 you change the system time zone occurs, 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 he maintains a timestamp, the timestamp represents the range than most, is to maintain their own, is not easy.

Question 7: Why not just store pictures, audio, video and other large-volume content?

Answer: We are in practical applications, are used to store files with HDFS. Then MySql, only keep the storage path of the file. MySQL has two field types are designed to store large files, that is, 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 tables do 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) too much binlog 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!

Question 8: Why field is defined as NOT NULL?

Answer: OK, this question from two angles A:

(1) Index performance is not good:

MySQL难以优化引用可空列查询,它会使索引、索引统计和值更加复杂。可空列需要更多的存储空间,还需要mysql内部进行特殊处理。可空列被索引后,每条记录都需要一个额外的字节,还能导致MYisam 中固定大小的索引变成可变大小的索引。
—— 出自《高性能mysql第二版》

(2) query some of the unexpected results.

To give an example, everyone would understand. Suppose, the table structure is as follows:

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

Table data is as follows:

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.

 

Published 154 original articles · won praise 404 · Views 650,000 +

Guess you like

Origin blog.csdn.net/Zhihua_W/article/details/96475290