Mysql 查询优化

根据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,手动去设置它,而不是把它设为默认值。


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值