我在 ASP.NET 应用程序中有一段代码,它从参数列表构建 SQL 查询。参数的数量可能会有所不同,因此可以将各种条件添加到此查询中。数据库是 Microsoft SQL Server 2008。
所有 AND 和 OR 都是以编程方式生成的。
该查询的执行时间超过 3 秒,但经过一些分析和索引后,它的运行时间不到一秒。我仍然认为查询本身可以优化。我看过执行计划,但这对我来说意义不大——不是 SQL 专家。
我想知道查询是否可以以更智能的方式完成 - 我无法弄清楚。下面是一个查询示例:
SELECT [id], [WorkTitle], [CreateDate], [UpdateDate], [Writer], [ValidFrom],
[ValidTo], [Text]
FROM dbo.Texts T
WHERE Category_id = 3 AND '2012-11-06' BETWEEN ValidFrom AND ValidTo
AND (EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 1 AND CL.Value = '95068')
OR NOT EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 1))
AND (EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 2 AND CL.Value = 'C')
OR NOT EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 2))
AND (EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 3 AND CL.Value = 'HEL')
OR NOT EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 3))
AND (EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 4 AND CL.Value = 'CC')
OR NOT EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 4))
AND (EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 5 AND CL.Value = NULL)
OR NOT EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 5))
AND (EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 7 AND CL.Value = '321')
OR NOT EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 7))
AND (EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 9 AND CL.Value = 'DK7778')
OR NOT EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 9))
AND (EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 10 AND CL.Value = 'TFS')
OR NOT EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 10))
AND (EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 11 AND CL.Value = 'TMP')
OR NOT EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 11))
AND (EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 13 AND CL.Value = 'OY-VKB')
OR NOT EXISTS (SELECT 'X' FROM dbo.Criteria_List CL WHERE T.id = CL.Text_id AND CL.Criteria_id = 13))
任何提示和技巧表示赞赏。
干杯詹斯