C#,Ado分页查询的错误:
Timeout 时间已到。在操作完成之前超时时间已过或服务器未响应。
情景是这样,一个生产环境,一个测试环境,我把测试环境的数据库指向生产环境的数据库,连接字符串完全一样。生产环境的一个分页存储过程A,调用的时候报出超时错误,所有我就新建了一个完全相同分页存储过程A2(仅仅是名字不一样)给测试环境调用(注意只想的是生产数据库),A和A2不同的是,A里面我在统计1W+的数据合计(sum求和)的地方加了top 0,这样明显的理论上A比A2少花时间。
然后发现调用A的生产环境报出超时错误,调用A2的测试环境正常,我再将测试环境改为调用A,结果报出超时。我就把生产的改成调用A2,生产环境正常了,我再把测试环境的改回A2,也正常了,再多走一步,我再把测试环境改成A,超时出现了。
最后我特地比较了一下,除了A里多出来的top 0,其他的代码完全一样,这是怎么回事???
简单说就是根据一堆条件对某表筛选,对筛选后的数据执行两个查询:1是分页,2是对某个字段分组求和,现在筛选后的数据最多只有1W+
create proc [dbo].[pager_buyApply2]
(
@p_ouCode nvarchar(20) = null,
@p_applyNo varchar(20) = null,
@p_customerManagerName nvarchar(100) = null,
@p_customerName nvarchar(200) = null,
@p_oildepotCode varchar(20) = null,
@p_oilCode varchar(20) = null,
@p_approveStatus int = null,
@p_isTakeOver int = null,
@p_acceptUnit nvarchar(100) = null,
@p_startTime datetime = null,
@p_endTime datetime = null,
@p_recommender nvarchar(50) = null,
@p_canApplySearchInCompany int = 1,
@p_currentUserId varchar(100) = null,--
@p_noPage int = 0,
@pageSize int = 10,
@pageIndex int = 1,
@totalRowCount int out
)
as
begin
declare @isOildepot int;
select top 1 @isOildepot = iif(IsUnit = 4, 1, 0) from sys_orgunit where oucode=@p_ouCode
select request.* into #tmp_request from WF_Request request
where request.WorkflowCode in ('Buy', 'OneSettle', 'Promotion');
select apply.* into #tmp_VBuyAndDeliveryApply from VBuyAndDeliveryApply apply
where
(
(OildepotCode=@p_ouCode and @isOildepot = 1)
or (@isOildepot = 0 and ManagerUserId in
(
select UserId from SYS_OrgUser
where
(OUCode like dbo.func_GetOrgCodeByDepartment(@p_ouCode) + '%' and @p_canApplySearchInCompany = 1)
or (UserID = @p_currentUserId and @p_canApplySearchInCompany != 1)
)
)
)
and (@p_applyNo is null or len(@p_applyNo) = 0 or ApplyNo like '%' + @p_applyNo + '%')
and (@p_customerManagerName is null or len(@p_customerManagerName) = 0 or ManagerName like '%' + @p_customerManagerName + '%')
and (@p_customerName is null or len(@p_customerName) =0 or CustomerName like '%' + @p_customerName + '%')
and (@p_oildepotCode is null or len(@p_oildepotCode) = 0 or OildepotCode = @p_oildepotCode)
and (@p_oilCode is null or len(@p_oilCode) = 0 or OilCode = @p_oilCode)
and (@p_acceptUnit is null or len(@p_acceptUnit) = 0 or AcceptUnit like '%' + @p_acceptUnit + '%')
and (@p_isTakeOver is null or isnull(IsTakeOver, 0) = @p_isTakeOver)
and (@p_recommender is null or len(@p_recommender) = 0 or Recommender like '%' + @p_recommender + '%')
and (@p_startTime is null or CreatedDate >= @p_startTime)
and (@p_endTime is null or CreatedDate <= @p_endTime)
select apply.* into #tmp_fullPageData from #tmp_VBuyAndDeliveryApply apply
left join #tmp_request request on apply.ID = request.RequestID
where (@p_approveStatus is null or request.RequestStatus=@p_approveStatus);
select @totalRowCount = count(1) from #tmp_fullPageData;
if @p_noPage = 1
begin
select * from #tmp_fullPageData;
end
else
begin
declare @pageEnd int = @pageIndex * @pageSize;
select * from (select ROW_NUMBER() over (order by CreatedDate desc) RowNum, * from #tmp_fullPageData) t
where RowNum > (@pageEnd - @pageSize) and RowNum <= @pageEnd;
end
select OilName, SUM(OrderQty) 'Qty' from #tmp_fullPageData group by OilName;
drop table #tmp_request;
drop table #tmp_VBuyAndDeliveryApply;
drop table #tmp_fullPageData;
end
开下sql profiler追一下就看到了啊。
要看什么内容?
@北在北方: rpc completed,sql:stmtcompleted
主要集中于read和cpu,看看高不高。
如果高的话就需要优化了,直接用查询分析器对语句进行分析,看看具体是哪里消耗大,然后再做具体的优化
如果不高就比较麻烦了,我不是dba,现在能想到的可能为锁等待
把语句贴出来,查询计划贴出来。你说这么多没用的。没数据看。怎么解决问题