当前位置:网站首页>Mysql database development specification
Mysql database development specification
2022-07-28 17:30:00 【Joshua02】
Table building related :
1、 Create database character set selection utf8mb4, Sort set selection utf8mb4_general_ci; Build table 、 Index building 、 There is no need to specify a character set for operations such as adding fields , The default is the character set of the database .
Example :create database [if not exists] dbname default character set UTF8MB4 default collate utf8mb4_general_ci;
2、 Library name 、 Table name 、 The field name cannot exceed 32 Characters . The number of fields in a single table should not exceed 50 individual , Big data wide table exception .
3、 Do not use complex data types ( Array , Customization, etc ); No use ENUM、SET type , Use TINYINT Instead of ;
4、 The field length depends on the actual business needs , Set the length as small as possible ;
5、 Each column must be defined with comments;
6、 The table must have a primary key , It is suggested to use snowflake algorithm to generate , The type is bigInt(19) unsigned;
7、 Precise floating-point data storage , Need to use DECIMAL, No use FLOAT and DOUBLE.
8、 Unified time storage timestamp ( Millisecond value ), Field type is bigint(19);
9、 The amount is saved in the database to Fen , Field type is bigint(19);
10、 The number of characters in a line ( The total length of characters in each column ) May exceed 15000 At the upper limit , To consider the use of TEXT Type storage , Another suggestion is TEXT Split the field from the original table , Associated with the original table with a primary key .
11、 It is forbidden to store plaintext password in database , Encrypt the password and store it .
12、 Avoid using stored procedures 、 trigger 、EVENT.
13、 It is suggested that there are eight redundant fields in the main business table :
`version` bigint(19) NOT NULL DEFAULT '0' COMMENT ' Version number , Optimism lock ',
`creator_id` bigint(19) NOT NULL DEFAULT '0' COMMENT ' founder ',
`creator_name` varchar(30) DEFAULT '' COMMENT ' Creator name ',
`create_time` bigint(19) NOT NULL DEFAULT '0' COMMENT ' Creation time ',
`modifier_id` bigint(19) NOT NULL DEFAULT '0' COMMENT ' Modifier ',
`modifier_name` varchar(30) DEFAULT '' COMMENT ' Name of modifier ',
`modify_time` bigint(19) NOT NULL DEFAULT '0' COMMENT ' Modification time ',
`is_deleted` tinyint(1) NOT NULL DEFAULT b'0' COMMENT ' Delete logo ',
Optimization :
1、 No use select * The query .
2、 need join Field of , There must be an index on the field , And the data type is absolutely consistent , avoid Block Nested Loop And implicit transformation ;
3、 Try to avoid using unequal queries , for example :NOT、!=、<>、!<、!>、NOT EXISTS、NOT IN、NOT LIKE etc. .
4、 For multi value matching of the same field in(), And pay attention to control IN The number of median values , Suggest less than 300 individual .
Example :select id,name,deptname from emp where deptname in ('clerk','manager','sales');
5、 because GROUP BY It has the functions of grouping and automatic sorting , In the business logic scenario without sorting , Use Order by NULL Avoid sorting .
Example :select ...from tab group by col1 order by null;
6、WHERE In the condition, there is no mathematical operation or function operation on the index column , If necessary, you can use related functions on some columns , But in Where It is strictly forbidden to use any function on the left of the predicate expression in Clause , Including data type conversion functions .
The wrong way to write :
select createtime
from .. .
where date_format(createtime, '%Y%m%d %H:%i:%s')= '20090101 00:00:0';
Correct writing :
select date_format(createtime, '%Y%m%d %H:%i:%s')
from .. .
where createtime =str_to_date('20090101 00:00:00', '%Y%m%d %H:%i:s');
7、 Do not use select And from Use subqueries , It is suggested to convert it into equivalent table associations in the scenario of using sub queries ;
Wrong writing :
Select col1,(select col2 from b where b.id = a.bid) bcol2 from a where a.name=’xxxx’;
Write it correctly :
Select a.col1,b.clo2 from a left join b on a.bid = b.id where a.name=’xxxx’;
Index related :
1、 Naming rules :
Primary key :pk_ Table name ;
UNIQUE Indexes :uk_ Table name _ Field name ( Or field abbreviations );
Indexes : Non unique cable idx_ Table name _ Field name ( Or field abbreviations );
Composite index :idx_ Table name _ Field abbreviations , Take abbreviations that represent meaning as much as possible , Such as business_contract surface customerid and customername Index on :idx_business_contract_cid_cname.
2、 Foreign keys are not allowed , But the associated field must have an index ;
3、 It is suggested that the number of indexes in a single table should be controlled at 5 Within a ;
4、 It is recommended that the number of fields in the index should not exceed 5 individual ;
5、 about where after and Multiple conditions for , Try to use combined index , It is forbidden to build an index for each condition .
6、 In the composite index , The column corresponding to the most frequently used equivalence condition is placed in front of the index , The column corresponding to the range condition is placed at the end of the index
7、 Try to put the column with small field length on the leftmost side of the composite index
8、 Avoid duplicate indexes 、 Redundant index , redundancy / Duplicate index
Duplicate index example :primary key(id)、index(id)、unique index(id);
Examples of redundant indexes :index(a,b,c)、index(a,b)、index(a);
Last , Good habits , Can reduce the 90% Of BUG
边栏推荐
- The actual combat of the beego framework of goweb development: Section III program execution process analysis
- Mysql database addition, deletion, modification and query (detailed explanation of basic operation commands)
- Differences between CNSA and CASC and CASIC
- Vscode界面介绍
- Using SQL server agent job to restore the database regularly
- Goweb开发之Beego框架实战:第三节 程序执行流程分析
- High speed circuit design practice -- Overview
- 【kibana】问题整理 kibana 7.x No indices match pattern “apm-*“
- wpf命令按钮透明样式
- Verilog daily question (vl27 settable counter)
猜你喜欢

Goweb开发之Beego框架实战:第四节 数据库配置及连接

我为什么选择使用Go语言?

Selection and application of capacitor in high speed circuit -- detailed explanation

Shell脚本之免交互操作

Goweb开发之Beego框架实战:第三节 程序执行流程分析

mysql实现按照自定义(指定顺序)排序
![[atlas] atlas compilation error sorting (all)](/img/bb/2ca51dffc7a09b378be4e581280246.png)
[atlas] atlas compilation error sorting (all)

Verilog 每日一题 (VL27 可置位计数器)

Analysis of kubernetes service principle

kubernetes service 原理解析
随机推荐
异步FIFO基本原理(基于Verilog的简单实现)
MySQL数据库增删改查(基础操作命令详解)
Verilog 每日一题(VL14 自动贩售机1--FSM常见题型)
Encountered.Sqlite file processing during Android Development
火了 2 年的服务网格究竟给微服务带来了什么?(转载)
格雷码和二进制的转换及典型例题(4bits格雷码计数器)
Verilog 每日一题 (VL24 多bit MUX同步器 跨时域输出)
List of supplementary questions
MySQL PgSQL realizes the merging of multiple lines of records into one line, grouping and merging, and dividing with specified characters
LNMP source code compilation and installation
Zero foundation uses unity3d to develop AR applications and download 3D models remotely
异步电路设计--同步脉冲器原理及例题
The practice of beego framework developed by goweb: Section 4 database configuration and connection
Role of Fortress machine
The actual combat of the beego framework of goweb development: Section III program execution process analysis
Goweb开发之Beego框架实战:第一节 Beego框架介绍
【impala】【报错解决】 Impala cannot read or execute the parent directory of dfs.domain.socket.path的解决方法
【CDH】通过 ClouderaManager 配置CDH组件用 prometheus 监控采集JMX信息
侦察机与预警机的区别
Janus series article 3 API usage guide videoroom creating a new video room