当前位置:网站首页>MySql data table structure optimization summary
MySql data table structure optimization summary
2022-08-06 06:10:00 【helpless coder】
- Data type optimization:
- Field data types:
You should try to use the smallest data type that can store the data correctly, smaller data types are usually faster because they take up less disk, memory, and CPU cache, and require less CPU for processingFewer cycles, but make sure you don't underestimate the range of values you need to store, and if you can't identify which data type, choose the smallest type that you think won't exceed the range
Case:
Design two tables, design different data types, and view the capacity of the table
If the query contains nullable columns, it is difficult to optimize for mysql, because nullable columns make indexing, index statistics and value comparison more complicated, frankly, usually nullThe performance improvement brought by changing the column to not null is relatively small, so there is no need to modify the schema of all tables, but it should be avoided as much as possible to design a nullable column, and a null field can be changed to an empty characterstring.
Integer types:
Several integer types that can be used: TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT use 8, 16, 24, 32, 64-bit storage space respectively.Try to use the smallest data type that meets your needs
String and character type
Char has a fixed length, that is, each piece of data occupies an equal-length byte space; the maximum length is 255 characters, which is suitable for ID card numbers, mobile phone numbers, etc.Fixed-length string
varchar is variable, and the maximum length can be set; the maximum space is 65535 bytes, which is suitable for use in variable-length attributes
text does not set the length, whenWhen you don't know the maximum length of the attribute, it is suitable to use text
according to the query speed: char>varchar>text
BLOB and TEXT types
MySQL treats each BLOB and TEXT value as a separate object.
Both are string types designed to store large data, using binary and character storage respectively.
datetime and timestamp
Do not use string types to store datetime data
Datetime types usually take up less storage space than strings
Date and time types can be compared using dates when searching and filtering
Date and time types also have rich processing functions, which can easily calculate dates for time types
Using int to store date and time is not as good as using timestamp type
Use enumeration instead of character type
Sometimes you can use enumeration class instead of common string type, MySQL storage enumeration type will be very compact, it will be compressed into one according to the data of list valueor two bytes, mysql internally saves the position of each value in the list as an integer, and saves a lookup table of the "number-string" mapping in the table's .frm file
create table enum_test(e enum('fish','apple','dog') not null);
insert into enum_test(e) values('fish'),('dog'),('apple');
select e+0 from enum_test;
Special types of data
People often use varchar(15) to store ip addresses, however, its nature is a 32-bit unsigned integer not a string, you can use INET_ATON() and INET_NTOAThe function converts between these two representations
Case:
select inet_aton('1.1.1.1')
select inet_ntoa(16843009)
- Field data types:
- Primary key selection:
- A surrogate primary key: a non-business-related, meaningless sequence of numbers
- Natural primary key: The natural unique identifier in the attribute of a thing
- It is recommended to use surrogate primary keys: they are not coupled to the business, so they are easier to maintain; a common key strategy for most tables, preferably all tables, can reduce the amount of source code that needs to be written and reduce the total cost of ownership of the system li>
- Storage engine selection comparison:

- Appropriate data sets:
- Independent small fields that are frequently referenced and can only be obtained by joining 2 (or more) large tables.
- In such a scenario, since each Join is only to obtain the value of a small field, and the records received by Join are large, it will cause a lot of unnecessary IO, which can be optimized by exchanging space for time.However, redundancy also needs to ensure that the consistency of the data will not be destroyed, and to ensure that the redundant fields are also updated during the update.
- Appropriate splitting: For example, in one table, there are fields that store too much data, or there are multiple fields that are not commonly used. The large storage fields and the fields that are not commonly used can be separately split into another table.
边栏推荐
猜你喜欢
随机推荐
桌面上随便创建一个word文档,打开它都是显示兼容模式的解决方法
selenium简单常见操作(部分WebdriverAPI)
jpa中orphanRemoval和cascade如何理解
3.2 定位shellcode
MapReduce总结
正则表达式
mysql优化查询细节及索引优化实例
shell脚本编写(3.修改文件内容)
二叉搜索树BST
lamp和lnmp的数据流向及区别
元宇宙是什么?元宇宙真的会改变我们的生活吗?
CDH6.3.2环境搭建
5.1 主机扫描:路由信息的收集
5.5 漏洞扫描:Web安全漏洞扫描及审计
搭建自己的V Rising自建服务器,以及常见的V Rising服务器问题解决方案
Docker 快速安装&搭建 Redis 环境
Unity 模型直接用 Scale 负数做镜像原理
揭示OT安全四大挑战!Fortinet 发布《2022年全球运营技术和网络安全态势报告》
四元数的简单应用
软件测试基本概念知识









