Operation Python Mysql database engine and database tables to learn ape character set #

# Mysql database engine and character set table

![](./imgs/752951346A5F4E7EBDE362FA97107707.png)

1. The server ### processes client requests

In fact, both the client and server processes are which way to communicate, to achieve the final effect are: * The client process sends a text (MySQL statements) to the server process server process and then send some to the client process text (processing results). ** That server processes the request sent by the client process what has been done processing in order to produce the final result of the processing of it? The client can send to the server CRUD all kinds of requests, we are here to more complex queries as an example to show you roughly draw a map of the process:

![image](./imgs/167f4c7b99f87e1c.png)

> Although the query cache can sometimes improve system performance, but also for the maintenance of this cache had caused some overhead, such as every time the query cache to search queries processed to update the query cache, query cache maintenance of the corresponding memory area. Starting MySQL 5.7.20, the query cache is not recommended, and delete MySQL 8.0 in.

### 2. Storage Engine

`MySQL` server's data storage and retrieval operations are encapsulated into a storage engine is called the` `module. `` We know that the table is composed of records line by line, but this is only a logical concept, how to represent the physical records, how to read data from the table, how the data is written on a specific physical memory, which are `` storage engine is in charge of things. In order to achieve different functions, `MySQL` offers a wide variety of storage engine` `, the table storage structure different specific` `storage management engine may be different, access algorithm may be different.

> Storage engine formerly called `` processor table, its function is passed down the instruction receiving an upper layer, then extract the data table or a write operation.

To facilitate the management, connection management people to ``, `` query cache, `` parsing, query optimization `` These do not involve real data storage function division is `MySQL server` function, the function of real access to data `` divided into storage engine's features. A variety of different storage engines provide a unified call interface (that is, the storage engine API) to `MySQL server` layer on top, including dozens of low-level functions, such as" read the index of the first pieces of content "," read the index next content, "" insert record "and so on.

After it is completed in `MySQL server` query optimization, simply call the API provided by the underlying storage engine execution plan generation, access to data is returned to the client just fine.

`MySQL` supports a large variety of storage engines:

| Storage engine | Description |

| :---------: | :----------------------------------: |

| `ARCHIVE` | for data archiving (you can not modify the row is inserted) |

| `BLACKHOLE` | discard the write operation, a read operation will return empty content |

| `CSV` | when storing data, separated by commas individual data items |

| `FEDERATED` | used to access remote tables |

| `InnoDB` | with foreign key support transactional storage engine |

| `MEMORY` | placed in the memory table |

| `MERGE` | used to manage a collection of tables consisting of multiple MyISAM tables |

| `MyISAM` | major non-transactional storage engine |

| `NDB` | MySQL Cluster dedicated storage engine |

Difference ### 3, MyISAM and InnoDB table engine

#### 1) transaction support

MyISAM does not support transactions, while InnoDB support.

> Things: access and update data in the database execution unit. Things operate in either perform or not to perform

#### 2) Memory

MyISAM: Each MyISAM storage into three files on disk.

+ .Frm file stores the table structure.

+ .MYD file to store data.

+ .MYI file storage index.

InnoDB: two main types of file storage

+ .Frm storage table structure

+ .Ibd store and index data (possibly multiple .ibd file, or a separate file table space)

#### 3) differences lock table

** MyISAM: supports only table-level locking **, users myisam operating table, select, update, delete, insert statements are automatically locked to the table, in the case of concurrent insert table meet later if locked, you can watch tail insert new data.

** InnoDB: supports transactions and row-level locking, innodb is the most prominent feature **. Row lock greatly improve the multi-user concurrent operation of new energy. However, InnoDB row lock, except the primary key in the WHERE is an effective, non-primary key in the WHERE will lock the whole table.

#### 4) primary key

MyISAM: Table does not allow any index and primary key exists, the address line of the index are saved.

InnoDB: If no non-null primary key or unique index, will automatically generate a 6-byte main key (not visible to users), the data part of the main index, the index is stored an additional value of the main index. InnoDB larger primary key range is twice the maximum of MyISAM.

DETAILED #### 5 rows) of the table

MyISAM: save the number of rows in the table has, if the select count () from table; this value will be taken directly.

InnoDB: number of rows in the table is not saved (only traverse), if you use select count () from table; it will traverse the entire table, consumption is quite large, but added a condition after wehre, myisam way innodb and processing are the same.

#### 6) CURD operation

MyISAM: If you perform a lot of SELECT, MyISAM is a better choice.

InnoDB: If you perform a large number of data INSERT or UPDATE, for performance reasons, you should use InnoDB tables. DELETE from InnoDB performance better, but DELETE FROM table, InnoDB will not re-establish the table, but deleted line by line, on the innodb If you want to save a large number of empty tables of data, it is best to use truncate table command.

#### 7), key

MyISAM: not supported

InnoDB: Support

#### 8) query efficiency

MyISAM is relatively simple, so the efficiency is better than InnoDB, a small application, consider using MyISAM.

Recommended consider using InnoDB instead of MyISAM engine, InnoDB itself because a lot of good features, such as transaction support, stored procedures, views, row-level locking, and so, in many cases concurrently, I believe that the performance of InnoDB MyISAM is certainly better than a lot stronger .

In addition, any kind of table is not a panacea, only the right to choose the right type of business for the table type used to play the greatest performance advantage of MySQL. If it is not very complex Web applications, non-critical applications, you can still continue to consider MyISAM, and this situation can own discretion.

#### 9) both MyISAM and InnoDB scenarios:

MyISAM manage non-transactional table. It provides high-speed storage and retrieval, as well as full-text search capabilities. If the application needs to perform a large number of SELECT query, MyISAM is a better choice.

InnoDB for transaction processing applications, has many features including ACID transaction support. If the application needs to perform a lot of INSERT or UPDATE operation, you should use InnoDB, which can improve the performance of multi-user concurrent operations. Now used by default InnoDB.

### 4. look at the characters and garbled

#### Introduction to Character Set

We know only the binary data stored in the computer, then how to store strings it? Of course, it is to establish mapping between character and binary data, and the establishment of this relationship, at least to find out two things children:

1. What you want the character map into binary data?

  That is clearly defined range of characters.

2. how mapping?

  A character mapped to a binary data encoding process is called `` binary data to a mapping process of decoding a character called ``.

People abstract concept of a `` character set to describe the range of a character encoding rules

We look at the case of some common character sets:

- `ASCII` character set

  It contains a total of 128 characters, including spaces, punctuation, numbers, uppercase and lowercase letters, and some invisible characters. Because a total of only 128 characters, so you can use 1 byte to encode, we look at some of the character encoding:

  ```

  'L' -> 01001100 (Hex: 0x4C, decimal: 76)

  'M' -> 01001101 (Hex: 0x4D, Decimal: 77)

  ```

- `ISO 8859-1` character set

  Contains a total of 256 characters, is based on `ASCII` character sets and expanded 128 characters commonly used in Western Europe (including France and Germany letters), you can use a byte to be encoded. This character set also has an alias `latin1`.

- `GB2312` character set

  A collection of characters and the Latin alphabet, Greek letters, Japanese hiragana and katakana characters, Russian Cyrillic alphabet. Which included 6763 Chinese characters, other text symbols 682. At the same time this character set and `ASCII` compatible character sets, so the encoding bit strange:

  - If the character set in the character `ASCII` is coded in one byte.

  - Otherwise, a 2-byte code.

  This character indicates the number of bytes may require a different encoding is called `` variable length coding. For example the string '' love u'`, wherein ` 'love' 'needs to be encoded using two bytes, encoded hexadecimal as` 0xCED2`, `' u'` needed 1 byte encode, encoded in hexadecimal notation as `0x75`, so split up as` 0xCED275`.

  > Tips: how do we distinguish between a byte represents a single character or a part of a character represent? Remember `ASCII` character set includes only 128 characters, using 0 to 127 can represent all the characters, so if a byte is in the range of 0 to 127, it means that a byte represents a single character otherwise, that is two bytes represent a single character.

- `GBK` character set

  `GBK` character set only included the expansion of the range of characters were` GB2312` character set, `GB2312` on compatible encoding.

- `utf8` character set

  All characters included can think of on Earth, but also expanding. This `ASCII` compatible character set character set, using the variable length coding, a coded character requires 1-4 bytes, so for example:

  ```

  'L' -> 01001100 (Hex: 0x4C)

  'Ah' -> 111001011001010110001010 (Hex: 0xE5958A)

  ```

  > Tips: in fact, to be exact, utf8 encoding scheme is just a Unicode character set, Unicode character set can be used utf8, utf16, utf32 these types of coding schemes, utf8 using 1 to 4 bytes encoding a character, utf16 using two or four byte encoding a character, utf32 coded using a 4-byte character. More detailed knowledge is not the focus of this book and its Unicode encoding scheme, we look up Ha ~ MySQL does not distinguish between the concept of character set and encoding scheme, it is time to put behind nagging utf8, utf16, utf32 are as A character set treatment.

For the same character, different character sets may also have a different encoding. For example, for Chinese character '' I '' is, `ASCII` character set did not included this character,` utf8` and `gb2312` character set encoding of Chinese characters` `I as follows:

```

utf8 encoding: 111001101000100010010001 (3 bytes, hexadecimal notation is: 0xE68891)

gb2312 encoding: 1100111011010010 (2 bytes hexadecimal notation is: 0xCED2)

```

### 5.MySQL in utf8 and utf8mb4

We said above `utf8` character set requires the use of a character from 1 to 4 bytes, but we used some of the characters from 1 to 3 bytes can be expressed. In the character set MySQL` 'indicates the maximum byte length in some respects and can affect the performance of the storage system, so the design of `MySQL` uncle secretly define two concepts used by a character:

- `utf8mb3`: castrated` utf8` character set, only 1 to 3 bytes represent the character.

- `utf8mb4`:` utf8` authentic character set, using 1 to 4 bytes characters.

One thing we need is a note in the `MySQL`` utf8` alias `utf8mb3`, so it means that after the mention` utf8` use 1 to 3 bytes to represent a character in `MySQL` in If you have a case of using a 4-byte encoded characters, such as what to store some emoji expression, then use the `utf8mb4`.

#### Character Set View

`MySQL` a lot of good support for multiple character sets, see the current` MySQL` supported character sets can use this statement below:

```mysql

show charset;

```

 


 

Mastery learning method, as will bend to overtake!

Learning to ape: the achievements of their own just a boutique!

Guess you like

Origin www.cnblogs.com/itxdl/p/12530715.html