T-SQL查询性能改进

时间:2015-03-23 20:50:07

标签: sql performance linq sql-server-2008 tsql

此查询大约需要30秒才能编译。此外,因为这是用LINQ写的,所以加载时间更长。

我打算将其转移到存储过程中。请指导我可以关注的领域,并提高查询的性能。

我有一个查询......有点像这样。

SELECT Table2.[Column1] AS [Column1]
    ,Table2.[Column2] AS [Column2]
    ,[Table7].[Column3] AS [Column3]
    ,Table2.[Column4] AS [Column4]
    ,......around 15 colums more selected
FROM (
    SELECT Table1.[Column1] AS [Column1]
        ,Table1.[Column2] AS [Column2]
        ,Table1.[Column3] AS [Column3]
        ,Table1.[Column4] AS [Column4]
        ,......around 10 colums more selected
    FROM (
        SELECT Table3.[Column1] AS [Column1]
            ,Table3.[Column2] AS [Column2]
            ,Table3.[Column3] AS [Column3]
            ,Table3.[Column4] AS [Column4]
            ,......around 10 colums more selected
        FROM [dbo].[Table3] AS Table3
        INNER JOIN [dbo].[Table5] AS Table5 ON Table3.[Id] = Table5.[Id]
        INNER JOIN [dbo].[Table4] AS Table4 ON Table5.[Id] = Table4.[Id] AS Table1
        INNER JOIN [dbo].[Table6] AS [Table6] ON Table1.[Id] = [Table6].[Id]
        ) AS Table2
    OUTER APPLY (
        SELECT TOP (1) [Table8].[Column1] AS [Column1]
        FROM (
            SELECT Table2.[Column1] AS [Column1]
                ,[Table9].[Column2] AS [Column2]
            FROM [dbo].[Table9] AS [Table9]
            WHERE ([Table9].[Id] = Table2.[Id])
                AND ('tableName' = [Table9].[TableName])
                AND ('columnName' = [Table9].[ColumnName])
                AND ([Table9].[LanguagCode] = @p__linq__0)
            ) AS [Table8]
        ORDER BY [Table8].[Coulmn2] ASC
         AS [Table7]
    )

2 个答案:

答案 0 :(得分:0)

我首先将每个子查询存储在具有相关索引的临时表中。例如,Table2可以像这样重构。这应该可以提高性能。

INSERT INTO #table2
SELECT Table3.[Column1] AS [Column1]
        ,Table3.[Column2] AS [Column2]
        ,Table3.[Column3] AS [Column3]
        ,Table3.[Column4] AS [Column4]
        ,......around 10 colums more selected
    FROM [dbo].[Table3] AS Table3
    INNER JOIN [dbo].[Table5] AS Table5 ON Table3.[Id] = Table5.[Id]
    INNER JOIN [dbo].[Table4] AS Table4 ON Table5.[Id] = Table4.[Id] AS Table1
    INNER JOIN [dbo].[Table6] AS [Table6] ON Table1.[Id] = [Table6].[Id]

答案 1 :(得分:0)

您可以在一个查询中自行创建3个子查询,而不是使用3个子查询。而不是外部应用,您可以使用左连接。您可以编写类似下面的查询。

SELECT Table3.[Column1] AS [Column1]
            ,Table3.[Column2] AS [Column2]
            ,Table3.[Column3] AS [Column3]
            ,Table3.[Column4] AS [Column4]
            ,......around 10 colums more selected
        FROM [dbo].[Table3] AS Table3
        INNER JOIN [dbo].[Table5] AS Table5 ON Table3.[Id] = Table5.[Id]
        INNER JOIN [dbo].[Table4] AS Table4 ON Table5.[Id] = Table4.[Id] AS Table1
        INNER JOIN [dbo].[Table6] AS [Table6] ON Table1.[Id] = [Table6].[Id]
		Left Outer join [dbo].[Table9] AS [Table9] ON [Table9].[Id] = Table3.[Id]
						AND ('tableName' = [Table9].[TableName])
						AND ('columnName' = [Table9].[ColumnName])
						AND ([Table9].[LanguagCode] = @p__linq__0)

但请确保两个查询在继续前进之前为您提供相同的输出。如果这对您有帮助,请告诉我。

谢谢, Sarat