简体   繁体   English

SQL Server的xml列上的性能

[英]performance on xml column in sql server

I have normalized tables which keep data in the following format. 我有规范化的表,这些表将数据保留为以下格式。

First Table name LogBook columns are JourneyID, LocationID, PositionID, ID, 第一表名称LogBook列是JourneyID,LocationID,PositionID,ID,

second table LogBookEvents columns are LogBookID, LifeCodeID, LifeCode, LifeCodeValue, TotalValue 第二个表LogBookEvents列是LogBookID,LifeCodeID,LifeCode,LifeCodeValue,TotalValue

the first table has 3.4 million records and the second table has 17.3 million records. 第一个表具有340万条记录,第二个表具有1730万条记录。

I found this the performance on these tables are big heavy even I have all the necessary indexes on the tables. 我发现即使我在表上拥有所有必需的索引,这些表的性能也非常沉重。

therefore I have designed a new table which has XML format 因此,我设计了一个具有XML格式的新表

new table is.. LogbookLifeCodes, columns are JourneyID, LifeCode (XML Type) 新表是.. LogbookLifeCodes,列是JourneyID,LifeCode(XML类型)

by doing this, I have reduced the number of records to 900,000 通过这样做,我将记录数减少到900,000

and the LifeCode hold data in the following a format 并且LifeCode以以下格式保存数据

<LogBookLifeCodeEvents><LifeCodes>
<LogBookID>11</LogBookID>
<LocationID>0</LocationID>
<PositionID>0</PositionID>
<LifeCodeID>4</LifeCodeID>
<LifeCode>FH</LifeCode>
<LifeValue>0.0000</LifeValue>
<LifeTotal>0.0000</LifeTotal>

and now am using the following query to return data in table format. 现在正在使用以下查询以表格式返回数据。

Select Tbl.Col.value('LogBookID[1]', 'int'),  
   Tbl.Col.value('LocationID[1]', 'int'),  
   Tbl.Col.value('LocationPositionID[1]', 'int'),
   Tbl.Col.value('LifeCodeID[1]', 'int'),
   Tbl.Col.value('LifeCode[1]', 'varchar(20)') from LogbookLifeCodes outer apply LogbookLifeCodes.LifeCode.nodes('//LogBookLifeCodeEvents/LifeCodes') Tbl(Col) where Tbl.Col.value('LogBookID[1]', 'int') = 11

however this query takes 4 mins to run get the value for LogBookID = 11, this is not acceptable, returning 130,000 records. 但是,此查询需要4分钟才能运行,以获取LogBookID = 11的值,这是不可接受的,返回130,000条记录。

but if I query the above-normalized tables only takes 01 seconds, same number of records 但是如果我查询以上规范化的表只需要01秒,记录数就会相同

the XML columns on the second table having indexes, primary and secondary. 第二个表上的XML列具有主索引和辅助索引。

when I check the execution plan, the normalized table index (clustered) cost 73% 当我检查执行计划时,标准化表索引(聚集)的成本为73%

but on the XML column index only cost 45 % 但在XML列索引上仅花费45%

just would like to know, using XML column rather using relational tables better ? 只是想知道,使用XML列而不是使用关系表更好? or is any other way to improve the performance of XML column as the second table has less records compare to the first structure ? 还是因为第二个表的记录少于第一个结构,所以有什么其他方法可以提高XML列的性能?

appreciate your commends and advices 感谢您的赞美和建议

many thanks 非常感谢

Referencing this article ( Performance tips of using XML data in SQL Server ) note the three tips; 参考本文( 在SQL Server中使用XML数据的性能提示 ),请注意以下三个提示:

  1. Promoting to relational columns by using persisted columns. 通过使用持久化列提升为关系列。
  2. Typing the data. 键入数据。
  3. Indexing the XML. 索引XML。

You may be indexing the XML, but indexing XML is (to me) even more difficult than indexing relational data based on query plans. 您可能正在为XML编制索引,但是(对我而言)为XML编制索引比根据查询计划为关系数据编制索引更加困难。 The point of the article is that, if you work really, really hard, you may be able to approach relational levels of performance with XML data, not exceed it. 本文的重点是,如果您确实非常努力地工作,则可以使用XML数据达到性能的相关级别,而不是超过它。

I understand why you might approach it this way conceptually, but SQL Server is a relational database. 我理解为什么您可能会在概念上采用这种方式,但是SQL Server是一个关系数据库。 You will almost always get the best performance out of relational objects which are managed appropriately. 通过适当管理的关系对象,您几乎总是会获得最佳性能。

You note that, "I found this the performance on these tables are big heavy even I have all the necessary indexes on the tables", but later state that the relational query of note takes only 1 second where the XML based query takes four minutes. 您注意到,“即使我在表上拥有所有必要的索引,我也发现这些表的性能非常沉重”,但后来指出note的关系查询仅需1秒,而基于XML的查询则需4分钟。

Based on the information provided I do not understand the problem you are trying to solve, but you should not expect better performance on XML data than relational data in SQL Server. 根据提供的信息,我不了解您要解决的问题,但是您不应该期望XML数据比SQL Server中的关系数据具有更好的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM