Schema optimization and indexing

Source: Internet
Author: User

Integer

There are two numeric types in total: integers and real numbers (with a small number of parts). If you save an integer, use the following integer: Tinyint,smallint,mediumint,int or bigint. They require 8,16,24,32, and 64-bit storage space, respectively. The range of values they store is n is the number of bits required for the storage space they use.

An integral type has an optional parameter, which is unsigned, which does not allow a negative number to be stored, whereas the maximum upper limit of a positive number changes to about twice times before. For example, a tinyint unsigned storage range is 0-255, rather than the previous -128-127.

Signed and unsigned types use the same amount of storage space, and their performance is the same, so which type depends on the stored data range.

Your choice determines how MySQL stores data in memory and on your hard disk. However, even on a 32-bit architecture, integer calculations often use 64-bit bigint. (exceptions typically occur on aggregate functions that use decimal or double calculations).

MySQL allows you to specify the "width" of an integral type, such as int (11). It doesn't make sense for most applications: it doesn't constrain the range of values, but it can simply specify the number of characters for the MySQL interaction tool. Just to limit the display. For storage and calculation, int (1) and int (20) are equivalent.

Real

The number of the real numbers has a decimal portion. But they are not just decimals. It can also save some integers that are too large to hold bigint. MySQL also supports both precise and imprecise types.

float and double support approximate standard floating-point operations. If you need to know the exact results of floating-point operations, you need to study the floating-point implementations of your platform.

The decimal type stores accurate floating-point numbers. In the MySQL5.0 version, the exact operation is supported. MySQL4.1 earlier versions, a general floating-point operation would get a more bizarre value because the precision was lost. In these MySQL versions, Decimal is simply a storage type.

MySQL5.0 the above version of the server itself to run decimal operations because the CPU does not directly support this calculation. Floating-point operations are slightly faster because these operations are performed locally by the CPU.

Both floating-point and decimal types allow you to specify the precision. For decimal, you can specify the maximum number of digits before and after the decimal point. This determines the size of the column's consumption space. MySQL5.0 The above version packs the numbers into a binary string (nine digits per four bytes). For example, Decimal (18,9) stores 9 digits on both sides of the decimal, using a space of 9 bytes: 4 bytes Store the number before the decimal point, 1 bytes store the decimal point, and 4 bytes store the digits after the decimal point.

The number of digits in the highest upper limit of a decimal number in the MySQL5.0 version is 65. Earlier versions of MySQL were limited to 254, and the stored values were not compressed (one byte a number). However, in these versions, these large values cannot be computed because decimal is a storage type. To calculate, you can only convert decimal to double.

There are two ways to specify the precision of floating-point numbers, and different methods cause MySQL to choose different types and approximate storage values. The designation of precision is not standard, so we recommend specifying the type you want rather than the precision.

When you save the same range of values, floating-point types use less space than decimal. A float column uses 4 bytes. Double consumes 8 bytes and has a range of better precision and greater values. Like an integer, you only choose a storage type. MySQL uses a double for floating-point operations.

Because of the need for additional storage space and calculation of the consumption. You use the case of decimal for the exact results you need to use. For example, the financial information is stored.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.