美文网首页
Mysql常用查询语句sql优化

Mysql常用查询语句sql优化

作者: Chaweys | 来源:发表于2021-07-06 15:33 被阅读0次

    Mysql常用查询语句sql优化:
    1、应尽量避免在 where 子句中使用 != 或 <> 操作符,否则将引擎放弃使用索引而进行全表扫描。
    
    2、应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描。
    select id from tableName where num is null; 
    改进:可以为num列设置默认值0,使用默认值0过滤
    select id from tableName where num=0;
    
    3、尽量避免在 where 子句中使用 or 来连接条件,否则将导致引擎放弃使用索引而进行全表扫描。
    select id from tableName where num=10 or num=12;
    改进:
    select id from tableName where num=10
    union all
    select id from tableName where num=12;
    
    4、前置百分号也将导致全表扫描:
    select id from tableName where num like %c%;
    改进:
    select id from tableName where num like c%;
    
    5、in和not in 也会导致全表扫描;
    select id from tablenName where id in (1,2,3);
    改进:对于连续的值尽量使用between
    select id from tableName where id between 1 and 3;
    
    6、在where子句中使用变量也会导致全表扫描。
    select id from tableName where num=@num;
    改进:强制增加使用索引
    select id from tableName with(index(索引名)) where num=@num;
    
    7、应尽量避免在 where 子句中对字段进行表达式操作,这将导致引擎放弃使用索引而进行全表扫描。
    select id from tableName where num/2=10;
    改进:
    select id from tableName where num=10*2;
    
    8、应尽量避免在where子句中对字段进行函数操作,这将导致引擎放弃使用索引而进行全表扫描。
    select id from tableName where substring(num,1,3)='abc'; #取num以abc开头的值
    改进:
    select id from tableName where num like 'abc%';
    
    9、不要在 where 子句中的 "=" 左边进行函数、算术运算或其他表达式运算,否则系统将可能无法正确使用索引。
    
    10、在使用索引字段作为条件时,如果该索引是复合索引,那么必须使用到该索引中的第一个字段作为条件时才能保证系统使用该索引,
    否则该索引将不会被使用,并且应尽可能的让字段顺序与索引顺序相一致。
    
    11、不要写一些没有意义的查询,如需要生成一个空表结构:这类代码不会返回任何结果集,但是会消耗系统资源。
    select col1,col2 into #t from t where 1=0;
    改进:
    create table #t(…)
    
    12、尽量使用exists代替in
    select id from tableName where num in (select num from tableName2);
    改进:
    select id from a tableName where exists (select 1 from tableName2 where num=a.num);
    
    13、并不是所有索引对查询都有效,SQL是根据表中数据来进行查询优化的,当索引列有大量数据重复时,
    SQL查询可能不会去利用索引,如一表中有字段 sex,male、female几乎各一半,那么即使在sex上建了索引也对查询效率起不了作用。
    
    14、索引并不是越多越好,索引固然可以提高相应的 select 的效率,但同时也降低了 insert 及 update 的效率,
    因为 insert 或 update 时有可能会重建索引,所以怎样建索引需要慎重考虑,视具体情况而定。
    一个表的索引数最好不要超过6个,若太多则应考虑一些不常使用到的列上建的索引是否有必要。
    
    15、尽量使用数字型字段,若只含数值信息的字段尽量不要设计为字符型,这会降低查询和连接的性能,并会增加存储开销。
    这是因为引擎在处理查询和连接时会 逐个比较字符串中每一个字符,而对于数字型而言只需要比较一次就够了。
    
    16、尽可能的使用 varchar/nvarchar 代替 char/nchar ,因为首先变长字段存储空间小,可以节省存储空间,
    其次对于查询来说,在一个相对较小的字段内搜索效率显然要高些。
    
    17、任何地方都不要使用 select * from t ,用具体的字段列表代替"*",不要返回用不到的任何字段。
    
    18、尽量使用表变量来代替临时表。如果表变量包含大量数据,请注意索引非常有限(只有主键索引)。
    
    19、避免频繁创建和删除临时表,以减少系统表资源的消耗。
    
    20、在新建临时表时,如果一次性插入数据量很大,那么可以使用 select into 代替 create table,避免造成大量 log ,以提高速度。
    如果数据量不大,为了缓和系统表的资源,应先create table,然后insert。
    
    21、如果使用到了临时表,在存储过程的最后务必将所有的临时表显式删除,
    先 truncate table ,然后 drop table ,这样可以避免系统表的较长时间锁定。
    
    22、尽量避免使用游标,因为游标的效率较差,如果游标操作的数据超过1万行,那么就应该考虑改写。
    
    23、在所有的存储过程和触发器的开始处设置 set nocount on ,在结束时设置 set nocount off 。
    无需在执行存储过程和触发器的每个语句后向客户端发送 DONEINPROC 消息。
    

    相关文章

      网友评论

          本文标题:Mysql常用查询语句sql优化

          本文链接:https://www.haomeiwen.com/subject/otugultx.html