目录
4.建表时增加comment来描述字段和表的含义(防止以后忘记)
5.建议包含create_time和update_time字段
一、MySQL8.0创建一张规范的表
例:
sql
CREATE TABLE student_info (
`id` INT NOT NULL AUTO_INCREMENT COMMENT '主键',
`stu_name` VARCHAR(10) NOT NULL DEFAULT '' COMMENT '姓名',
`stu_class` VARCHAR(10) NOT NULL DEFAULT '' COMMENT '班级',
`stu_num` INT NOT NULL DEFAULT '0' COMMENT '学号',
`stu_score` SMALLINT UNSIGNED NOT NULL DEFAULT '0' COMMENT '总分',
`tuition` DECIMAL(5, 2) NOT NULL DEFAULT '0' COMMENT '学费',
`phone_number` VARCHAR(20) NOT NULL DEFAULT '0' COMMENT '电话号码',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录创建时间',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '记录更新时间',
`status` TINYINT NOT NULL DEFAULT '1' COMMENT '1代表记录有效,0代表记录无效',
PRIMARY KEY (`id`),
UNIQUE KEY uniq_stu_num (`stu_num`),
KEY idx_stu_score (`stu_score`),
KEY idx_update_time_tuition (`update_time`, `tuition`)
) ENGINE = INNODB charset = utf8mb4 COMMENT '学生信息表';
1.表、字段全采用小写
(0 区分大小写,1 不区分大小写)
2.int类型不再加上最大显示宽度
3.每张表必须显式定义自增int类型的主键
如果innodb没有显示定义主键,可能会选择唯一索引作为主键,唯一索引在写入的过程可能不是递增的,写入数据时可能会导致数据页频繁分裂,从而导致写入效率低和页空间浪费。另外主键能保证每一行的唯一性,在日常使用也会很方便,提升查询效率、归档处理。
4.建表时增加comment来描述字段和表的含义(防止以后忘记)
5.建议包含create_time和update_time字段
用于审计和跟踪,方便查看创建更新、时间,分析问题方便。
数据归档也可以根据这两个字段处理。
6.核心业务增加记录标记字段
7.用尽量少的存储空间来存储一个字段的数据
例:能用int就不varchar/char, 能用tinyint就不用int.
8.存储精确浮点数必须使用DECIMAL
9.尽量能不使用TEXT、BLOB类型
浪费磁盘空间,影响性能
如果某一个字段必须使用,建议单独建一张表
10.添加合适的索引增加查询速度
11.字符集建议使用utf8mb4
如果应用程序使用多种语言,使用utf8mb4可以防止乱码,支持多种语言
12.存储引擎建议使用InnoDB
绝大多数情况使用:主持事务、行锁、在线备份、支持MVCC多版本并发控制
13.但表字段数目建议小于30
列越多,越影响性能,结构变得越复杂,不利于维护和管理
二、MySQL8.0需要调整的参数
sql
###配置文件举例
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
## dir set
datadir = /data/mysql/data
innodb_data_home_dir = /data/mysql/data
innodb_log_group_home_dir = /data/mysql/data
log-bin = /data/mysql/binlog/mysql-bin
max_binlog_size = 1G
log_bin_index = /data/mysql/binlog/mysql-bin.index
relay-log = /data/mysql/binlog/mysql-relay-bin
tmpdir = /data/mysql/tmpdir
slow_query_log_file = /data/mysql/log/mysql-slow.log
general_log_file = /data/mysql/log/mysql-general.log
log-error = /data/mysql/log/mysql.err
## slave and binlog
server_id = 6666
skip-slave-start = 0
read_only = 0
binlog_format = row
log_slave_updates = 1
master_info_repository = table
relay_log_info_repository = table
relay_log_purge = 1
relay_log_recovery = 1
sync_binlog = 100
binlog_cache_size = 1M
binlog_expire_logs_seconds = 2592000
log_bin_trust_function_creators = 1
slave_net_timeout=60
#binlog_error_action="IGNORE_ERROR"
innodb_autoinc_lock_mode=1
##
back_log = 200
bulk_insert_buffer_size = 8M
#character-set-server = utf8
lower_case_table_names = 1
## 基线
local-infile = off
skip-networking = off
skip-name-resolve = on
## connect
max_allowed_packet = 32M
max_connect_errors = 1000
max_connections = 1000
wait_timeout = 3600
interactive_timeout = 3600
table_open_cache = 4096
thread_cache_size = 64
thread_stack = 192K
transaction_isolation = REPEATABLE-READ #
pid-file = mysql.pid
## slow
slow_query_log = 1
long_query_time = 1
log_slow_admin_statements
log_queries_not_using_indexes = 0
slow_launch_time = 1
read_buffer_size = 4M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 32M
tmp_table_size = 128M
max_heap_table_size = 128M
default_storage_engine = innodb
explicit_defaults_for_timestamp = on
## innodb
innodb_buffer_pool_size = 1G
innodb_max_dirty_pages_pct = 80
innodb_thread_concurrency = 8
innodb_buffer_pool_instances = 2
innodb_flush_log_at_trx_commit = 2
innodb_read_io_threads = 8
innodb_write_io_threads = 4
innodb_io_capacity = 1000
innodb_io_capacity_max = 2000
innodb_lru_scan_depth = 1024
innodb_use_native_aio = 1
innodb_flush_neighbors = 1
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_data_file_path=ibdata:1G:autoextend
innodb_log_files_in_group = 3
innodb_log_file_size = 2G
innodb_file_per_table = 1
innodb_flush_method = O_DIRECT
innodb_strict_mode = 1
innodb_lock_wait_timeout = 30
innodb_log_buffer_size = 16M
innodb_adaptive_flushing = 1
innodb_change_buffering = all
innodb_purge_threads = 4
innodb_purge_batch_size = 300
innodb_old_blocks_time = 1
innodb_fast_shutdown = 0
performance_schema = 1
innodb_print_all_deadlocks = 1
innodb_sort_buffer_size = 4M
innodb_page_size = 16k
gtid_mode=on
enforce_gtid_consistency=on
table_open_cache_instances=16
binlog_rows_query_log_events=1
## pasword
default_password_lifetime=0
[mysqldump]
quick
max_allowed_packet = 32M
[mysql]
no-auto-rehash
prompt=\p@\d>\_
[mysqld_safe]
open-files-limit = 28192
[mysqlhotcopy]
interactive-timeout
1.缓冲池大小和数量
innodb_buffer_pool_size = 1G 在机器内存的60%~80%
需要留一部分内存给系统。
2.最大连接客户端数(默认151,通常不够用)
max_connections = 1000
3.Binlog相关参数
bin_log路径
binlog_format = row 行模式
binlog_expire_logs_seconds = 2592000 (30天自动清除)
log_slave_updates = 1 主从库开启复制,建议全部开启
4.数据目录
配置单独的数据盘,提高查询效率
5.服务ID
server_id = ( 建议设备IP后两段,防止同一机房ID冲突)
6.只读参数
read_only = 0 (一般主库关闭0,从库开启1)
7.双一参数
sync_binlog = 100 执行100个事务再落盘 (一致性要求高就减小数值)