Interviewer: Tell me about the design should pay attention to what mysql table

introduction

Due to the recent review of the content of what mysql, and some experience. Readily speaking part of knowledge, they are some of their usual cigarettes brother and a summary of the work experience. It reads, in fact, be able to avoid a lot of the pit. And many problems are the real deal will be asked to interview the!
such as
img

OK, we have the following specific issues

  • 1, why must set a primary key?
  • 2, you use the primary key is incremented or UUID?
  • 3, primary keys, why not have a business meaning recommend?
  • 4, do not indicate why the field enumeration enum type?
  • 5. What type of monetary field use?
  • 6. What type of field time with?
  • 7, why not direct high-capacity storage pictures, audio, video and other content?
  • 8, why should the field is defined as NOT NULL?

In fact, the above these problems, my first idea was that every problem can be a long-winded article. Later, due to conscience, smoke brother decided to speak to understand these issues with the article.
Of course, I gave the answer may not be the standard answer, after all, it is a summary of some of their own work. Our readers have a better answer, please share!

Here I have to say, I used only with mysql innodbstorage engine, the other engine really not used. So my answer is based innodbstorage engine.

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:? Primary key is incremented or UUID
answer : 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:
img

, 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? "

ps: This question, if you can UUIDspeak a reasonable justification is 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, 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.

Question 4: Why enumerated field without representation enum type?
Answer: enumerations work in the fields, usually with tinyinttype.
Why not enum type it? For two reasons
(1) low 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
img

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 Inttype. If you persist in using the yuan with Decimal.
Do not answer floatand double, because 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 1234567.23, you will find displays of data becomes 1234567.25, the accuracy of inaccurate!

Question 6: What type of field time with?
Answer: This question has no fixed answer, should be combined with their own project background to answer! The reason it clear on the line!
(1) varchar, if the memory type varchar time, 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 2013111the data, is that representatives January 11, 2013, or November 1, 2013?
Second, the time to do comparison operation, you need to use STR_TO_DATEother 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 four-byte integer type, it represents the time range of 08:00:01 to 1970-01-01 2038-01-1911: 14: 07 2038 a later time, is not with the timestamptype of storage.
But it has an advantage timestamptype 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, stored datetime 8 bytes, it is stored in the time ~ 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 time stamp indicating range than timestampmost, that is, to maintain their own, is not easy.

Question 7:? Why not just store pictures, audio, video and other large-volume content
Answer: We in practical applications, are used HDFSto store files. Then mysql, only keep the storage path of the file. mysql has been used to design two types of fields to store large files, i.e. text, and blobtype. 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) binlogtoo much content. Because you data content is relatively large, it will cause binlogmore content. As we all know, is a master-slave synchronization by binlogsynchronizing, binlogtoo, will lead to the master-slave synchronization problems efficiency!

Therefore, not recommended textand blobtype!

Question 8: Why field is defined as NOT NULL?
Answer: the OK, the problem from two angles A
bad (1) Index Performance

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 of"

(2) the query will be some unpredictable results
here 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

id name
1 Lonely smoke
3
5 Towards fat
7

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.

Guess you like

Origin www.cnblogs.com/XuChengNotes/p/11747499.html