Is it possible to create an indexed view from Xml Data in Sql Server 2008?

后端 未结 2 1490
你的背包
你的背包 2021-01-27 17:03

I see from the 2005 documentation that you cannot create an indexed view from an Xml column.

Is this possible in 2008 or 2008R2? I can\'t find any documentation saying t

相关标签:
2条回答
  • 2021-01-27 17:33

    I don't believe this is possible. Without a better explanation of what you are trying to do, one suggestion I can offer is to pull the XML apart before insert (perhaps using an instead of trigger, or doing this shredding at the application layer) and storing the part(s) you want to use for the indexed view in separate non-XML columns.

    0 讨论(0)
  • 2021-01-27 17:52

    Depending on your need, what you could do is this:

    • create a set of stored functions that extract certain bits of key information from your XML (function receives XML as input, extracts the info using XPath/XQuery, returns a VARCHAR or INT or something value)

      CREATE FUNCTION dbo.SomeFunction(@Input XML)
      RETURNS VARCHAR(20)
      WITH SCHEMABINDING
      AS BEGIN
         ......
      END
      
    • add those key bits to your base table as computed columns that reference those functions, with the PERSISTED keyword:

      ALTER TABLE dbo.YourTable
         ADD ComputedColumns1 AS dbo.SomeFunction(XmlColumn) PERSISTED
      
    • create your view on the table and those computed columns, with schemabinding:

      CREATE VIEW vYourView 
      WITH SCHEMABINDING
      AS  
            SELECT (list of columns)
            FROM dbo.YourTable
      
    • create a unique, clustered index on that view - unless you've violated any of the requirements of the indexed view, this should work just fine:

      CREATE UNIQUE CLUSTERED INDEX CIX_YourView ON dbo.vYourView(.....)
      

    This works fine if you need to extract a small number of key bits of information from an XML column - it's definitely not recommended for lots of XML elements / values.

    0 讨论(0)
提交回复
热议问题