简体   繁体   中英

XQuery - Search within XML Column in Table

Please find the following query that is being used to search for keyword in a database xml column

The shown query is being used to search for Name,word or brief from an xml column in SQL Server. The CMS in use is Umbraco.

The query was designed initially when the data was limited.

Now the database has millions of records and the query times out unable to fetch data. The query also prioritizes the order in which the search results are retrieved as in if the Name field data search result is returned the CASE 1 is returned else depending on the search data is returned

     SELECT Name,word,brief,              */Selecting Values */
     CASE
       WHEN Name  like '%' THEN 1       */Prioritizing Order in which data to be seen */
     WHEN word  like '%' THEN 2
     WHEN brief like '%' THEN 3
     END AS Search
  from
      (select                                 
            A.xml.value('(//@node)[1]','nvarchar(20)') as Name,   /* XQuery */    
          A.xml.value('(//word)[1]','nvarchar(225)') as word,
          A.xml.value('(//brief)[1]','nvarchar(max)')as brief
  from 
         (Select Convert(xml, xml) AS XML 
          from [dbo].[cmsContentXml]) AS B
          Cross Apply xml.nodes('//items/item') AS A(xml)) D  
        where  ((Name like '%')                                   /*Condition */
               OR(word like '%')   
               OR(brief like '%'))
          group by word,Name,brief
      order by 3 ASC

Please help with a solution on optimizing or rewriting this query to search for data in the xml column. Also apart from retrieving data from the column.

When in the front end the search button is clicked without entering any keyword the click operation has to retrieve all the items from the database.

Thanks in advance

First, don't allow a search with no keyword, direct the user to a browse mechanism instead. There's no point in having the overhead of a search query if there's nothing to search for.

Second, Select Convert(xml, xml) is unnecessary overhead, your database column should be xml if it's storing xml. Having the column with an xml type also allows you to put xml indexes on it which will help speed things up. See http://msdn.microsoft.com/en-us/library/ms191497.aspx for more information on XML Indexes. Also, without the conversion you no longer need to keep that as a subquery.

Third don't use the ORDER BY 3 syntax, use the column name. ORDER BY 3 is not ANSI standard and also can cause maintainability problems.

Fourth, your xml sample data appears to be invalid xml (an unclosed data tag for brief, no closing element for id, no opening element for block). Please check your data, if it's actually broken like that in the real database, it will need to be fixed as bad xml does slow down the node searches.

Fifth, avoid single letter aliases, it's another maintainability issue.

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