62,243
社区成员




select * from(
select count(0) from tabel1 where time1=@start and time2=@end as n1
,select count(0) from tabel2 where time1=@start and time2=@end as n2
,select count(0) from tabel3 where time1=@start and time2=@end as n3
)t
select * from(
select count(0) from tabel1 where time1='2015-01-24' and time2='2015-01-25' as n1
,select count(0) from tabel2 where time1='2015-01-24' and time2='2015-01-25' as n2
,select count(0) from tabel3 where time1='2015-01-24' and time2='2015-01-25' as n3
)t
declare @start datetime
declare @end datetime
set @start =convert(datetime,'2015-01-24')
set @end =convert(datetime,'2015-01-25')
select * from(
select count(0) from tabel1 where time1=@start and time2=@end as n1
,select count(0) from tabel2 where time1=@start and time2=@end as n2
,select count(0) from tabel3 where time1=@start and time2=@end as n3
)t
select * from(
select count(0) from tabel1 where convert(varchar(8),time1,112)=@start and time2=@end as n1
,select count(0) from tabel2 where convert(varchar(8),time1,112)=@start and time2=@end as n2
,select count(0) from tabel3 where convert(varchar(8),time1,112=@start and time2=@end as n3
)t
另外,看下执行计划,看慢在哪儿了
select * from(
select count(0) from tabel1 where time1='2015-01-24' and time2='2015-01-25' as n1
,select count(0) from tabel2 where time1='2015-01-24' and time2='2015-01-25' as n2
,select count(0) from tabel3 where time1='2015-01-24' and time2='2015-01-25' as n3
)t
select * from(
select count(0) from tabel1 where convert(varchar(8),time1,112)=@start and time2=@end as n1
,select count(0) from tabel2 where convert(varchar(8),time1,112)=@start and time2=@end as n2
,select count(0) from tabel3 where convert(varchar(8),time1,112=@start and time2=@end as n3
)t
另外,看下执行计划,看慢在哪儿了
select * from(
select count(0) from tabel1 where time1='2015-01-24' and time2='2015-01-25' as n1
,select count(0) from tabel2 where time1='2015-01-24' and time2='2015-01-25' as n2
,select count(0) from tabel3 where time1='2015-01-24' and time2='2015-01-25' as n3
)t
[/quote]
select * from(
(select count(0) from tabel1 where convert(varchar(8),time1,112)<=@start and time2>=@end) as n1
,(select count(0) from tabel2 where convert(varchar(8),time1,112)<=@start and time2>=@end) as n2
,(select count(0) from tabel3 where convert(varchar(8),time1,112<=@start and time2>=@end) as n3
)t
超时
select * from(
(select count(0) from tabel1 where time1<='2015-01-24' and time2>='2015-01-25') as n1
,(select count(0) from tabel2 where time1<='2015-01-24' and time2>='2015-01-25') as n2
,(select count(0) from tabel3 where time1<='2015-01-24' and time2>='2015-01-25') as n3
)t
这个也超时,直接查询分析器运行也超时
declare @start datetime
declare @end datetime
set @start =convert(datetime,@BeginTime)
set @end =convert(datetime,@EndTime)
select * from(
(select count(0) from tabel1 where time1<=@start and time2>=@end) as n1
,select count(0) from tabel2 where time1<=@start and time2>=@end as n2
,select count(0) from tabel3 where time1<=@start and time2>=@end as n3
)t
SqlParameter[] parameters = {
new SqlParameter("@BeginTime", SqlDbType.DateTime),
new SqlParameter("@EndTime", SqlDbType.DateTime)};
parameters[0].Value = time1.Value;
parameters[1].Value = time2.Value;
反而这样运行是0秒,把SqlParameter指到SQL的变量最快,其它拼接SQL,参数放到SQL里都是超时