Database specification must-see

About library:

  1. [Mandatory] library name must be controlled within 32 characters, English lowercase all.

  2. [Mandatory] library name format: name of business system _ subsystem name is not recommended shulan prefix.

  3. [Mandatory] library name only letters, numbers, underscores, and the English letter.

  4. [Forced] to create must explicitly specify the character set of the database, and can only be utf8 character set or utf8mb4. Create a database SQL example: Create database db1 default character set utf8;

  5. [Proposed] temporary library, table name  tmp_ as a prefix and suffix to date, the backup repository to the table  bak_ as a prefix and suffix to date. Zhengzhou three infertility hospital: http: //mobile.zzfkyy120.com/

About Table

  1. [Mandatory] table and column names must be controlled within 32 characters, table names can only use letters, numbers and underscores, all lowercase.

  2. [Mandatory] Module name table name requires a strong correlation module uses the same table names to make use of the unified prefix.

  3. When you create a table [to force] you must explicitly specify the character set to utf8 or utf8mb4.

  4. [Column Name] do not try to force the keyword (such as type, order, etc.).

  5. Table must be explicitly specified storage engine type when [forced] to create the table, if no special needs, all for the InnoDB.

  6. [Forced] to build the table must have a comment. http://www.chacha8.cn/detail/1132398236.html

  7. [Forced] to more than 100W for a large table rows alter table, it must be reviewed by DBA, and execute business-peak period, more needs to alter together. 
    Alter table because the table will have a lock, blocking period for all write the table, for business could have a drastic impact.

  8. [Recommendations on the primary key of the table when the building: table must have a primary key 
    (1) primary key mandatory id, int type or bigint, and is recommended to use unsigned auto_increment unsigned.

    (2) identify each row in the main table fields do not set the primary key, it is recommended to other fields such as user_id, order_id, etc., and to establish unique key index. 
    If it provided as a primary key and the master key is randomly inserted will cause the page innodb internal division and large number of random I / O, performance degradation.

  9. [Proposed] core tables (such as user table) must have created time field create_time rows of data and last update time field update_time, easy to look into the problem.

  10. [Proposed] table as far as possible all fields are NOT NULL attribute, DEFAULT business value can be defined as required. 
    Because there will be a NULL value for each row takes up additional storage space, data migration error-prone, aggregate function variations, and other calculations.

  11. [Proposed] middle of the table used to hold intermediate result sets, the name must  tmp_ begin with. Backup table for backup or grab a snapshot of the source table name must bak_begin with. Backup middle of the table and the table cleaned regularly.

  12. [Demonstration] a more standardized construction of the table statement:


    1. CREATE TABLE user_info (

    2.  `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键',

    3.  `user_id` bigint(11) NOT NULL COMMENT '用户id',

    4.  `username` varchar(45) NOT NULL COMMENT '真实姓名',

    5.  `email` varchar(30) NOT NULL COMMENT '用户邮箱',

    6.  `nickname` varchar(45) NOT NULL COMMENT '昵称',

    7.  `birthday` date NOT NULL COMMENT '生日',

    8.  `sex` tinyint(4) DEFAULT '0' COMMENT '性别',

    9.  `short_introduce` varchar(150) DEFAULT NULL COMMENT '一句话介绍自己,最多50个汉字',

    10.  `user_resume` varchar(300) NOT NULL COMMENT '用户提交的简历存放地址',

    11.  `user_register_ip` int NOT NULL COMMENT '用户注册时的源ip',

    12.  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',

    13.  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',

    14.  `user_review_status` tinyint NOT NULL COMMENT '用户资料审核状态,1为通过,2为审核中,3为未通过,4为还未提交审核',

    15.  PRIMARY KEY (`id`),

    16.  UNIQUE KEY `uniq_user_id` (`user_id`),

    17.  KEY `idx_username`(`username`),

    18.  KEY `idx_create_time_status`(`create_time`,`user_review_status`)

    19. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='网站用户基本信息'

About Index

  1. [] InnoDB table must be forced primary key id int / bigint auto_increment, and the updated primary key is prohibited.

  2. [Mandatory] InnoDB and MyISAM storage engine tables, index type must be BTREE.

  3. [Proposed] name of the primary key in  pk_ the beginning, the only key to  uniq_ or  uk_ at the beginning, the general index at  idx_ the beginning, always use lowercase to name or abbreviation fields as a suffix.

  4. [Recommendations on a single table index number can not exceed eight.

  5. [Proposed] in the index, but to consider the establishment of joint index and the highest degree of distinction on the front of the field. Userid The column may be calculated from the degree of differentiation select count (distinct userid).

  6. [Proposed] in the multi-table join SQL, the guarantee on the index driven table join column, so join the highest efficiency.

  7. 【建议】建表或加索引时,保证表里互相不存在冗余索引。 
    对于MySQL来说,如果表里已经存在key(a,b),则key(a)为冗余索引,需要删除。

SQL编写

  1. 【强制】程序端SELECT语句必须指定具体字段名称,禁止写成 *。

  2. 【强制】程序端insert语句指定具体字段名称,不要写成insert into t1 values(…)。

  3. 【强制】除静态表或小表(100行以内),DML语句必须有where条件,且使用索引查找。

  4. 【强制】where条件里等号左右字段类型必须一致,否则无法利用索引。

  5. 【强制】WHERE 子句中禁止只使用全模糊的LIKE条件进行查找,必须有其他等值或范围查询条件,否则无法利用索引。

  6. 【强制】索引列不要使用函数或表达式,否则无法利用索引。如where length(name)=’Admin’或where user_id+2=10023。

  7. 【建议】insert into…values(XX),(XX),(XX).. 这里XX的值不要超过5000个。 
    值过多虽然上线很很快,但会引起主从同步延迟。

  8. 【建议】SELECT语句不要使用UNION,推荐使用UNION ALL,并且UNION子句个数限制在5个以内。 郑州看不孕不育哪家好:http://yyk.39.net/zz3/zonghe/1d427.html
    因为union all不需要去重,节省数据库资源,提高性能。

  9. 【强制】禁止跨db的join语句。

  10. 【建议】不建议使用子查询,建议将子查询SQL拆开结合程序多次查询,或使用join来代替子查询。

  11. 【建议】线上环境,多表join不要超过5个表。

  12. 【建议】在多表join中,尽量选取结果集较小的表作为驱动表,来join其他表。

  13. 【建议】批量操作数据时,需要控制事务处理间隔时间,进行必要的sleep。

  14. 建议】事务里包含SQL不超过5个 
    因为过长的事务会导致锁数据较久,MySQL内部缓存、连接消耗过多等问题。

  15. 【建议】事务里更新语句尽量基于主键或unique key,如update … where id=XX; 
    否则会产生间隙锁,内部扩大锁定范围,导致系统性能下降,产生死锁。

  16. 【建议】减少使用order by,和业务沟通能不排序就不排序,或将排序放到程序端去做。Order by、group by、distinct这些语句较为耗费CPU,数据库的CPU资源是极其宝贵的。

  17. 【建议】order by、group by、distinct这些SQL尽量利用索引直接检索出排序好的数据。如where a=1 order by b可以利用key(a,b)。

  18. 【建议】包含了order by、group by、distinct这些查询的语句,where条件过滤出来的结果集请保持在1000行以内,否则SQL会很慢。


Guess you like

Origin blog.51cto.com/14510269/2442940