从 nvarchar 列获取 xml 节点值的 SQL 查询非常慢
我有一个名为 tblAdmissions 的表,该表中的一列称为“ProgramInformation”。此列包含 xml 字符串,如
<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" num="1">
<COURSE>reference_project</COURSE>
<FEE>true</FEE>
</row>
<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" num="2">
<COURSE>RoomID</COURSE>
<FEE>99</FEE>
</row>
我正在使用此查询通过交叉应用从该列获取值,但速度非常慢。
使用 vDynamic AS ( 选择
xmlProgramInformation = CAST(ProgramInformation AS XML)
FROM
tblAdmission
)
SELECT
t.p.query('COURSE' ).value('.', 'varchar(max)') AS Decipline,
t.p.query('FEE' ).value('.', 'varchar(max)') AS CourseFee
FROM
vDynamic
CROSS apply xmlProgramInformation.nodes('/DOCUMENT/ROWS/row') AS t(p)
是否有其他方法以便我的查询可以快速运行。
谢谢
I have one table called tblAdmissions, one of the column in this table is called "ProgramInformation". This column contain xml string like
<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" num="1">
<COURSE>reference_project</COURSE>
<FEE>true</FEE>
</row>
<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" num="2">
<COURSE>RoomID</COURSE>
<FEE>99</FEE>
</row>
I am using this query to get values from this columns by using cross apply and its very slow.
With vDynamic AS (
Select
xmlProgramInformation = CAST(ProgramInformation AS XML)
FROM
tblAdmission
)
SELECT
t.p.query('COURSE' ).value('.', 'varchar(max)') AS Decipline,
t.p.query('FEE' ).value('.', 'varchar(max)') AS CourseFee
FROM
vDynamic
CROSS apply xmlProgramInformation.nodes('/DOCUMENT/ROWS/row') AS t(p)
Is there any other method so that my query can run fast.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试一些操作,从最容易实现到最困难:
如果所有数据都在 XML 中,则将 nvarchar 更改为 XML 列。我相对确定这不会对您的性能产生明显影响,但它是后续步骤的基础。
实现 XML 索引。查看在线书籍了解详细信息
如果您正在查询的属性是固定的,那么您可能需要继续将它们分解为固定的表格式。 SQL Server更喜欢SQL; XML 和 XQuery 应用于临时探索和/或变革性工作。
Some things you can try, starting from easiest to implement to most difficult:
If all of your data is in XML, then change the nvarchar to an XML column. I'm relatively certain that this won't have a noticeable impact on your performance, but it's foundational for the next steps.
Implement an XML index. Check Books OnLine for details
If the attributes that you are querying are fixed, then you may want to go ahead and shred them into a fixed table format. SQL Server prefers SQL; XML and XQuery should be used for ad-hoc exploration and/or transformative work.