MySQL/InnoDB tips & tricks

本文讨论的是 MySQL 5.7。

1、混用 * 和列名会产生解析错误,要避免这个错误,要使用 tbl_name.* 的格式。

2、where 子句中不能使用 select 中定义的别名,因为 SQL 先解析 where 再解析 select。

3、移除字段首尾的空格:update tble_name set col = trim(col)。

4、MySQL 会停止扫描不需要的表(MySQL stops reading from t2 (for any particular row in t1) when it finds the first row in t2:

SELECT DISTINCT t1.a FROM t1, t2 where t1.a=t2.a;)。

5、update 操作中,如果字段值是相同的,则不会更新记录。

 

InnoDB 的好处:

1、如果服务器崩溃了,只需重启即可。

InnoDB 会保证崩溃时数据的一致性;可以在重启时恢复 buffer pool、change buffer 的状态,避免 warm-up。

2、支持 foreign key。

3、支持 buffer pool、change buffer(formerly named insert buffer)。

4、支持 checksum,可用于提醒数据损坏。

5、使用 clustered index 实现 primary key。

如果操作使用到了聚簇索引中的列时会很快(where,order by,group by,join)。

6、Adaptive hash index。

7、可以压缩表、索引,节省磁盘空间。

8、新建、删除索引对性能影响很小。

9、Truncating 一个 file-per-table tablespace 会很快,并且释放后的空间可以被用于存储新的数据。

不同于 system tablespace,system tablespace 中的空间被释放后只能被 system tablespace 使用。

 

posted @ 2015-08-09 11:11  赫尔修斯  阅读(130)  评论(0编辑  收藏  举报