Detailed explanation of MYSQL database table structure optimization method

Detailed explanation of MYSQL database table structure optimization method

This article uses an example to illustrate the method of optimizing the MYSQL database table structure. Share with you for your reference, the details are as follows:

Choose the right data type

1. Use the smallest data type that can hold your data

2. Use simple data types. Int is easier to process in MySQL than varchar type.

3. Use not null to define fields as much as possible

4. Use text type as little as possible. If you have to use it, it is best to consider splitting the table

Use int to store date and time, and use FROM_UNIXTIME() [Convert int type timestamp to date and time format], UNIX_TIMESTAMP() [Convert date and time format to int type] to convert

Use bigint to store IP addresses, and use INET_ATON() [Convert IP format to int], INET_NTOA() [Convert int format to normal IP format] to convert

Normalization and denormalization of tables

Normalization refers to the specification of database design. The current normalization generally refers to the third design paradigm, which requires that there are no non-key fields in the data table for any candidate key fields.

The transfer function dependency conforms to the third normal form.

Tables that do not conform to the third normal form have the following problems:

1. Data redundancy: (classification, classification description) records will be kept for each product

2. Data insertion/update/deletion exceptions

Normalized operation:

Denormalization means to increase redundancy appropriately for tables that originally conform to the third normal form for the sake of query efficiency and considerations, so as to achieve the purpose of optimizing query efficiency. Denormalization is an operation that exchanges space for time.

example:

Denormalize the table

Denormalize order information:

Vertical split of table

Vertical splitting is to split the original table with many columns into multiple tables, which solves the problem of table width. Usually vertical splitting can be done according to the following principles:

1. Store infrequently used fields in a separate table.

2. Store large fields independently in a table.

3. Put fields that are often used together.

Horizontal Split

The purpose of horizontal splitting of tables is to solve the problem of too much data in a single table. The structure of each table in the horizontal split is exactly the same.

Commonly used horizontal splitting methods:

1. Perform a hash operation on customer_id. If you want to split it into 5 tables, use mod(custoneer_id,5) to extract 0-4 values.

2. Store data in different tables for different hashIDs

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL common functions summary", "MySQL log operation skills", "MySQL transaction operation skills summary", "MySQL stored procedure skills" and "MySQL database lock related skills summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • Basic operations of MySQL data tables: table structure operations, field operation example analysis
  • How to compare two database table structures in mysql
  • 3 methods to restore table structure from frm file in mysql [recommended]
  • Detailed explanation of Linux mysqldump exporting database, data, and table structure
  • MySQL uses procedure analyse() function to optimize table structure
  • A simple method to export table structure script using Navicat for MySQL
  • Mysql method of copying table structure and table data
  • Some things to note when modifying the table structure in MySQL
  • Summary of MySQL table structure modification commands
  • How to quickly modify the table structure of MySQL table

<<:  Node.js makes a simple crawler case tutorial

>>:  Detailed explanation of generic cases in TypeScript

Recommend

Use Grafana+Prometheus to monitor MySQL service performance

Prometheus (also called Prometheus) official webs...

Let's talk about the size and length limits of various objects in MySQL

Table of contents Identifier length limit Length ...

Convert XHTML CSS pages to printer pages

<br />In the past, creating a printer-friend...

How to set the style of ordered and unordered list items in CSS

In an unordered list ul>li, the symbol of an u...

Master the CSS property display:flow-root declaration in one article

byzhangxinxu from https://www.zhangxinxu.com/word...

How to change the host name in Linux

1. View the current host name [root@fangjian ~]# ...

How to use VirtualBox to build a local virtual machine environment on Mac

1. Big Data and Hadoop To study and learn about b...

Detailed steps to implement the Excel import function in Vue

1. Front-end-led implementation steps The first s...

How to install and deploy gitlab server on centos7

I am using centos 7 64bit system here. I have tri...

Mysql5.7.14 Linux version password forgotten perfect solution

In the /etc/my.conf file, add the following line ...

HTTP header information interpretation and analysis (detailed summary)

HTTP Header Explanation 1. Accept: Tells the web s...

Detailed tutorial on installing MySQL 8.0.19 in zip version on win10

Table of contents 1. After downloading, unzip it ...

How to implement the prototype pattern in JavaScript

Overview The prototype pattern refers to the type...