简体   繁体   中英

Index on nvarchar datatype with XML data in SQL Server

I have a table with this structure :

CREATE TABLE [dbo].[GasTests]
(
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [GasReceptionId] [bigint] NOT NULL,
    [LineId] [bigint] NOT NULL,
    [LabelCode] [nvarchar](max) NULL,
    [StartTestDatetime] [datetime] NULL,
    [EndTestDatetime] [datetime] NULL,
    [ResultTest] [nvarchar](max) NULL,

    CONSTRAINT [PK_dbo.GasTests] 
        PRIMARY KEY CLUSTERED ([Id] ASC)
)

In resulttest I store a xml datatype like this :

<?xml version="1.0" encoding="utf-16"?>
<GasResultTestParameterView xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ReceptionId>86</ReceptionId>
  <ISIRI_9426_C1>on</ISIRI_9426_C1>
  <ISIRI_9426_C2>on</ISIRI_9426_C2>
  <ISIRI_9426_C3>on</ISIRI_9426_C3>
  <ISIRI_9426_C4>on</ISIRI_9426_C4>
  <ISIRI_9426_C5>on</ISIRI_9426_C5>
  <ISIRI_9426_C6>on</ISIRI_9426_C6>
  <ISIRI_9426_C7>on</ISIRI_9426_C7>
  <ISIRI_9426_C8>on</ISIRI_9426_C8>
  <ISIRI_9426_C9>on</ISIRI_9426_C9>
  <ISIRI_9426_C10>on</ISIRI_9426_C10>
  <ISIRI_9426_C11>on</ISIRI_9426_C11>
  <ISIRI_9426_C12>on</ISIRI_9426_C12>
  <ISIRI_9426_C13>on</ISIRI_9426_C13>
  <ISIRI_9426_C14>on</ISIRI_9426_C14>
  <ISIRI_9426_C15>on</ISIRI_9426_C15>
  <ISIRI_9426_C16>on</ISIRI_9426_C16>
  <ISIRI_9426_C17>on</ISIRI_9426_C17>
  <ISIRI_9426_C18>on</ISIRI_9426_C18>
  <ISIRI_9426_C19>on</ISIRI_9426_C19>
  <ISIRI_9426_C20>on</ISIRI_9426_C20>
  <ISIRI_9426_C21>on</ISIRI_9426_C21>
  <ISIRI_9426_C22>on</ISIRI_9426_C22>
  <ISIRI_9426_C23>on</ISIRI_9426_C23>
  <ISIRI_9426_C24>on</ISIRI_9426_C24>
  <ISIRI_9426_C25>on</ISIRI_9426_C25>
  <ISIRI_9426_C26>on</ISIRI_9426_C26>
  <ISIRI_9426_C27>on</ISIRI_9426_C27>
  <ISIRI_9426_C28>on</ISIRI_9426_C28>
  <ISIRI_9426_C29>on</ISIRI_9426_C29>
  <ISIRI_9426_C30>on</ISIRI_9426_C30>
  <ISIRI_9747_K1>on</ISIRI_9747_K1>
  <ISIRI_9747_K2>on</ISIRI_9747_K2>
  <ISIRI_9747_K3>on</ISIRI_9747_K3>
  <ISIRI_9747_K4>on</ISIRI_9747_K4>
  <ISIRI_9747_K5>on</ISIRI_9747_K5>
  <ISIRI_9747_K6>on</ISIRI_9747_K6>
  <ISIRI_9747_K7>on</ISIRI_9747_K7>
  <ISIRI_9747_K8>on</ISIRI_9747_K8>
  <ISIRI_9747_K9>on</ISIRI_9747_K9>
  <ISIRI_9747_K10>on</ISIRI_9747_K10>
  <ISIRI_9747_K11>on</ISIRI_9747_K11>
  <ISIRI_9747_K12>on</ISIRI_9747_K12>
  <ISIRI_9747_K13>on</ISIRI_9747_K13>
  <ISIRI_9747_K14>on</ISIRI_9747_K14>
  <ISIRI_9747_K15>on</ISIRI_9747_K15>
  <ISIRI_9747_K16>on</ISIRI_9747_K16>
  <ISIRI_9747_K17>on</ISIRI_9747_K17>
  <ISIRI_9747_K18>on</ISIRI_9747_K18>
  <ISIRI_9747_K19>on</ISIRI_9747_K19>
  <ISIRI_9747_K20>on</ISIRI_9747_K20>
  <ISIRI_9747_K21>on</ISIRI_9747_K21>
  <ISIRI_9747_K22>on</ISIRI_9747_K22>
  <ISIRI_9747_K23>on</ISIRI_9747_K23>
  <ISIRI_9747_K24>on</ISIRI_9747_K24>
  <ISIRI_9747_K25>on</ISIRI_9747_K25>
  <ISIRI_9747_K26>on</ISIRI_9747_K26>
  <ISIRI_9747_K27>on</ISIRI_9747_K27>
  <ISIRI_9747_K28>on</ISIRI_9747_K28>
  <ISIRI_9747_K29>on</ISIRI_9747_K29>
  <ISIRI_9747_K30>on</ISIRI_9747_K30>
  <ISIRI_9747_K31>on</ISIRI_9747_K31>
  <ISIRI_9747_K32>on</ISIRI_9747_K32>
  <ISIRI_9747_K33>on</ISIRI_9747_K33>
  <ISIRI_9747_K34>on</ISIRI_9747_K34>
  <ISIRI_9747_K35>on</ISIRI_9747_K35>
  <ISIRI_9747_K36>on</ISIRI_9747_K36>
  <ISIRI_9747_K37>on</ISIRI_9747_K37>
  <ISIRI_9747_K38>on</ISIRI_9747_K38>
  <ISIRI_9747_K39>on</ISIRI_9747_K39>
  <ISIRI_9747_K40>on</ISIRI_9747_K40>
  <ISIRI_9747_K41>on</ISIRI_9747_K41>
  <ISIRI_9747_K42>on</ISIRI_9747_K42>
  <ISIRI_9747_K43>on</ISIRI_9747_K43>
  <ISIRI_9747_K44>on</ISIRI_9747_K44>
  <ISIRI_9747_K45>on</ISIRI_9747_K45>
  <ISIRI_9747_K46>on</ISIRI_9747_K46>
  <TapCompany>LANDIRENZO</TapCompany>
  <TapSerialNumber>039837</TapSerialNumber>
  <RegulatorCompany>OMVL</RegulatorCompany>
  <RegulatorSerialNumber>039837</RegulatorSerialNumber>
  <CapsuleCompany>JINDUN</CapsuleCompany>
  <CapsuleSerialNumber>1004986</CapsuleSerialNumber>
  <CapsuleType>2</CapsuleType>
  <CapsuleBuiltDate>1391</CapsuleBuiltDate>
  <CapsuleExpireDate>1402</CapsuleExpireDate>
  <GasSystemGeneration>2</GasSystemGeneration>
  <Remark>تایید</Remark>
  <PassedISIRI9747>on</PassedISIRI9747>
  <PassedISIRI9426>on</PassedISIRI9426>
  <PassedISIRI6792>noneed</PassedISIRI6792>
</GasResultTestParameterView>

I need a lot of reports based on my xml result. But when I want to search an item in my XML, it takes a lot of time. How can I make my table and my xml faster?

If you define your Resulttest column as XML , then you can fetch individual bits from your XML and store them into your table structure - as computed columns .

Storing as XML has two main benefits:

  • the storage is more efficient, than storing it as plain text - the XML will be "shredded" internally, and stored as tokens. This cuts back on the storage size, and it makes accessing the XML faster
  • using a XML column allows you to use the XQuery function on it, to handle the XML structures

First, define a scalar function like this (assuming you want to "surface" the TapCompany value):

CREATE FUNCTION dbo.GetTapCompany(@Input XML)
RETURNS VARCHAR(100)
AS
BEGIN
    DECLARE @Result VARCHAR(100)

    SELECT @Result = @Input.value('(/GasResultTestParameterView/TapCompany)[1]', 'VARCHAR(100)')

    RETURN @Result
END

This function takes the XML value (from your table row) as input, goes into the XML and gets the TapCompany value, and returns it.

Then, add this as a column to your database table:

ALTER TABLE dbo.GasTests
ADD TapCompany AS dbo.GetTapCompany(Resulttest);

Now, you should be able to select from your table, and get the value of TapCompany along side your other table values:

SELECT 
    Id,
    GasReceptionId,
    LineId,
    LabelCode,
    TapCompany
FROM   
    dbo.GasTests

Unfortunately, you cannot make this column persisted - so each time you select from that table and include that column, the scalar function that provides the value will be called. Therefore, I'm unsure if you'll see any performance improvement - but I think that should be quite easy to check for you - with your real data - give it a try!

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