mysql optimize data type

1. The smaller is usually better

Selecting a minimum range type does not exceed

2. Simple enough

For example, the integer operation is lower than the cost of the character, since the character set and collation (collation) to make the comparison more complicated than character comparison shaping.

3. Try to avoid null

If the query contains the column can be null, optimized for mysql is harder, because the index can be made null columns, indexes and statistical value comparison is more complicated.

More storage space can be used to null columns in the mysql also require special handling. When the index may be null columns, each index requires an additional byte record, even in the myISAM may result in an index of a fixed size (e.g., only an integer column index) becomes an index of variable size.

Usually can be changed to bring to not null null column performance is relatively small, so no tuning necessary first to find and edit out the case in the existing schema. Unless you are sure this can cause problems. However, if you plan to build an index on the column, it should be avoided designed to be as null columns.

There are exceptions, InnoDB use a separate bit storage null value, so good for sparse data space efficiency. But this does not apply to myISAM.

 

4. integer type:

tinyint(8),smallint(16),mediumint(24),int(32),bigint(64)

mysql can specify the width integer types, such as int (11), for most applications it makes no sense: it does not limit the scope of legal value, but provides a number of interactive tools mysql (for example, the mysql command-line client) with to display the number of characters.

Real type:

Not only for storing the fractional part; may be used bigint larger than the stored decimal integer. mysql supports both the exact type, and also supports imprecise type.

float and double, are directly supported cpu floating-point calculations, it is significantly faster floating-point operations.

exact decimal type for storing a decimal.

And decimal floating-point type can be specified accuracy. For decimal column, you can specify the maximum number of digits allowed before and after the decimal point. This affects the spatial column consumption. mysql 5.0 and later saved to a digital packed binary string (4 bytes per memory 9 digits). For example each memory decimal (18,9) on both sides of nine decimal digits, using a total of nine bytes: digit before the decimal point 4 bytes, the number after the decimal point 4 bytes, one byte decimal itself.

There are multiple ways to specify the desired columns floating point accuracy, which would make mysql quietly select different data types, or the values ​​are stored at choice. The accuracy of the definition is non-standard, so we recommend only the development of data-type is specified accuracy.

Because of the extra space and computing overhead, so you should try to use only decimal store financial data, such as only when the mice were accurate calculation. In larger amount of data, they can be considered instead of bigint with decimal, the need to store currency unit can be multiplied by the corresponding multiple of the number of bits of the decimal.

 

Guess you like

Origin www.cnblogs.com/lccsblog/p/12632145.html