【IT168 技术】SQLSERVER的表值函数是SQLSERVER 2005以来的新特性,由于它使用比较方便,就像一个单独的表一样,在我们的系统中大量使用。有一个获取客户数据的SQLSERVER 表值函数,如果使用管理员登录,这个函数会返回150W行记录,大概需要30秒左右,但如果将TOP语句放到表值函数外,效率异常低下,需要约3分钟:
select top 20 * from GetFrame_CustomerSerch('admin','1')
下面是该存储过程的定义:
ALTER FUNCTION [dbo].[GetFrame_CustomerSerch]
(
-- Add the parameters for the function here
@WorkNo varchar(38)
,@SerchChar varchar(500)
)
RETURNS TABLE
AS
RETURN
(
-- Add the SELECT statement with parameter references here
select a.GUID,a.CustomerName,a.CustomerIDcard,a.CustomerPhone,a.CustomerMobile from
(
--具体子查询略
)
) a union all
select b.GUID,b.CustomerName,b.CustomerIDcard,b.CustomerPhone,b.CustomerMobile from WFT_ManagerCollectUsers a left join WFT_Customer b on a.FundAccount=b.FundAccount
--where a.WorkNo=@WorkNo
WHERE a.WorkNo IN
(
--具体子查询略
)
)
(
-- Add the parameters for the function here
@WorkNo varchar(38)
,@SerchChar varchar(500)
)
RETURNS TABLE
AS
RETURN
(
-- Add the SELECT statement with parameter references here
select a.GUID,a.CustomerName,a.CustomerIDcard,a.CustomerPhone,a.CustomerMobile from
(
--具体子查询略
)
) a union all
select b.GUID,b.CustomerName,b.CustomerIDcard,b.CustomerPhone,b.CustomerMobile from WFT_ManagerCollectUsers a left join WFT_Customer b on a.FundAccount=b.FundAccount
--where a.WorkNo=@WorkNo
WHERE a.WorkNo IN
(
--具体子查询略
)
)
这个语句放在PDF.NET数据开发框架的SQL-MAP文件中,开始还以为是框架引起的,将这个语句直接在查询分析器中查询,仍然很慢。