我正在尝试查询一个表,该表有一个存储XML格式的事务数据的列。我的文件管理器选项实际上是我的XML数据中的节点之一。以下是我的查询
SELECT eRefNo, eCreationDate, eData
FROM TableA
WHERE CAST(CAST(CAST(eData AS text) AS xml ).query('Test/Header/ExportToGMACS/text()')AS nvarchar(1000)) = 'True'
AND ((CAST(CAST(CAST(eData AS text) AS xml ).query('Test/PaymentAccountingDetail/PaymentAccounting1/KindOfPayment[@MPText]')AS nvarchar(1000)) = 'Claims')
OR (CONVERT(datetime, CAST(CAST(CAST(eData AS text) AS xml ).query('Test/PaymentAccountingDetail/PaymentAccounting1/PaymentDate/text()')AS nvarchar(1000)), 103) >= convert(datetime, '01/01/2011', 103) AND (CONVERT(datetime, CAST(CAST(CAST(eData AS text) AS xml ).query('Test/PaymentAccountingDetail/PaymentAccounting1/PaymentDate/text()')AS nvarchar(1000)), 103) <= convert(datetime, '31/07/2011', 103)))
OR (CONVERT(datetime, CAST(CAST(CAST(eData AS text) AS xml ).query('Test/PaymentAccountingDetail/PaymentAccounting2/PaymentDate/text()')AS nvarchar(1000)), 103) >= convert(datetime, '01/01/2011', 103) AND (CONVERT(datetime, CAST(CAST(CAST(eData AS text) AS xml ).query('Test/PaymentAccountingDetail/PaymentAccounting2/PaymentDate/text()')AS nvarchar(1000)), 103) <= convert(datetime, '31/07/2011', 103)))
OR (CONVERT(datetime, CAST(CAST(CAST(eData AS text) AS xml ).query('Test/PaymentAccountingDetail/PaymentAccounting3/PaymentDate/text()')AS nvarchar(1000)), 103) >= convert(datetime, '01/01/2011', 103) AND (CONVERT(datetime, CAST(CAST(CAST(eData AS text) AS xml ).query('Test/PaymentAccountingDetail/PaymentAccounting3/PaymentDate/text()')AS nvarchar(1000)), 103) <= convert(datetime, '31/07/2011', 103)))
OR (CONVERT(datetime, CAST(CAST(CAST(eData AS text) AS xml ).query('Test/PaymentAccountingDetail/PaymentAccounting4/PaymentDate/text()')AS nvarchar(1000)), 103) >= convert(datetime, '01/01/2011', 103) AND (CONVERT(datetime, CAST(CAST(CAST(eData AS text) AS xml ).query('Test/PaymentAccountingDetail/PaymentAccounting4/PaymentDate/text()')AS nvarchar(1000)), 103) <= convert(datetime, '31/07/2011', 103)))
OR (CONVERT(datetime, CAST(CAST(CAST(eData AS text) AS xml ).query('Test/PaymentAccountingDetail/PaymentAccounting5/PaymentDate/text()')AS nvarchar(1000)), 103) >= convert(datetime, '01/01/2011', 103) AND (CONVERT(datetime, CAST(CAST(CAST(eData AS text) AS xml ).query('Test/PaymentAccountingDetail/PaymentAccounting5/PaymentDate/text()')AS nvarchar(1000)), 103) <= convert(datetime, '31/07/2011', 103)))
OR (CONVERT(datetime, CAST(CAST(CAST(eData AS text) AS xml ).query('Test/PaymentAccountingDetail/PaymentAccounting6/PaymentDate/text()')AS nvarchar(1000)), 103) >= convert(datetime, '01/01/2011', 103) AND (CONVERT(datetime, CAST(CAST(CAST(eData AS text) AS xml ).query('Test/PaymentAccountingDetail/PaymentAccounting6/PaymentDate/text()')AS nvarchar(1000)), 103) <= convert(datetime, '31/07/2011', 103))))
当我将日期范围设置为从01/Jan/2011
搜索到31/Jul/2011
时,与我的日期范围从01/Jul/2011
到31/Jul/2011
的时间相比,它更快。
我无法理解这种特殊的性能问题。一直以来我认为更宽的日期范围需要更长的时间来执行,因为它有更多的行要返回?
有任何专家请指教。
由于 约翰逊
我已经尝试了,无论如何,通过减少单个演员,它对我的查询没有多大帮助。
我的主要问题是较宽的日期范围过滤器比较短的日期范围过滤器花费的时间更短。
任何想法是什么原因?
我正在使用MS SQL Server 2005
答案 0 :(得分:0)
此查询未使用索引进行优化,因此必须进行表扫描以确定应返回哪些行。这意味着将根据where子句检查表中的每一行。
使用cast
和query
的技术相当昂贵所以我的猜测(我还没有测试过)是查询优化器快捷方式or
条件因此,当你受到打击时,它将不会继续检查连续的条件。这意味着您可以通过扩展日期范围来节省时间,因为将针对所有 or
条件检查更少的行。