当前位置:网站首页>Mysql database design

Mysql database design

2022-06-22 22:41:00 xcrj

theory

E-R (entity-relationship): Entity 、 attribute 、 relation (1:1, 1:n, m:n)
CDM (Conceptual Data Model): Conceptual data model
PDM (Physical Data Model): Physical data model

1NF normal form :

  • Field atomicity , Entity attributes cannot be further divided

2NF normal form :

  • Part of it depends on , Each non candidate key can be directly distinguished by candidate keys
  • stay 1NF On the basis of normal form, the partial dependence of non primary attribute on primary code is eliminated

3NF normal form :

  • Transitive dependency , Any non primary attribute does not depend on other non primary attributes
  • stay 2NF On the basis of eliminating - Transitive dependency

summary

Do you want to use a database , With what kind of database , How to use database

Monomers , Distributed , data type , Data volume , Concurrency , efficiency , Security

A database is just a way to store data , There is also a certain use cost ; In some cases, you can change the data storage method , such as JSON file 、Excel Forms even txt file
json Storage and proto Storage can be cross language ,json The storage database type is text ,proto Format storage is binary
Conform to the paradigm , Add redundancy ( Speed up data processing )
Suo Yin
Add view ( Increase speed , After the table is split union View )

Security : Master slave copy 、mysql colony
efficiency : Read / write separation 、 Sub database and sub table 、mysql colony

Be careful

no need mysql Keyword as field name , The name of the table
No foreign keys , Use code to ensure consistency between tables

database

database : Storage engine 、 code 、 Sort rule

Storage engine :MyISAM And InnoDB

name lock Business Indexes
InnoDB Row lock Support There is no limit to
MyISAM Table locking I won't support it most 64 An index , The total number of field bytes in the index must not be greater than 1000 bytes
Merge Premise :MYISAM engine && The table structure is consistent , Single table unique indexes are supported

code :

  • utf-8mb4 (most bytes 4bytes): Use of English 1 Bytes , Use of Chinese 3 Two bytes to encode ; Special character expression , Wide characters 4 byte ; Emoticons can be stored
  • utf-8: English and symbols account for 1 Bytes , Chinese and symbols account for 3 Bytes , The number is one byte
  • unicode: chinese 、 All in English 2 Bytes
  • utf-16: In general, both English and Chinese need to occupy 2 Bytes ;unicode The Chinese characters in the extension area occupy 4 Bytes
  • utf-32: All characters 4 Bytes

Sort rule :

Sort rule advantage shortcoming other
utf8_general_ci More efficient Poor correctness Can accept ß = s( single s) instead of ss( double ss) Then use ;ß = s
utf8_unicode_ci Low efficiency High accuracy German and French ,utf8_unicode_ci It worked well ;ß = ss

surface

What data can be put into the table
classification : Basic table 、 Extended table 、 Association table
name

Refer to the website
The data volume of a single table shall be controlled within 500 Within ten thousand lines

  • 500 Wanxing is not mysql Database limitations , Modify table structure , Backup , Recovery limits
  • Historical data archiving , Solve the problem that the log data is too large
  • Sub database and sub table , Solution table is too large

Use caution mysql Partition table , Choose the partition key carefully ??

  • Logically, it is represented as a table , Physically, it is represented as multiple files
  • It is suggested to use physical sub-table to manage big data

Do not use mysql Large binary files are stored in the database

  • Large files often need to be random IO Operation is too time consuming
  • mysql Store address in URL that will do
  • picture 、 Files are stored on a dedicated file server

Do not do database stress tests online

Relationship

  • one-on-one : Either party puts the primary key of the other party
  • One to many : Put the primary key of the one party in the multiple party
  • Many to many : To create a separate table, you need to create an associated table

surface / name

Prefixes are used to distinguish between system tables and business tables
The suffix represents the classification of the table

Field

Field

  • classification : General fields 、 Business fields
  • Field type and size 、 The default values are null and index
  • Field name , type , length 、 Width 、 With or without symbols , The default value is , Allow null , Indexes
  • Primary key 、 Foreign keys
  • Redundant fields

Design

  1. General fields 、 Business fields are separated
  2. Use programs instead of foreign keys
  3. The space is right :
    • The type is precise , multi-purpose int、char;
    • The length is accurate , Fixed length ;
    • With or without symbols unsigned
    • Avoid using text,blob
    • The larger the field type , Indexing takes up more space , The fewer inodes a page box can store , When traversing the disk IO The more
    • The inode in the disk will load the page box in memory
  4. If possible, define all columns as not null
    • The generation of a clustered index requires that it be unique and non empty

Field / data type

Refer to the website
data type

Type name Storage size Number of bytes Introduce
bit Storage bit value ,0 or 1, The longest 64 position
char Width :0~850-255 bytes Fixed length characters , The display becomes longer , Space occupation fixed length ;255/3=85 The Chinese characters
varchar Width :0~5000~2 ten thousand 0-65535 bytes Variable length characters , The display becomes longer , The space occupation becomes longer ;65535/3=21845 The Chinese characters ; The Ali display is the widest 5000
binary contain 0 or 1 character
varbinary contain 0 or 1 character
bigint8 byte Unsigned value :0 To about 10 Of 19 Power Often used as primary key
tinyint1 byte Unsigned value :0 To 255 Often used as a dictionary type
int4 byte Unsigned value :0 To about 43 Billion
smallint2 byte Unsigned value :0 To 65535
mediumint3 byte Unsigned :0 To 16777215(1 thousand 600 ten thousand )
double8bytes(2.22…E-308,1.79… E+308) Double precision , Floating point numbers
decimal(a,b)16bytesa Maximum 38,b stay 0~a Between decimal(10, 7): The total length is 10, Decimal length 7
float4bytes(1.17…E-38,3.40…E+38) Single precision , Floating point numbers
tinytext255bytes0-65 535 bytes255/3=85 The Chinese characters
text65535/1024=64K0-65535 bytes65535/3=21845 The Chinese characters , about 2 Ten thousand Chinese characters
mediumtext16777215/1024/1024=16M0-16 777 215 bytes16777215/3=5592405 The Chinese characters ,559 Ten thousand Chinese characters
longtext4294967295/1024/1024/1024=4G0-4 294 967 295 bytes4294967295/3=1431655765 The Chinese characters ,14 Hundred million Chinese characters
TINYBLOB255bytes0-255 bytes No more than 255 Binary string of characters
BLOB63.9KB0-65 535 bytes Long text data in binary form
MEDIUMBLOB15.9MB0-16 777 215 bytes Medium length text data in binary form
LONGBLOB3.9GB0-4 294 967 295 bytes Maximum text data in binary form

Be careful :

  • mysql varchar(3) The number in describes the number of characters, not bytes :, No matter in Chinese english Numbers All are 3 individual .
    • 4.0 Version below ,varchar(100), refer to 100 byte , If stored UTF8 Chinese character time , Can only save 33 individual ( Every Chinese character 3 byte )
    • 5.0 Above version ,varchar(100), refer to 100 character , It doesn't matter what the numbers are 、 The letters are still UTF8 Chinese characters ( Every Chinese character 3 byte ), All can be stored 100 individual .
  • BINARY and VARBINARY Be similar to CHAR and VARCHAR, The first two contain binary strings, that is, byte strings instead of character strings
  • Integer type as long as the type is set , The storage size is determined by the type, not by the display width
  • navicat The number in parentheses after the integer type in , Indicates the display width , Does not indicate storage length , With this varchar、char The following numbers have different meanings .
  • Longitude and latitude :decimal(10, 7) It only needs to be accurate to the decimal point 7 position , The precision is 1CM, therefore , The longitude and latitude stored in the database are decimal(10, 7) that will do .
  • money :decimal(15, 2) One trillion . Corner score ; Or go directly to the integer of the fraction ,
  • You can store decimals and integers separately
  • varchar: Maximum display width 5000
  • bigint(20) Auto increment as primary key
  • tinyint(0~255) Make a dictionary
  • text:2 Ten thousand Chinese characters

Date time :

Type name Range Format Introduce byte
datetime1000-01-01 00:00:00/9999-12-31 23:59:59YYYY-MM-DD HH:MM:SS Ali rules datatime; Whether to update according to the current time ; by null Stored as null8 byte
timestamp1970-01-01 00:00:00/2038YYYYMMDD HHMMSS Time stamp ;timestamp Will store the time zone ; by null Hour is the current time 4 byte
time‘-838:59:59’/‘838:59:59’HH:MM:SS
year1901/2155YYYY
date1000-01-01/9999-12-31YYYY-MM-DD

Be careful :

  • Time default :CURRENT_TIMESTAMP
  • create_time Use datetime, Use only when creating
  • update_time Use datetime, This field needs to be updated when updating

Field / Public fields

part

id(bigint(20), Primary key , Self increasing , Unsigned )
create_time(datetime) : Create with 1 Time 
update_time(datetime) : Update according to the current time , The default value is CURRENT_TIMESTAMP
tenant_id(bigint(20), Unsigned ): Tenant ID

All

id(bigint(20), Primary key , Self increasing , Unsigned )

create_time(datetime) : Create with 1 Time 
update_time(datetime) : Update according to the current time , The default value is CURRENT_TIMESTAMP
delete_time(datetime): Use when deleting 1 Time 

create_by(char(30)): The creator 
update_by(char(30)): Update the 
delete_by(char(30)): Deleted by 

tenant_id(bigint(20), Unsigned ): Tenant ID
data_scope: Under the same tenant , The range of data 

is_enabled(tiny_int(1), Unsigned ): notes : Is it enabled?  1:true Enable  0:flase Discontinue use   Default :1  
is_deleted(tiny_int(1), Unsigned ): notes : Whether or not to delete  1:true Delete  0:false Not delete   Default :0  

Field / The default value is

Avoid the business implications of field defaults
Directly set the default value of the field to the common value

Field / name

Primary key

Environmental classification : Monomers 、 Distributed
generating algorithm :UUID、 Snowflake algorithm
Generation method :mysql Self increasing 、java Code generation 、ORM Frame generation

innoDB Database engine , Use int Auto increment as primary key

Refer to the website

classification explain
mysql Since the primary key innoDB
mysql Bring their own uuid
ORM Framework auto insert snowflake algorithm
java Generate uuid、 Snowflake algorithm
  1. Design experience :
  • Use auto increment primary key ; It is not convenient to obtain the data generated by the database id, The frame returns the inserted data object
  • Snowflake algorithm :bigint(20)
  • uuid:varchar(36)

Foreign keys

The primary key of the primary table serves as the foreign key of the secondary table
Constraints on deletion : When deleting master table records , From the actions in the table

  • CASCADE( cascade ): When deleting the primary key table , Delete from the table
  • RESTRICT/NO ACTION: Constrain master table from table - Records in the main table are sometimes in foreign keys , Not delete
  • SET NULL: Constrain master table from table - Records in the main table are sometimes in foreign keys , Set the value of the foreign key from the table to null

Indexes

  1. Index building , Build a good index
    • Union index left , Highly differentiated ( Priority index left ), The most frequently queried , The field length is small (1 Pages can store more inodes )
    • varchar Field indexing , The length must be set , length =20 Time zone division =90%, count(distinct left( Name , Index length ))/count(*)
  2. Index usage , Try to use indexes for queries
  3. Single table index is recommended not to exceed 5 individual
  4. It is forbidden to create a separate index for each column in a table
  5. Each table must have a primary key
  6. General development and use Ordinary index and unique index are OK
  7. Set a unique index for the mobile number ; Set the ID card as a unique index
  8. The table corresponding to the data dictionary sys_dict Build more indexes
  9. Relational query join Field indexing

Index principle

  • B+ Trees
  • establish B+ The tree process is time-consuming
  • Build a good B+ Trees , Help to improve query speed

The index name :

  • primary key :pk_ Field name
  • unique index :uk_ Field name
  • General index :idx_ Field name
  • Composite index :idx_ Field name 1_ Field name 2

Business

Sub database and sub table

Time to divide the warehouse and table :523 principle , The number of single table rows exceeds 500 The capacity of ten thousand rows or single table exceeds 2GB; It is estimated that the data volume in three years will not reach this level at all , Please do not divide the database and tables when you create the tables .
sub-treasury : Requests are published to different libraries for processing , The pressure of the same requisition table has not decreased .
table : The quantity is appropriate

table / Horizontal split

Refer to the website

Method 1 And methods 3 Combine the methods to divide the tables

  1. It is estimated that there will be a large amount of data and frequently accessed tables in advance , Divide it into several tables : Mapping method surface id-(hash Algorithm 、 Remainder algorithm )- Sub table name
    • I didn't think of what to do in advance
    • Changing the algorithm destroys the correspondence
  2. merge Storage engine :TYPE=MERGE UNION=(user1,user2) INSERT_METHOD=LAST
    • First, there are several storage engines MYISAM Table of + Their table structure is consistent
    • One merge Tables cannot be maintained on the entire table unique Indexes , Can only be maintained on a single table unique Indexes . When you execute a insert, The data enters the sub table ( Depending on insert_method The value of the option )

table / Split Vertically

Split by business , Split the business into more details

Example

Transitive dependency :​employee Table has depart_code,depart_name;depart Table has depart_code,depart_name; If depart In the table depart_name Changed the ,employee In the table depart_name What do I do
object database :mysql Is a structured database , Binary file entities such as images and files are put into the object database ,mysql Just put the link address in
Primary key :mybatis-plus The snowflake algorithm defaults to int type 19 position , So when designing a database bigint(20)
Field : Required fields for tree directory ,id parent

Example / The data dictionary

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for sys_dict_type
-- ----------------------------
DROP TABLE IF EXISTS `sys_dict_type`;
CREATE TABLE `sys_dict_type`  (
  `dict_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT ' Dictionary key ',
  `dict_name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT ' Dictionary name ',
  `dict_type` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT ' Dictionary type ',
  `status` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '0' COMMENT ' state (0 normal  1 Discontinue use )',
  `create_by` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT ' The creator ',
  `create_time` datetime NULL DEFAULT NULL COMMENT ' Creation time ',
  `update_by` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT ' Update the ',
  `update_time` datetime NULL DEFAULT NULL COMMENT ' Update time ',
  `remark` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT ' remarks ',
  PRIMARY KEY (`dict_id`) USING BTREE,
  UNIQUE INDEX `dict_type`(`dict_type`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 12 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = ' Dictionary type table ' ROW_FORMAT = DYNAMIC;
-- ----------------------------
-- Table structure for sys_dict_data
-- ----------------------------
DROP TABLE IF EXISTS `sys_dict_data`;
CREATE TABLE `sys_dict_data`  (
  `dict_code` bigint(20) NOT NULL AUTO_INCREMENT COMMENT ' Dictionary code ',
  `dict_type` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT ' Dictionary type ',
  `dict_label` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT ' Dictionary labels ',
  `dict_value` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT ' Dictionary key value ',
  `is_default` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT 'N' COMMENT ' Default (Y yes  N no )',
  `dict_sort` int(11) NULL DEFAULT 0 COMMENT ' Dictionary sort ',
  `status` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '0' COMMENT ' state (0 normal  1 Discontinue use )',
  `create_by` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT ' The creator ',
  `create_time` datetime NULL DEFAULT NULL COMMENT ' Creation time ',
  `update_by` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT ' Update the ',
  `update_time` datetime NULL DEFAULT NULL COMMENT ' Update time ',
  `remark` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT ' remarks ',
  PRIMARY KEY (`dict_code`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 40 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = ' Dictionary data table ' ROW_FORMAT = DYNAMIC;

SET FOREIGN_KEY_CHECKS = 1;

Be careful

  1. dict_type Add a unique prefix , A dictionary that distinguishes between different businesses

Reference documents

《 Alibaba Taishan version Java Development Manual 》

原网站

版权声明
本文为[xcrj]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221743049897.html