我有一個像下面這樣的SQL Server表值函數。表值函數性能
CREATE FUNCTION dbo.GetDeptName (@DId INT)
RETURNS TABLE
AS
RETURN
(SELECT DeptID ,
DeptName ,
Location
FROM dbo.department
WHERE Deptno = @DId
);
現在我想從Employee
表中使用此表值函數得到數據。哪一個能夠爲以下3種情況下的50k記錄提供更好的性能。
方案1:
SELECT *
FROM dbo.employee
WHERE deptname IN (SELECT deptname
FROM dbo.GetDeptName(50));
方案2:
SELECT *
FROM dbo.employee e
JOIN dbo.GetDeptName (50) fn ON e.deptname = fn.deptname;
方案3:
SELECT *
FROM dbo.employee e
WHERE EXISTS (SELECT 1
FROM dbo.GetDeptName (50) fn
WHERE e.deptname = fn.deptname);
只要運行它,看看自己。並且你的函數沒有條款 –
你的查詢是否工作 – TheGameiswar
是的這些工作... – Ram