通过CTE连接的链接服务器表的性能调优

时间:2018-12-04 06:18:35

标签: sql-server sql-server-2008-r2

想要通过执行SELECT操作来减少OPENQUERYJOIN的数量来调整性能。

这是我的查询的样子。

查询:

;WITH CTE AS
(
    SELECT 
        [EmployeeFirstName], [EmployeeMiddleName],
        [EmployeeLastName], [EmployeeAddress]
    FROM 
        Employee
    WHERE 
        [EmployeeSalary] >= 200000 
)
SELECT 
    [First Name], [Fathers Husband Name], [Last Name], [Department Name]
FROM 
    OPENQUERY ([Depart], 'SELECT [First Name], [Fathers Name], [Last Name],[Department Name] 
                          FROM Department
                          INNER JOIN CTE C1 ON PATINDEX(''[First Name]'',''C1.[EmployeeFirstName]'') > 0'); 

错误:

  

无效的对象名称“ CTE”

1 个答案:

答案 0 :(得分:1)

尝试以下

;WITH CTE AS
(
    SELECT 
        [EmployeeFirstName], [EmployeeMiddleName],
        [EmployeeLastName], [EmployeeAddress]
    FROM 
        LocalServer.Database.Schema.Employee
    WHERE 
        [EmployeeSalary] >= 200000 
)
SELECT 
    [First Name], [Fathers Husband Name], [Last Name], [Department Name]
    --It's better to specify the table eg: C1.ColumnName
FROM 
    (
      SELECT [First Name], [Fathers Name], [Last Name],[Department Name] 
      FROM LinkedServer.Database.Schema.Department
      INNER JOIN CTE C1 
      ON PATINDEX([First Name],C1.[EmployeeFirstName]) > 0
    ) TBL;