Mysql 5.7优化
为了达到数据库胡最佳性能
1. 普通用户通过配置软件与硬件来实现
2. 高级用户会寻求机会改善MySQL本身,开发自己的数据存储引擎,硬件应用。
在数据库层面的优化
1. 表设计,通常列有适合的数据类型,表有适合的列。例子,写操作多的应有通常有多点表,少点列。
大量的数据分析的,会多点列,少些表。
2. 建立适合的索引。
3. 使用适合的存储引擎。
优化select语句。
例子:
优化InnoDB Queries
如果列中不可能有NULL,声明为NOT NULL更有利于优化器
不要单独建立二级索引,因为每次查询只使用一个索引。尝试使用组合索引。
行级锁:InnoDB多用户,高并发,在线计算
为了防止死锁,InnoDB可以自动发现死锁条件,回滚。
在高并发系统里,这个机制导致缓慢。所以disable 死锁检测,依靠innodb_lock_wait_timeout, innodb_deadlock_detect这个可以关掉。
https://dev.mysql.com/doc/refman/5.7/en/table-locking.html
To specify that all updates issued in a specific connection should be done with low priority, set the low_priority_updates
server system variable equal to 1.
When tuning a MySQL server, the two most important variables to configure are key_buffer_size
and table_open_cache
. You should first feel confident that you have these set appropriately before trying to change any other variables.
使用视图的注意事项是,视图是根据你定义的联表查询规则生产的临时表,当你基于视图来做各种条件查询,他会首先联表生成一张大表,那就相当于全表扫描,性能很差,生成完临时表后才筛选你的条件。如果是你自己灵活查询,在联表之前你就可以用条件筛选出很少的记录,避免了全表扫描。
show index from [table]
查看 cardinality
有时mysql会挂掉
tail /var/log/mysql/error.log
2018-07-29T06:04:29.385989Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2018-07-29T06:04:29.386000Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2018-07-29T06:04:29.386006Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2018-07-29T06:04:29.386013Z 0 [ERROR] Failed to initialize builtin plugins.
2018-07-29T06:04:29.386017Z 0 [ERROR] Aborting
可以尝试修改
/etc/mysql/mysql.conf.d/mysqld.cnf
sudo service mysql restart
record lock + gap lock = next_try_lock