我正在使用 SQL Server 2008 并尝试运行:
WITH results (Row, code, p_name, phone, intake_date, shipped_status, shipped_date,
event_status, intake_status, slsperson, referral_source, dr )
AS
(
SELECT ROW_NUMBER() OVER (ORDER BY intake_date ASC)AS Row, code, p_name, phone,
intake_date, shipped_status, shipped_date, event_status, intake_status, slsperson,
referral_source, dr
FROM db.schema.sales_referral_list('2012-05-1', '2012-06-1', 'CXJ7')
)
SELECT Row, code as p_id, p_name, phone, intake_status, intake_date, shipped_status,
shipped_date, event_status, slsperson, referral_source, dr
FROM results
WHERE Row BETWEEN 0 AND 50 ORDER BY Row
当我在没有 With AS 语句的情况下运行它(即只是内部选择语句)时,它会在大约 1 秒内执行。当我使用用于在网站上进行分页的 With 语句时,执行时间超过 15 秒。我可以做一些优化来提高这个语句的性能吗?(sales_referral_list是一个表值函数,需要3个参数。它一直在1秒内运行,所以我比较确定不是问题)。谢谢你提供的所有帮助。
==========更新=========
表值函数如下: 表值函数如下:
(
-- Add the parameters for the function here
@START DATE,
@END DATE,
@SLSCODE VARCHAR(4)
)
RETURNS TABLE
AS
RETURN
(
-- Add the SELECT statement with parameter references here
SELECT
A.code,
B.last+', '+B.first AS p_name,
B.phoneday AS Phone,
B.slcode,
B.regdate as intake_date,
CASE
WHEN CAST(C.newdate AS DATE) is not null THEN 'SHIPPED'
WHEN B.udef1='03' THEN 'NON-SERV'
ELSE 'NOT SHIPPED'
END as shipped_status,
CASE
WHEN CAST(C.newdate AS DATE)='1900-01-01' THEN ''
ELSE CAST(C.newdate AS DATE)
END as shipped_date,
CASE
WHEN E.status='1' THEN 'ACTIVE'
WHEN E.status='2' THEN 'COMPLETE'
WHEN E.status='0' THEN 'DELETED'
ELSE 'NO EVENT'
END event_status,
F.file_status as intake_status,
D.employee as slsperson,
B.rfname as referral_source,
B.dcname as doctor
FROM event.dbo.distinct_account() a
LEFT OUTER JOIN event.dbo.patient_dg() B ON A.code=B.code
LEFT OUTER JOIN event.dbo.newdate() c on a.code=c.ACCOUNT
LEFT OUTER JOIN event.dbo.employee D ON B.slname=D.employee
LEFT OUTER JOIN(
SELECT
id,
patient_id,
status
FROM event.dbo.event A
WHERE A.task_id IN ('WF','WT')
group by id,patient_id,status
) E ON A.code=E.patient_id
LEFT OUTER JOIN event.dbo.taskWF F ON E.id=F.event_id
WHERE b.regdate>=@START
AND b.regdate<=@END
AND slcode=@SLSCODE
)
正常返回的结果介于 100 - 500 条记录之间。表值函数 patient_dg 大约有 60,000 条记录