读书人

SQL查询速度很慢要如何才能优化或

发布时间: 2012-02-20 21:18:23 作者: rapoo

SQL查询速度很慢,要怎么才能优化,或者有更好的方法达到查询效果
declare @tyear as int,@tmonth as int
set @tyear=2011
set @tmonth=10
select
distinct convert(varchar(10), c.checktime, 120) as mydate,
u.userid,u.name,
(select
convert(varchar(5), min(checktime), 108)
from
checkinout
where
convert(varchar(10), checktime, 120)=convert(varchar(10), c.checktime, 120)
and
userid=u.userid) as mintime,
(select
convert(varchar(5), max(checktime), 108)
from
checkinout where convert(varchar(10), checktime, 120)=convert(varchar(10), c.checktime, 120)
and userid=u.userid) as maxtime
from
checkinout as c,userinfo as u
where
c.checktime between '2011-11-01' and '2011-11-30'

order by
mydate


表的关系:
userinfo:
NAME USERID
刘一1
周二2
李三3
徐四4
王五5
邹六6
黄七7
林八8
易九9
蔺十10


checkinout:
CHECKTIME USERID
2011-11-1 8:25 1
2011-11-1 17:331
2011-11-1 8:312
2011-11-1 17:422
2011-11-1 8:264
2011-11-1 19:504
2011-11-1 8:255
2011-11-1 17:325
2011-11-1 8:267
2011-11-1 17:327

2011-11-2 13:573
2011-11-2 17:483
2011-11-2 8:264
2011-11-2 18:514
2011-11-2 8:265
2011-11-2 17:305
2011-11-2 8:276
2011-11-2 18:346
2011-11-2 8:267
2011-11-2 19:497
2011-11-2 8:288


2011-11-3 8:273
2011-11-3 18:473
2011-11-3 8:104
2011-11-3 17:364
2011-11-3 8:287
2011-11-3 17:387
2011-11-3 8:248
2011-11-3 17:318


查询结果如下:
MYDATE NAME MINTIME MAXTIME
2011-11-1刘一8:2517:33
2011-11-1周二8:3117:42
2011-11-1李三NULLNULL
2011-11-1徐四8:2619:50
2011-11-1王五8:2517:32
2011-11-1邹六NULLNULL
2011-11-1黄七8:2617:32
2011-11-1林八NULLNULL
2011-11-1易九NULLNULL
2011-11-1蔺十NULLNULL

2011-11-2刘一NULLNULL
2011-11-2周二NULLNULL
2011-11-2李三13:5717:48
2011-11-2徐四8:2618:51
2011-11-2王五8:2617:30
2011-11-2邹六8:2718:34
2011-11-2黄七8:2619:49
2011-11-2林八8:288:28
2011-11-2易九NULLNULL
2011-11-2蔺十NULLNULL

2011-11-3刘一NULLNULL
2011-11-3周二NULLNULL
2011-11-3李三8:2718:47
2011-11-3徐四8:1017:36
2011-11-3王五NULLNULL
2011-11-3邹六NULLNULL
2011-11-3黄七8:2817:38
2011-11-3林八8:2417:31
2011-11-3易九NULLNULL
2011-11-3蔺十NULLNULL


[解决办法]

SQL code
declare @tyear as int,@tmonth as intset @tyear=2011set @tmonth=10select mydate, u.userid,u.name,  mintime, maxtime fromuserinfo as u ,    (    select         userid,         convert(varchar(10), c.checktime, 120) as mydate,        convert(varchar(5), min(checktime), 108)   mintime,        convert(varchar(5), max(checktime), 108)  maxtime      from          checkinout     where          checktime between '2011-11-01' and '2011-11-30'    GROUP BY userid    )Twhere T.userid=u.useridorder by  mydate
[解决办法]
从跟底下优化吧。
[解决办法]
探讨
SQL code

declare @tyear as int,@tmonth as int
set @tyear=2011
set @tmonth=10
select
mydate,
u.userid,u.name,
mintime,
maxtime
from
userinfo as u ,
(
select
userid……

------解决方案--------------------


探讨
引用:
SQL code
declare @tyear as int,@tmonth as int
set @tyear=2011
set @tmonth=10
select
mydate,
u.userid,u.name,
mintime,
maxtime
from
userinfo as u ,
(
select
userid, ……
……

[解决办法]
[code=SQL][/code]
declare @tyear as int,@tmonth as int
set @tyear=2011
set @tmonth=10
select
mydate,
u.userid,u.name,
mintime,
maxtime
from
userinfo as u
left outer join
(
select
userid,
convert(varchar(10), c.checktime, 120) as mydate,
convert(varchar(5), min(checktime), 108) mintime,
convert(varchar(5), max(checktime), 108) maxtime
from
checkinout
where
checktime between '2011-11-01' and '2011-11-30'
GROUP BY userid
)T
on T.userid=u.userid

order by mydate

[解决办法]
关于优化 你可以吧 操作的顺序 修改一下 执行convert 操作 是为了显示出你想要的结果
但是如果你为了要将显示的结果 转换成喜欢的格式 就对全表的数据执行该操作的话 自然会慢了 所以
将 处理的次序调整一下 先不换 显示的样式 是否满意 先通过查询语句将 想要的那部分记录集 查询出来 比如说 2011-10-1至2011-10-31日之间的数据 这个数据集是一个子集 然后在对它执行转换格式的操作 我想一定会飞一般的快的
[解决办法]
checkinout表left join表就行了。
[解决办法]
SQL code
select a.dt,a.name,max(convert(varchar(5),b.checktime,108))maxtime,min(convert(varchar(5),b.checktime,108))mintimefrom (select distinct convert(varchar(10),a.checktime,120)dt,b.userid,b.name from checkinout a,userinfo b)a left join checkinout b on a.dt=convert(varchar(10),b.checktime,120) and a.userid=b.useridgroup by a.dt,a.nameorder by 1/*dt         name       maxtime mintime---------- ---------- ------- -------2011-11-01 黄七         17:32   08:262011-11-01 李三         NULL    NULL2011-11-01 林八         NULL    NULL2011-11-01 蔺十         NULL    NULL2011-11-01 刘一         17:33   08:252011-11-01 王五         17:32   08:252011-11-01 徐四         19:50   08:262011-11-01 易九         NULL    NULL2011-11-01 周二         17:42   08:312011-11-01 邹六         NULL    NULL2011-11-02 黄七         19:49   08:262011-11-02 李三         17:48   13:572011-11-02 林八         08:28   08:282011-11-02 蔺十         NULL    NULL2011-11-02 刘一         NULL    NULL2011-11-02 王五         17:30   08:262011-11-02 徐四         18:51   08:262011-11-02 易九         NULL    NULL2011-11-02 周二         NULL    NULL2011-11-02 邹六         18:34   08:272011-11-03 黄七         17:38   08:282011-11-03 李三         18:47   08:272011-11-03 林八         17:31   08:242011-11-03 蔺十         NULL    NULL2011-11-03 刘一         NULL    NULL2011-11-03 王五         NULL    NULL2011-11-03 徐四         17:36   08:102011-11-03 易九         NULL    NULL2011-11-03 周二         NULL    NULL2011-11-03 邹六         NULL    NULL警告: 聚合或其他 SET 操作消除了空值。(30 行受影响)*/ 

读书人网 >SQL Server

热点推荐