MySQL日常使用过程中经历过的坑,持续更新
目录
- MySQL 5.6 版本不支持View中存在Subquery
- view外层尽量不要再使用where条件
- MySQL来管理记录数据更新时间
- Not In (sub-query), sub-query中不能有Null
- 长查询首先使用explain extended分析执行逻辑
- view中尽量避免使用union all
- 新建readonly slave应对长查询
- waiting for table flush
- 查看进程状态
- SQL
- SQL Mode: only_full_group_by
- Error writing file '/opt/mis/tmpdb/MYeblCvF' (Errcode: 28 - No space left on device):
- 连接数监控
MySQL 5.6 版本不支持View中存在Subquery
Error Code: 1349. View’s SELECT contains a subquery in the FROM clause
view外层尽量不要再使用where条件
view外面再加where条件是需要先等view内检索条件执行完毕创建temp table之后再对table过滤,所以where条件中即使有Index也不起作用。多用explain extended去分析执行逻辑。
MySQL来管理记录数据更新时间
logtime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
不要使用代码插入时间,这样能保证数据的准确性,尤其当多个工具都连接这个数据库并修改数据时候。
Not In (sub-query), sub-query中不能有Null
与Null比较的结果是Unknown,需要避免与Null比较。如果查询中需要使用Is Not Null。
长查询首先使用explain extended分析执行逻辑
关注 type=ALL, rows数字比较大,key
view中尽量避免使用union all
如果view创建后仍需一些条件过滤使用,view中不要使用union all,拆分为两个独立的view,在应用端实现数据合并。
新建readonly slave应对长查询
slave地理位置靠近查询源
waiting for table flush
查看哪些操作正在写入导致需要等table flush,运行下面命令
SHOW ENGINE INNODB STATUS
查看进程状态
show full processlist
重点关注进程运行时间,state等
SQL
left join如果右边表有多行,则左表数据会重复。
SQL Mode: only_full_group_by
当sql_mode=only_full_group_by, 使用group by时候,必须指明select 列的聚合max or min or avg。
expression #16 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘mytable.updated’ which is not functionally dependent on columns in GROUP BY clause;
this is incompatible with sql_mode=only_full_group_by"}
Error writing file ‘/opt/mis/tmpdb/MYeblCvF’ (Errcode: 28 - No space left on device):
执行一些长查询时候抛出的错误。
连接数监控
查看MySQL的连接数,是否有过多连接。