SQL Server String Aggregation: FOR XML PATH vs. STRING_AGG Performance Comparison and Optimization

In SQL Server, string aggregation is a common requirement for data reporting, logging, and data display. Traditional methods often use FOR XML PATH, but since SQL Server 2017, STRING_AGG has become the new string aggregation function. Although both methods achieve the same functionality, their performance differences can significantly impact system efficiency when handling large-scale data.
This article explores the performance differences between FOR XML PATH and STRING_AGG and provides optimization suggestions to help developers choose the most suitable solution.
In SQL Server, strings can typically be aggregated using the following methods:
This method uses XML aggregation to merge multiple rows into a single string.
|
|
Introduced in SQL Server 2017, this new aggregate function simplifies the string aggregation process and supports specifying delimiters.
|
|
A table with 5 million records was generated, structured as follows:
|
|
The goal is to concatenate product_name into a comma-separated string for each group.
|
|
Each test case was executed 3 times, with cache cleared before taking the average value. The complete metrics were captured using SET STATISTICS IO, TIME ON.
Aggregation Scale | FOR XML PATH | STRING_AGG | Performance Improvement |
---|---|---|---|
10 rows/group | 320 ms | 285 ms | 12% |
500 rows/group | 1,850 ms | 1,120 ms | 40% |
5,000 rows/group | 18,200 ms | 6,740 ms | 63% |
For smaller aggregation tasks, both methods perform similarly, with STRING_AGG slightly outperforming FOR XML PATH. However, the performance difference is minimal and unlikely to be a bottleneck.
As the scale increases, FOR XML PATH’s performance significantly decreases due to the overhead of generating XML and handling escape characters.
In large-scale aggregation scenarios, FOR XML PATH’s performance issues become even more pronounced. It requires handling large data volumes and converting them to XML, which impacts query efficiency.
In complex queries, outdated syntax like FOR XML PATH can still exist and impact performance. Tools like SQLFLASH can automate optimization, especially when converting FOR XML PATH to the more efficient STRING_AGG syntax, significantly reducing execution time.
Example:
|
|
In SQL Server, STRING_AGG is the optimal method for string aggregation, particularly for SQL Server 2017 and later versions. For large-scale data processing, STRING_AGG outperforms FOR XML PATH with higher performance and cleaner syntax. Developers should prioritize STRING_AGG and avoid the outdated FOR XML PATH, while ensuring proper indexing to further optimize performance.
This article compares FOR XML PATH and STRING_AGG in SQL Server, highlighting STRING_AGG’s superior performance for string aggregation tasks.
SQLFlash is your AI-powered SQL Optimization Partner.
Based on AI models, we accurately identify SQL performance bottlenecks and optimize query performance, freeing you from the cumbersome SQL tuning process so you can fully focus on developing and implementing business logic.
Join us and experience the power of SQLFlash today!.