sql优化

文章详细阐述了SQL优化的步骤,包括使用showstatus、EXPLAIN和showprofile分析,以及避免全表扫描、使用索引、优化WHERE子句的建议。还介绍了如何创建和管理数据库索引,以提升查询性能。
摘要由CSDN通过智能技术生成

sql优化

sql 优化步骤一般是:
  • 通过 show status 命令了解各种 SQL 的执行频率

  • 定位执行效率较低的 SQL 语句

  • 通过 EXPLAIN 分析较低 SQL 的执行计划

  • 通过 show profile 分析 SQL

  • 通过 trace 分析优化器如何选择执行计划

  • 确定问题并采取相应的优化措施

sql具体优化措施举例
  1. 对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引。

  1. 应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描。

  1. 应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:

select id from t where num is null

可以在num列设置默认值0,然后通过=等号查询:

select id from t where num=0
  1. 应尽量避免在 where 子句中使用 or 来连接条件,否则将导致引擎放弃使用索引而进行全表扫描,如:

select id from t where num=10 or num=20

此查询可优化为:

select id from t where num=10 union all select id from t where num=20

UNION:会自动压缩多个结果集合中的重复结果,同时进行默认规则的排序;

UNION All:对两个结果集进行并集操作,包括重复行,不进行排序;

  • UNION 和 UNION ALL 内部的 SELECT 语句必须拥有相同数量的列

  • 每条 SELECT 语句中列的顺序必须相同

  1. 下面的查询也将导致全表扫描:

select id from t where name like '%abc%'

若要提高效率,可以考虑全文检索。

  1. in 和 not in 也要慎用,否则会导致全表扫描,如:

select id from t where num in(1,2,3)

若查询的数值为连续值,则可以优化为between来查询:

select id from t where num between 1 and 3
  1. 如果在 where 子句中使用参数,也会导致全表扫描。因为SQL只有在运行时才会解析局部变量,但优化程序不能将访问计划的选择推迟到运行时;它必须在编译时进行选择。然而,如果在编译时建立访问计划,变量的值还是未知的,因而无法作为索引选择的输入项。如下面语句将进行全表扫描:

select id from t where num=@num

可以改为强制查询使用索引:

select id from t with(index(索引名)) where num=@num
  1. 应尽量避免在 where 子句中对字段进行表达式操作,这将导致引擎放弃使用索引而进行全表扫描。如:

select id from t where num/2=100

应改为:

select id from t where num=100*2
  1. 应尽量避免在where子句中对字段进行函数操作,这将导致引擎放弃使用索引而进行全表扫描。如:

select id from t where substring(name,1,3)='abc'--name以abc开头的id
select id from t where datediff(day,createdate,'2005-11-30')=0--'2005-11-30'生成的id

应改为:

select id from t where name like 'abc%'
select id from t where createdate>='2005-11-30' and createdate<'2005-12-1'

DATEDIFF(datepart,startdate,enddate)------SQL server

DATEDIFF(startdate*,*enddate)----------------Mysql

  1. 不要在 where 子句中的“=”左边进行函数、算术运算或其他表达式运算,否则系统将可能无法正确使用索引。

  1. 在使用索引字段作为条件时,如果该索引是复合索引,那么必须使用到该索引中的第一个字段作为条件时才能保证系统 使用该索引,否则该索引将不会被使用,并且应尽可能的让字段顺序与索引顺序相一致。

  1. 不要写一些没有意义的查询,如需要生成一个空表结构:

select col1,col2 into #t from t where 1=0

这类代码不会返回任何结果集,但是会消耗系统资源的,应改成这样:

 create table #t(...)
  1. 很多时候用 exists 代替 in 是一个好的选择:

select num from a where num in(select num from b)

用下面的语句替换:

select num from a where exists(select 1 from b where num=a.num)

exists函数不会返回数据,只会返回true或false

exists先进行外圈循环,及前面的sql语句的执行,然后带入后面的语句执行

in先进行内圈循环,然后进行外圈循环

  1. 并不是所有索引对查询都有效,SQL是根据表中数据来进行查询优化的,当索引列有大量数据重复时,SQL查询可能不会去利用索引,如一表中有字段sex,male、female几乎各一半,那么即使在sex上建了索引也对查询效率起不了作用。

  1. 索引并不是越多越好,索引固然可以提高相应的 select 的效率,但同时也降低了 insert 及 update 的效率,因为 insert 或 update 时有可能会重建索引,所以怎样建索引需要慎重考虑,视具体情况而定。一个表的索引数最好不要超过6个,若太多则应考虑一些不常使用到的列上建的索引是否有必要。

  1. 应尽可能的避免更新 clustered(聚簇) 索引数据列,因为 clustered 索引数据列的顺序就是表记录的物理存储顺序,一旦该列值改变将导致整个表记录的顺序的调整,会耗费相当大的资源。若应用系统需要频繁更新 clustered 索引数据列,那么需要考虑是否应将该索引建为 clustered 索引。

  1. 尽可能的使用char/nchar 代替 varchar/nvarchar ,因为首先变长字段存储空间小,可以节省存储空间,其次对于查询来说,在一个相对较小的字段内搜索效率显然要高些。

  1. 任何地方都不要使用 select * from t ,用具体的字段列表代替“*”,不要返回用不到的任何字段。

  1. 尽量使用表变量来代替临时表。如果表变量包含大量数据,请注意索引非常有限(只有主键索引)。

  1. 避免频繁创建和删除临时表,以减少系统表资源的消耗。

创建索引

1. 新建表中添加索引

添加普通索引

create table t(id int primary key auto_increment,name char(5),index index_id(id));

添加唯一索引

create table t(id int primary key auto_increment,name char(5),unique index index_id(id));

添加全文索引

create table t(id int primary key auto_increment,name char(5),fulltext index index_id(id));

添加多列索引

create table t(id int primary key auto_increment,name char(5),key index index_id_name(id,name));

2. 在已建表中添加索引

添加普通索引

create index index_name on person(name);

添加唯一索引

create unique index index_name on person(name);

添加全文索引

create fulltext index index_name on person(name);

添加多列索引

create index index_name_age on person(name,age);

3. 以修改表的方式添加索引

添加普通索引

alter table person add index index_age(age);

添加唯一索引

alter table person add unique index index_age(age);

添加全文索引

alter table person add fulltext index index_age(age);

添加多列索引

alter table person add index index_age_name(age,name);

查看索引

show index from person;

删除索引

drop index index_name on person;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值