mysql语句的优化(调优)

1、单条查询最后添加 LIMIT 1,停止全表扫描(否则,若无索引,将会查的太多)。


2、where子句中不要使用 != ,否则放弃索引全表扫描。


3、尽量避免 NULL 值判断,否则放弃索引全表扫描。
                优化前:select number from t1 where number is null;
                优化后:select number from t1 where number=0;                                                            

                # 在number列上设置默认值0,确保number列无NULL值


4、尽量避免 or 连接条件,否则放弃索引全表扫描。
                 优化前:select id from t1 where id=10 or id=20;
                 优化后: select id from t1 where id=10 union all  select id from t1 where id=20;


5、模糊查询尽量避免使用前置% ,否则全表扫描。
   select name from t1 where name like "c%";


6、尽量避免使用 in 和 not in, 否则全表扫描。
                优化前:select id from t1 where id in(1,2,3,4);
                优化后:select id from t1 where id between 1 and 4;


7、尽量避免使用 select * ...;   用具体字段代替 * ,不要返回用不到的任何字段

猜你喜欢

转载自blog.csdn.net/weixin_45037357/article/details/108299367