Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8608499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:37:37+00:00 2026-06-12T03:37:37+00:00

I have two tables, XMLtable and filterTable . I need all the XMLtable.ID values

  • 0

I have two tables, XMLtable and filterTable.

I need all the XMLtable.ID values from XMLtable where the data in Col_X contains MyElement, the contents of which matches filterColumn in filterTable.

The XML for each row in Col_X may contain multiple MyElement‘s, and I want that ID in case ANY of those elements match ANY of the values in filterColumn.

The problem is that those columns are actually of varchar(max) datatype, and the table itself is huge (like 50GB huge). So this query needs to be as optimized as possible.

Here’s an example for where I am now, which merely returns the row where the first matching element equals one of the ones I’m looking for. Due to a plethora of different error messages I can’t seem to be able to change this to compare to all of the same named elements as I want to.

SELECT ID, 
   CAST(Col_X AS XML).value('(//*[local-name()=''MyElement''])', N'varchar(25)') 
FROM  XMLtable

…and then compare the results to filterTable. This already takes 5+ minutes.

What I’m trying to achieve is something like:

SELECT ID
FROM XMLtable
WHERE CAST(Col_X AS XML).query('(//*[local-name()=''MyElement''])') 
   IN (SELECT filterColumn FROM filterTable)

The only way I can currently achieve this is to use the LIKE operator, which takes like a thousand times longer.

Now, obviously it’s not an option to start changing the datatypes of the columns or anything else. This is what I have to work with. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:37:39+00:00Added an answer on June 12, 2026 at 3:37 am

    Try this:

    SELECT
      ID,
      MyElementValue
    FROM 
      (
        SELECT ID, myE.value('(./text())[1]', N'VARCHAR(25)') AS 'MyElementValue'
        FROM XMLTable
          CROSS APPLY (SELECT CAST(Col_X AS XML)) as X(Col_X)
          CROSS APPLY X.Col_X.nodes('(//*[local-name()="MyElement"])') as T2(myE)
      ) T1
    WHERE MyElementValue IN (SELECT filterColumn FROM filterTable)
    

    and this:

    SELECT
      ID,
      MyElementValue
    FROM 
      (
        SELECT ID, myE.value('(./text())[1]', N'VARCHAR(25)') AS 'MyElementValue'
        FROM XMLTable
          CROSS APPLY (SELECT CAST(Col_X AS XML)) as X(Col_X)
          CROSS APPLY X.Col_X.nodes('//MyElement') as T2(myE)
      ) T1
    WHERE MyElementValue IN (SELECT filterColumn FROM filterTable)
    

    Update

    I think that you are experiencing what is described here Compute Scalars, Expressions and Execution Plan Performance. The cast to XML is deferred to each call to the value function. The test you should make is to change the datatype of Col_X to XML.

    If that is not an option you could query the rows you need from XMLTable into a temporary table that has an XML column and then do the query above against the temporary table without the need to cast to XML.

    CREATE TABLE #XMLTable
    (
      ID int,
      Col_X xml
    )
    
    INSERT INTO #XMLTable(ID, Col_X)
    SELECT ID, Col_X
    FROM XMLTable
    
    SELECT
          ID,
          MyElementValue
        FROM 
          (
            SELECT ID, myE.value('(./text())[1]', N'varchar(25)') AS 'MyElementValue'
            FROM #XMLTable
              CROSS APPLY Col_X.nodes('//MyElement') as T2(myE)
          ) T1
        WHERE MyElementValue IN (SELECT filterColumn FROM filterTable)
    
    
    DROP TABLE #XMLTable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables: A, B. I need to select all data from B.
I have two tables in MYSQL database which contains same Columns by counting and
Have two tables say ABC and XYZ and contain one column which data will
I have two tables that I need to combine to get the data I
I have two tables in my MySQL database, one is a library of all
I have two tables. One table contains information about Assets, another Table about their
I have two tables, Users and TempUsers and I need to do operations on
Have two sets of data (two tables) for patient records, one 1999-2003, the other
I have two tables users and work_orders. Now I need to get work_orders results
I have two tables. One is Employee_Mstr and other is EmployeeLeaveRequest_mstr . My data:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.