<p>There is a field in my company's "Contacts" table. In that table, there is an XML type column. The column holds misc data about a particular contact. EG. </p>

<pre><code>&lt;contact&gt;
&lt;refno&gt;123456&lt;/refno&gt;
&lt;special&gt;a piece of custom data&lt;/special&gt;
&lt;/contact&gt;
</code></pre>

<p>The tags below <code>contact</code> can be different for each contact, and I must query these fragments
alongside the relational data columns in the same table.</p>

<p>I have used constructions like:</p>

<pre><code>SELECT c.id AS ContactID,c.ContactName as ForeName,
c.xmlvaluesn.value('(contact/Ref)[1]', 'VARCHAR(40)') as ref,    
INNER JOIN ParticipantContactMap pcm ON c.id=pcm.contactid 
AND pcm.participantid=2140
WHERE xmlvaluesn.exist('/contact[Ref = "118985"]') = 1
</code></pre>

<p>This method works ok but, it takes a while for the Server to respond.
I have also investigated using the nodes() function to parse the XML nodes and exist() to test if a nodes holds the value I'm searching for.</p>

<p>Does anyone know a better way to query XML columns??</p>

[{'answer_id': 52097, 'author': 'Paulj', 'author_id': 5433, 'author_profile': 'https://Stackoverflow.com/users/5433', 'pm_score': 2, 'selected': True, 'text': '<p>I\'ve found the msdn xml best practices helpful for working with xml blob columns, might provide some inspiration...\n<a href="http://msdn.microsoft.com/en-us/library/ms345115.aspx#sql25xmlbp_topic4" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms345115.aspx#sql25xmlbp_topic4</a></p>\n'}, {'answer_id': 52152, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 0, 'selected': False, 'text': '<p>In addition to the page mentioned by @pauljette, this page has good performance optimization advice:</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/ms345118.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms345118.aspx</a></p>\n\n<p>There\'s a lot you can do to speed up the performance of XML queries, but it will never be as good as properly indexed relational data.  If you are selecting one document and then querying inside just that one, you can do pretty well, but when your query needs to scan through a bunch of similar documents looking for something, it\'s sort of like a key lookup in a relational query plan (that is, <em>slow</em>).</p>\n'}, {'answer_id': 52464, 'author': 'Tina Marie', 'author_id': 4666, 'author_profile': 'https://Stackoverflow.com/users/4666', 'pm_score': 2, 'selected': False, 'text': '<p>If you are doing one write and a lot of reads, take the parsing hit at write time, and get that data into some format that is more query-able.  A first suggestion would be to parse them into a related but separate table, with name/value/contactID columns.</p>\n'}, {'answer_id': 60377, 'author': 'MotoWilliams', 'author_id': 2730, 'author_profile': 'https://Stackoverflow.com/users/2730', 'pm_score': 0, 'selected': False, 'text': '<p>If you have a XSD for your Xml then you can import that into your database and you can then build indexes for your Xml data.</p>\n'}, {'answer_id': 21453759, 'author': 'Shailesh', 'author_id': 1241943, 'author_profile': 'https://Stackoverflow.com/users/1241943', 'pm_score': 0, 'selected': False, 'text': "<p>Try this</p>\n\n<p>SELECT  * FROM  conversionupdatelog WHERE \nconvert(XML,colName).value('(/leads/lead/@LeadID=''xyz@airproducts.com'')[1]', 'varchar(max)')='true'</p>\n"}]
['https://Stackoverflow.com/questions/52084', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5197/']