MySQL-MySQL tutorial

Source: Internet
Author: User
Best Choice for MySQL data types: bitsCN.com

Optimal choice of MySQL data types

It is important to carefully select a data type. why? It can improve performance and the principle is as follows:

● Storage (memory, disk), thus saving I/O (retrieval of the same data)

● Computing, reducing the CPU load

There are three types of data: numbers, characters, and periods.

(I) numbers

① Category

② It makes no sense to specify the width for the integer type. Simply put, it is also to display the number of characters and the humanization point.

Because, for storage and computing, INT (1) and INT (100) are the same

③ For floating point, we recommend that you only specify the data type, instead of the precision.

④ If possible, it is used only for precise decimal calculations, such as the storage of financial data.

However, some financial software in Tianchao uses float as the wage type in its database.

The ugly display and contempt of naked capitalists !!!

⑤ When the data volume is large, convert the real number type to the integer type.

The reason is simple:

● Inaccurate floating point

● Fixed-point computing is expensive

For example:

To store financial data accurate to one thousandth, you can multiply all amounts by 1 million, and then there is a BIGINT

(Ii) string

① Category

② How data is stored depends on the storage engine

Char and varchar may be different in memory and disk of different engines

However, for char, it is the same in filling and intercepting, because,

This is done at the Server layer.

③ Common char application scenarios

● A short string or all values are close to the same length, such as the MD5 value of the stored password

● Columns that are frequently changed and contain less fragments due to char

● Very short columns, such as gender and char (1), are more efficient. after all, varchar (1) wastes 1 byte.

④ It's tricky with character sets.

CSDN has a very popular post: Does someone think unicode is equivalent to UTF-8? Click to view discussion posts

I have no idea how to discuss it, because these two are two different things at all.

Unicode is character encoding.

Utf8 is a character set

We recommend that you use varchar for Unicode character sets and wall cracking.

Obviously, utf8 is also Unicode encoded, and, with the international trend, utf8 is the first choice

The result is that, in the competition between char and varchar, varchar is the biggest winner.

You can view the character sets supported by MySQL Unicode and the current character sets in the following ways

[Plain]

Mysql> select CHARACTER_SET_NAME from character_sets

-> Where DESCRIPTION like '% Unicode %'/G;

* *************************** 1. row ***************************

CHARACTER_SET_NAME: utf8

* *************************** 2. row ***************************

CHARACTER_SET_NAME: ucs2

* *************************** 3. row ***************************

CHARACTER_SET_NAME: utf8mb4

* *************************** 4. row ***************************

CHARACTER_SET_NAME: utf16

* *************************** 5. row ***************************

CHARACTER_SET_NAME: utf32

5 rows in set (0.06 sec)

Mysql> status;

--------------

Mysql Ver 14.14 Distrib 5.5.16, for Linux (i686) using EditLine wrapper

Connection id: 1

Current database: information_schema

Current user: root @ localhost

SSL: Not in use

Current pager: stdout

Using outfile :''

Using delimiter :;

Server version: 5.5.16-log Source distribution

Protocol version: 10

Connection: Localhost via UNIX socket

Server characterset: latin1

Db characterset: utf8

Client characterset: utf8

Conn. characterset: utf8

UNIX socket:/tmp/mysql. sock

Uptime: 2 min 9 sec

⑤ Varchar is recommended for InnoDB

Now InnoDB has been used in many occasions, and varchar has won again.

⑥ Store big data

● BLOB: binary storage, no character set or sorting rules

● TEXT: Character storage, character sets, and sorting rules

It is best to avoid using the two. if possible, you can separate the blob text column into a separate table.

When BLOB or TEXT is used to delete or update a large number of objects,

We recommend that you use optimize table fragment regularly.

(Iii) date and time types

Common examples include datatime, timestamp, and date.

In terms of storage, the timestamp occupies 4 bytes and the other two 8 bytes.

Therefore, if there is no accident, it is best to use timestamp whenever possible, even if it is more naughty, such as time zone-related

BitsCN.com

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.