简体   繁体   中英

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,

second table LogBookEvents columns are LogBookID, LifeCodeID, LifeCode, LifeCodeValue, TotalValue

the first table has 3.4 million records and the second table has 17.3 million records.

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

new table is.. LogbookLifeCodes, columns are JourneyID, LifeCode (XML Type)

by doing this, I have reduced the number of records to 900,000

and the LifeCode hold data in the following a format

<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.

but if I query the above-normalized tables only takes 01 seconds, same number of records

the XML columns on the second table having indexes, primary and secondary.

when I check the execution plan, the normalized table index (clustered) cost 73%

but on the XML column index only cost 45 %

just would like to know, using XML column rather using relational tables better ? or is any other way to improve the performance of XML column as the second table has less records compare to the first structure ?

appreciate your commends and advices

many thanks

Referencing this article ( Performance tips of using XML data in SQL Server ) note the three tips;

  1. Promoting to relational columns by using persisted columns.
  2. Typing the data.
  3. Indexing the XML.

You may be indexing the XML, but indexing XML is (to me) even more difficult than indexing relational data based on query plans. 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.

I understand why you might approach it this way conceptually, but SQL Server is a relational database. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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