The amount of memory that MySQL occupies

Source: Internet
Author: User

1. mysql executes the query plan, Key_len represents the number of bytes used by the index, and the number of bytes is related to three conditions.
mysql> CREATE TABLE T1 (v1 char (10));
Query OK, 0 rows affected

Mysql> CREATE index INDEX_V1 on T1 (v1);
Query OK, 0 rows affected
records:0 duplicates:0 warnings:0

2, whether to allow NULL, allow NULL to be one more byte, identify whether the value is null. As follows:
mysql> ALTER TABLE t1 modify column V1 char (TEN) not null;
Query OK, 0 rows affected
records:0 duplicates:0 warnings:0

Mysql> DESC SELECT * from t1;
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| ID | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| 1 | Simple | T1 | Index | NULL | INDEX_V1 | 30 | NULL | 1 | Using Index |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
1 row in Set

mysql> ALTER TABLE t1 modify column V1 char (ten) null;
Query OK, 0 rows affected
records:0 duplicates:0 warnings:0

Mysql> DESC SELECT * from t1;
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| id | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| 1 | Simple | T1 | Index | NULL | INDEX_V1 | 31 | NULL | 1 | Using Index |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
1 Row in Set
3, whether it is variable length, longer than two bytes to represent the length, as follows:
mysql> ALTER TABLE t1 modify column v1 varchar () null;
Query OK, 0 rows affected
records:0 duplicates:0 warnings:0

Mysql> DESC SELECT * from t1;
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| ID | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| 1 | Simple | T1 | Index | NULL | INDEX_V1 | 33 | NULL | 1 | Using Index |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
1 row in Set

mysql> ALTER TABLE t1 modify column V1 char (ten) null;
Query OK, 0 rows affected
records:0 duplicates:0 warnings:0

Mysql> DESC SELECT * from t1;
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| id | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| 1 | Simple | T1 | Index | NULL | INDEX_V1 | 31 | NULL | 1 | Using Index |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
1 Row in Set
4, character type, n in varchar (n) is the number of characters, not how many bytes of memory are consumed. For different encodings, the same character takes up memory differently. Latin1 one character occupies 1 bytes, gb2312 one character occupies 2 bytes, utf8 one character occupies 3 bytes.
Mysql> ALTER TABLE t1 modify column V1 char (TEN) CharSet latin1;
Query OK, 0 rows affected
records:0 duplicates:0 warnings:0

Mysql> DESC SELECT * from t1;
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| ID | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| 1 | Simple | T1 | Index | NULL | INDEX_V1 | 11 | NULL | 1 | Using Index |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
1 row in Set

mysql> ALTER TABLE t1 modify column V1 char (TEN) CharSet gb2312;
Query OK, 0 rows affected
records:0 duplicates:0 warnings:0

Mysql> DESC SELECT * from t1;
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| ID | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| 1 | Simple | T1 | Index | NULL | INDEX_V1 | 21 | NULL | 1 | Using Index |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
1 row in Set

mysql> ALTER TABLE t1 modify column V1 char (TEN) charset UTF8;
Query OK, 0 rows affected
records:0 duplicates:0 warnings:0

Mysql> DESC SELECT * from t1;
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| ID | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| 1 | Simple | T1 | Index | NULL | INDEX_V1 | 31 | NULL | 1 | Using Index |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
1 row in Set

The amount of memory that MySQL occupies

Related Article

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.