First, verify that querying the XML is what is taking the time (if your query is any more complex than what you showed). You can do this by looking at the query plan and looking for steps such as Table Value Function [XML Reader].I may not have understood your description well enough, but it seems to me that if you are looking for the first AnswerSet node then you can change your query to one of the following (or may be both) select n2.* from nodehistory n2 CROSS APPLY n2.content.nodes('Answers/AnswerSet[1]') T(c) WHERE c.value('./Answer[@questionId="ProductCode"][1]','varchar(100)') ='J154'
or perhaps,;with cte as( SELECT n2.*,ROW_NUMBER() OVER (PARTITION BY nodeid ORDER BY createDate DESC) AS RN FROM nodehistory n2)select n2.* from cte n2 CROSS APPLY n2.content.nodes('Answers/AnswerSet') T(c) WHERE c.value('./Answer[@questionId="ProductCode"][1]','varchar(100)') ='J154'AND RN = 1;
Also, I assume you created a primary XML index. You could consider creating a PATH secondary index, but as with all XML indexes, it takes up space and slows down udpates/insertions, so use that as the last option.