电子商务网站建设背景,低价建网站,小说网站源码html,搜狗排名优化工具作为程序员经常和数据库打交道的时候还是非常频繁的#xff0c;掌握住一些Sql的优化技巧还是非常有必要的。下面列出一些常用的SQl优化技巧#xff0c;感兴趣的朋友可以了解一下。1、注意通配符中Like的使用以下写法会造成全表的扫描#xff0c;例如#xff1a;select id,n… 作为程序员经常和数据库打交道的时候还是非常频繁的掌握住一些Sql的优化技巧还是非常有必要的。下面列出一些常用的SQl优化技巧感兴趣的朋友可以了解一下。1、注意通配符中Like的使用以下写法会造成全表的扫描例如select id,name from userinfo where name like %name%或者select id,name from userinfo where name like %name下面的写法执行效率快很多因为它使用了索引select id,name from userinfo where name like name%2、避免在where子句中对字段进行函数操作比如select id from userinfo where substring(name,1,6) xiaomi或者select id from userinfo where datediff(day,datefield,2017-05-17) 0上面两句都对字段进行了函数处理会导致查询分析器放弃了索引的使用。正确的写法select id from userinfo where name likexiaomi%select id from userinfo where datefield 2017-05-17通俗理解就是where子句‘’ 左边不要出现函数、算数运算或者其他表达式运算3、在子查询当中尽量用exists代替inselect name from userinfo a where id in(select id from userinfo b)可以改为select name from userinfo a where exists(select 1 from userinfo b where id a.id)下面的查询速度比in查询的要快很多。4、where子句中尽量不要使用is null 或 is not null对字段进行判断例如select id from userinfo where name is null尽量在数据库字段中不出现null如果查询的时候条件为 is null 索引将不会被使用造成查询效率低因此数据库在设计的时候尽可能将某个字段可能为空的时候设置默认值那么查询的时候可以根据默认值进行查询比如name字段设置为0查询语句可以修改为select id from userinfo where name05、避免在where子句使用or作为链接条件例如select id from userinfo where namexiaoming or namexiaowang可以改写为select id from userinfo where name xiaoming union allselect id from userinfo where name xiaowang6、避免在 where 子句中使用 ! 或 操作符。例如select name from userinfo where id 0说明数据库在查询时对 ! 或 操作符不会使用索引而对于 、 、 、 、 、 BETWEEN AND数据库才会使用索引。因此对于上面的查询正确写法可以改为select name from userinfo where id 0 union allselect name from userinfo where id 07、少用in 或 not in对于连续的数值范围查询尽量使用BETWEEN AND例如select name from userinfo where id BETWEEN 10 AND 70以上只是相对来说比较常用的sql优化技巧当然还有很多欢迎补充欢迎关注公众号DoNet技术分享平台阅读原文