简体   繁体   English

SQL Server 2008 XML列性能问题

[英]SQL Server 2008 XML column performance Issue

We have a table with an XML column holding quite a bit of data, this has worked fine in our dev environments but as the table grew in size (close to 10,000 rows) we started seeing performance issues. 我们有一个带有XML列的表,其中包含很多数据,这在我们的开发环境中运行良好,但是随着表的大小增加(接近10,000行),我们开始发现性能问题。

Just doing SELECT * takes 12 seconds alone... 仅执行SELECT *仅需12秒钟...

Any suggestions to remedy this? 有什么建议可以解决这个问题吗?

Thanks in advance. 提前致谢。

You could check out several things - at least if the performance hit is mostly when dealing with and selecting data from the XML column: 您可以检查几件事-至少在处理和从XML列中选择数据时,性能下降是否最主要:

  • you can put an index on your XML column - this can help if you need to grab lots of data from within the XML column. 您可以在XML列上放置索引 -如果您需要从XML列中获取大量数据,则可以提供帮助。 One word of caution: XML indices use a lot of disk space - in our case, a database of 1.5GB rocketed up to 11GB in disk size .... use with caution! 请注意:XML索引会占用大量磁盘空间-在我们的例子中,1.5GB的数据库的磁盘大小猛增至11GB ....请谨慎使用!

  • you can "surface" certain elements from within your XML onto the "parent" table as computed, persisted columns and thus find the rows you need more quickly (needs a stored function - but it's really quite a nice technique if you have this need) 您可以将XML内的某些元素作为计算的,持久化的列“浮出水面”到“父”表上,从而更快地找到所需的行(需要存储函数-但是,如果您有此需求,这确实是一种很好的技术)

Also: never do a SELECT * anyway - and if you don't need the XML column - don't select it - it will be quite verbose and use quite a bit of memory. 另外:无论如何都不要执行SELECT * -如果不需要XML列-不要选择它-这将很冗长,并且会占用大量内存。

If querying records, and filtering on data within an XML data type, you're asking SQL Server to examine all the XML content to find results. 如果查询记录并过滤XML数据类型内的数据,则要求SQL Server检查所有XML内容以查找结果。

To speed things up, combine XML data type filters with full text search expressions. 为了加快速度,请将XML数据类型过滤器与全文搜索表达式结合使用。 The full text search narrows down the results (depending how specific you are) before the XML is parsed and searched. 在解析和搜索XML之前,全文搜索会缩小结果范围(取决于您的具体程度)。 It can save a lot of CPU and IO. 它可以节省大量CPU和IO。 Here's an example: 这是一个例子:

SELECT * 
FROM   Table 
WHERE  CONTAINS(XmlColumn,'value') 
AND    XmlColumn.exist('/element/element/text()[contains(.,"value")]') = 1

This is documented by Microsoft here , and you can examine your before and after by running your queries with statistics on. 这是由Microsoft 在此处记录的 ,您可以通过运行带有统计信息的查询来检查之前和之后。 Here's how you turn statistics on: 这是您打开统计信息的方式:

SET STATISTICS IO ON;
SET STATISTICS TIME ON;

Just to add a bit to what marc_s said: I would also recommend an index -- 10k records is not very much. 只是要补充一下marc_s所说的话:我还建议使用索引-10k记录不是很多。 But make sure that you are adding an index on the correct thing -- usually the best places to put indexes are on columns that are used for JOIN conditions, WHERE clauses, or ORDER BY clauses. 但是请确保在正确的地方添加索引-通常,放置索引的最佳位置是在用于JOIN条件,WHERE子句或ORDER BY子句的列上。 If your query is not using the XML itself for these cases, you may be better served by creating an index on a different column (for example if you are doing a lookup on an ID which is in a non-XML column, you might see more benefit by creating the index on the ID). 如果在这些情况下您的查询未使用XML本身,则可以通过在其他列上创建索引来更好地服务(例如,如果您正在非XML列中的ID上进行查找,则可能会看到通过在ID上创建索引获得更多收益)。

If actually extracting the XML data is slow, you could consider making a covering index (using INCLUDE keyword), where you have an index on the ID but INCLUDE an expression that extracts the value from the XML column. 如果实际上提取XML数据的速度很慢,则可以考虑创建覆盖索引(使用INCLUDE关键字),在该索引上您具有ID上的索引,但是包含一个从XML列中提取值的表达式。 This made a huge difference for me on one of my projects, but as always make sure to test the performance. 这对我的一个项目产生了巨大的影响,但请务必确保测试性能。

Of course, if your queries are actually doing JOIN/WHERE/ORDER BY on the XML data then you should probably do what marc_s recommends and create the index on the XML column. 当然,如果您的查询实际上是对XML数据执行JOIN / WHERE / ORDER BY,那么您可能应该执行marc_s建议的操作并在XML列上创建索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM