欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

sql语句优化及后一条减前一条

程序员文章站 2022-06-11 15:39:16
...

sql语句优化: 1。表加索引 2。少用like,直接用=所有值 3. where语句把能大量筛查的条件写在前面 4.数据量大时,参与计算的值相同时只取一条 后一条减前一条, select houec,[houdate],houtimes ,tw=ISNULL(convert(decimal(18,2),houtw-(select top 1 qiant

sql语句优化:

1。表加索引

2。少用like,直接用=所有值

3.where语句把能大量筛查的条件写在前面

4.数据量大时,参与计算的值相同时只取一条

后一条减前一条,,

select houec,[houdate],houtimes
,tw=ISNULL(convert(decimal(18,2),houtw-(select top 1 qiantw from #qian where houec=qianec and houcid>qiancid order by qiancid desc)),0)
,mins=isnull(DATEDIFF (minute ,(select top 1 qiandatetimes from #qian where houec=qianec and houcid>qiancid order by qiancid desc),houdatetimes),0)
from #hou

其中:

1 临时表#hou与#qian为同一个表中的数据,为方便计算写入两个临时表;

2 ec为设备编号,id为表的ID号,相同的设备中id号唯一,houec=qianec and houcid>qiancid 限定了相同设备中相临ID号的记录相减。

select top 1 qiantw from #qian where houec=qianec and houcid>qiancid order by qiancid desc

为查询houec=qianec 而且houcid>qiancid相同设备中相临ID号的记录相减

日报

drop table #qian

CREATE TABLE #qian(qiancid nvarchar(20),qianec nvarchar(20) null, qiandatetimes smalldatetime null, qiandate datetime null,
qiantimes varchar(20) null,qiantw float null,
CONSTRAINT [PK_Electricqian] PRIMARY KEY CLUSTERED
(
[qiancid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

INSERT INTO #qian
select min(cid) as qiancid,cequipmentcode as qianec,
min(dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)))as qiandatetimes ,
convert(varchar(10),dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)),120) as qiandate,
str(datepart(hh,dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)))) + ':00' as qiantimes,
cast(ctotalyield as float) as qiantw
from InverterData_201310
where convert(varchar(10),dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)),120) = '2013-11-12' and ctotalyield'na' and ctotalyield'' and ctotalyield is not null and ctotalyield'0' and ctotalyield'#INV#' and ctotalyield'nan'
group by cequipmentcode,convert(varchar(10),dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)),120),datepart(hh,dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120))),ctotalyield
UNION select min(cid) as qiancid,cequipmentcode as qianec,
min(dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)))as qiandatetimes ,
convert(varchar(10),dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)),120) as qiandate,
str(datepart(hh,dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)))) + ':00' as qiantimes,
cast(ctotalyield as float) as qiantw
from InverterData_201311
where convert(varchar(10),dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)),120) = '2013-11-12' and ctotalyield'na' and ctotalyield'' and ctotalyield is not null and ctotalyield'0' and ctotalyield'#INV#' and ctotalyield'nan'
group by cequipmentcode,convert(varchar(10),dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)),120),datepart(hh,dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120))),ctotalyield
UNION select min(cid) as qiancid,cequipmentcode as qianec,
min(dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)))as qiandatetimes ,
convert(varchar(10),dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)),120) as qiandate,
str(datepart(hh,dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)))) + ':00' as qiantimes,
cast(ctotalyield as float) as qiantw
from InverterData_201312
where convert(varchar(10),dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)),120) = '2013-11-12' and ctotalyield'na' and ctotalyield'' and ctotalyield is not null and ctotalyield'0' and ctotalyield'#INV#' and ctotalyield'nan'
group by cequipmentcode,convert(varchar(10),dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)),120),datepart(hh,dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120))),ctotalyield

drop table #hou

CREATE TABLE #hou(houcid nvarchar(20),houec nvarchar(20) null,houdatetimes smalldatetime null, houdate smalldatetime null,houtimes varchar(20) null,houtw float null,
CONSTRAINT [PK_Electrichou] PRIMARY KEY CLUSTERED
(
[houcid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

INSERT INTO #hou
select min(cid) as houcid,cequipmentcode as houec,min(dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)))as houdatetimes ,convert(varchar(10),dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)),120) as houdate,
str(datepart(hh,dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)))) + ':00' as houtimes,
cast(ctotalyield as float) as houtw
from InverterData_201310
where convert(varchar(10),dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)),120)='2013-11-12' and ctotalyield'na' and ctotalyield'' and ctotalyield is not null and ctotalyield'0' and ctotalyield'#INV#' and ctotalyield'nan'
group by cequipmentcode,convert(varchar(10),dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)),120),datepart(hh,dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120))),ctotalyield
UNION
select min(cid) as houcid,cequipmentcode as houec,min(dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)))as houdatetimes ,convert(varchar(10),dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)),120) as houdate,
str(datepart(hh,dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)))) + ':00' as houtimes,
cast(ctotalyield as float) as houtw
from InverterData_201311
where convert(varchar(10),dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)),120)='2013-11-12' and ctotalyield'na' and ctotalyield'' and ctotalyield is not null and ctotalyield'0' and ctotalyield'#INV#' and ctotalyield'nan'
group by cequipmentcode,convert(varchar(10),dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)),120),datepart(hh,dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120))),ctotalyield
UNION
select min(cid) as houcid,cequipmentcode as houec,min(dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)))as houdatetimes ,convert(varchar(10),dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)),120) as houdate,
str(datepart(hh,dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)))) + ':00' as houtimes,
cast(ctotalyield as float) as houtw
from InverterData_201312
where convert(varchar(10),dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)),120)='2013-11-12' and ctotalyield'na' and ctotalyield'' and ctotalyield is not null and ctotalyield'0' and ctotalyield'#INV#' and ctotalyield'nan'
group by cequipmentcode,convert(varchar(10),dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120)),120),datepart(hh,dateadd(hh,0,convert(SMALLDATETIME,(cdate+' '+ctime),120))),ctotalyield