MySQL Performance optimization data types and instances --- 2

Select the character set

Directly determines the character set encoding data stored in MySQL, for the same content using different character sets representing the amount of space will vary greatly, so by using the appropriate character set that can help us reduce the data as much as possible amount, thereby reducing the number of IO operations.
Contents 1. Pure Latin characters can be represented, no need to select a different character encodings other than latin1, because it would save a lot of storage space.
2. If you do not need to be stored in multiple languages we can determine, there is no need to have to use other UNICODE or UTF8 character type, this time caused a lot of wasted storage space.
3.MySQL can be accurate to data type field, so when we need a large database stored in multi-byte data, different data types can be used to a large extent reduce the amount of data stored in different fields in different tables, thereby reducing IO number of operations and improve the cache hit rate.

Select the primary key

Recommend the use of a surrogate primary key, not with a business couple, easier to maintain
a surrogate primary key: business-related, meaningless sequence of numbers
natural primary key: things property in the natural uniquely identify
a majority of the table, preferably all tables, universal key strategy can reduce the number of source code needs to be written, the system to reduce total cost of ownership

Data type optimization

1. Small data, good data
should try to use the smallest data type that can store data correctly, smaller data types are generally faster because they take up less disk, memory, and CPU cache, and requires less CPU cycles when processing
2. avoid null
may be optimized for performing sql null columns so that when the index, and the statistical value comparison is more complicated
3. more appropriate data type
1, the integer operation is lower than the cost of the character, since the character set and collation rules are complex character comparison than the integer
2, using mysql self-built type. Such can not be used to store the date and time of the string
3 can make IP address stored as an integer , with INET_ATON () function converts INET_NTOA and between the two representations. The advantage is reduced storage space, the disadvantage is less readable

SELECT INET_ATON('192.168.23.4');
SELECT INET_NTOA(3232241412);

4. TINYINT, SMALLINT, MEDIUMINT, the INT, BIGINT were used 8,16,24,32,64 bit memory space. Needs to make use of a minimum data type
5 in accordance with the query speed: char> VARCHAR> text
char fixed length, i.e. length of each data byte space occupied by the like; the maximum length is 255 characters, suitable for use in ID number, phone number other fixed-length string of
variable degree varchar, the maximum length may be provided; maximum size is 65535 bytes, suitable for use in the variable length attribute of
text length is not set, does not know when the maximum length of the attribute, with the appropriate text
. 6. BLOB and TEXT values as a separate object processing. String type designed for storing large data respectively stored binary and character.
7. enumeration class
can sometimes be used instead of the usual enumeration class string type, mysql enum types are stored in a very compact, it will be compressed to one or two bytes in the data list of values, mysql inside each will save the value of the position in the list is an integer and stored in .frm file table - lookup table mapping relationship of "numeric string"

create table enum_test(e enum('fish','apple','dog') not null);--创建表字段为枚举类型
insert into enum_test(e) values('fish'),('dog'),('apple');--插入数据
select e+0 from enum_test;--查询
select * from enum_test where e = 0;--查询
select * from enum_test where e = 'fish';--查询

8.datetime和timestamp
datetime

8 bytes
regardless of the time zone, time zone configuration underlying database of invalid datetime
saved to millisecond
can save a large range of time
not to store the date string type, a large space, ease of loss of function of the date of type

timestamp

4 bytes
time: 1970-01-01 to 2038-01-19
accurate to the second
integer stores
the time zone-dependent set of databases
automatically updates the timestamp value of the column

date

Ratio of the number of bytes occupied by the string, datetime, int less storage, use date only three bytes of type
using the date type may also be calculated by using the date and time between the date function
date type for storing 1000-01- date of between 01 9999-12-31

Paradigm and counter-paradigm

You do not have to strictly abide by the paradigm and counter-paradigm, the actual analysis based on actual business. Necessary redundancy field can reduce a lot of the associated table

Paradigm

advantage:

The paradigm Model updates are usually faster than trans
when better data normalization, little or no duplication of data
normalization the data is relatively small, can be placed in memory, faster operating
drawbacks:
usually needs to be associated

Anti paradigm

advantage:

All data in the same table, to avoid association
can design an effective index
Disadvantages:
more redundancy in the table, delete data when the table will cause some useful information is lost

Storage engine of choice

Storage Engine

Appropriate split

The large field type TEXT fields such as large or large type VARCHAR, access to this table if not accessing the main field, it is broken in a separate table to another, to reduce the storage space occupied by the frequently used data.
This allows the number of pieces of data in each data block may be stored is increased, the number of the IO reduce the physical, cache hit rate can be improved memory.

Guess you like

Origin www.cnblogs.com/farmersun/p/12483816.html