Mysql 查询优化

来源:互联网 发布:人工智能讲座 编辑:程序博客网 时间:2024/05/22 01:29

根据EXPLAIN来分析SQL语句的执行计划,对关键的条件加上索引。

ySQL操作手册中有一段话让我决定把小表放在JOIN的前面:“MySQL resolves all joins using asingle-sweep multi-join method. This means that MySQL reads a row from the first table, and then finds a matching row in the second table, the third table, and so on. When all tables are processed, MySQL outputs the selected columns and backtracks through the table list until a table is found for which there are more matching rows. The next row is read from this table and the process continues with the next table.”。再有的就是,能够不用UNIONGROUP BY的地方,尽量不用。


索引的建立原则:
  如果一列的中数据的前缀重复值很少,我们最好就只索引这个前缀。Mysql支持这种索引。我在上面用到的索引方法就是对username最左边的6个字符进行索引。索引越短,占用的 磁盘空间越少,在检索过程中花的时间也越少。这方法可以对最多左255个字符进行索引。

IN、OR子句常会使用工作表,使索引失效

对于like ‘许%’类型的查询,速度提升是最明显的

尽量把所有的列设置为NOT NULL,如果你要保存NULL,手动去设置它,而不是把它设为默认值。


.经常同时存取多列,且每列都含有重复值可考虑建立组合索引

原创粉丝点击