SQL Server 2022: Improved Performance for String Splitting and Parsing

In SQL Server 2022, Microsoft has introduced significant improvements in string splitting and parsing capabilities, making data manipulation more efficient. This blog explores these enhancements, providing practical examples using the JBDB database, and highlights a business use case to demonstrate the impact of these features.


📊 Business Use Case: Streamlining Data Analysis

Scenario:

A retail company, “TechShop,” collects customer feedback via online surveys. The responses are stored in a SQL Server database, and each response includes a comma-separated list of keywords describing the customer’s experience. The company wants to analyze these keywords to identify trends and improve its services.

Challenge:

With the previous SQL Server versions, splitting these comma-separated strings into individual keywords for analysis was resource-intensive and time-consuming, especially with large datasets. The goal is to leverage SQL Server 2022’s improved string splitting and parsing features to streamline this process.

🛠️ Key Features and Enhancements

1. STRING_SPLIT with Ordering Support

SQL Server 2022 introduces ordering support for the STRING_SPLIT function, allowing users to retain the order of elements in the original string. This enhancement is crucial for analyses where the sequence of data is significant.

2. Improved Performance

The performance of string splitting operations has been optimized, reducing execution time and resource consumption. This is particularly beneficial for large-scale data processing.

3. Enhanced Parsing Functions

Enhanced parsing functions provide more robust error handling and compatibility with different data types, improving data quality and reducing manual data cleaning efforts.

🧩 Example Demonstration with JBDB Database

Let’s dive into some examples using the JBDB database to showcase these improvements.

Setting Up the JBDB Database

First, we’ll set up a table to store customer feedback:

CREATE TABLE CustomerFeedback (
    FeedbackID INT IDENTITY(1,1) PRIMARY KEY,
    FeedbackText NVARCHAR(MAX)
);

INSERT INTO CustomerFeedback (FeedbackText)
VALUES
('Great service, fast shipping, quality products'),
('Slow delivery, excellent customer support'),
('Fantastic prices, will shop again, good variety'),
('Quality products, quick response time, friendly staff');

CREATE TABLE LargeCustomerFeedback (
    FeedbackID INT IDENTITY(1,1) PRIMARY KEY,
    FeedbackText NVARCHAR(MAX)
);

INSERT INTO LargeCustomerFeedback (FeedbackText)
VALUES
('Great service, fast shipping, quality products'),
('Slow delivery, excellent customer support'),
('Fantastic prices, will shop again, good variety'),
('Quality products, quick response time, friendly staff')
,('Great service1, fast shipping1, quality products1'),
('Slow delivery1, excellent customer support1'),
('Fantastic prices1, will shop again1, good variety1'),
('Quality products1, quick response time1, friendly staff1')
,('Great service2, fast shipping2, quality products2'),
('Slow delivery2, excellent customer support2'),
('Fantastic prices2, will shop again2, good variety2'),
('Quality products2, quick response time2, friendly staff2')
,('Great service3, fast shipping3, quality products3'),
('Slow delivery3, excellent customer support3'),
('Fantastic prices3, will shop again3, good variety3'),
('Quality products3, quick response time3, friendly staff3');

Using STRING_SPLIT with Ordering Support

Previously, STRING_SPLIT did not guarantee the order of elements. In SQL Server 2022, you can specify the order of elements:

SELECT 
    FeedbackID,
    value AS Keyword
FROM 
    CustomerFeedback
    CROSS APPLY STRING_SPLIT(FeedbackText, ',', 1)
ORDER BY 
    FeedbackID, ordinal;

In this query:

  • FeedbackText is split into individual keywords.
  • The ordinal column (optional) provides the order of elements as they appear in the original string.

Improved Performance Demonstration

To demonstrate the performance improvements, let’s compare the execution times for splitting a large dataset in SQL Server 2022 vs. a previous version. For simplicity, assume we have a LargeCustomerFeedback table similar to CustomerFeedback but with millions of rows.

Example Query for Large Dataset

SELECT 
    FeedbackID,
    value AS Keyword
FROM 
    LargeCustomerFeedback
    CROSS APPLY STRING_SPLIT(FeedbackText, ',', 1)
ORDER BY 
    FeedbackID, ordinal;

In practice, SQL Server 2022 processes this operation significantly faster, showcasing its enhanced string handling capabilities.

Counting Keywords from Feedback

To analyze the frequency of keywords mentioned in customer feedback, you can use the following query:

SELECT 
    value AS Keyword,
    COUNT(*) AS Frequency
FROM 
    CustomerFeedback
    CROSS APPLY STRING_SPLIT(FeedbackText, ',', 1)
GROUP BY 
    value
ORDER BY 
    Frequency DESC;

This query splits the feedback text into keywords and counts their occurrences, helping identify common themes or issues mentioned by customers.

Filtering Feedback Containing Specific Keywords

If you want to filter feedback entries containing specific keywords, such as “quality,” you can use:

SELECT 
    FeedbackID,
    FeedbackText
FROM 
    CustomerFeedback
WHERE 
    EXISTS (
        SELECT 1
        FROM STRING_SPLIT(FeedbackText, ',', 1)
        WHERE value = 'quality'
    );

This query finds feedback entries that mention “quality,” allowing the analysis of customer sentiments regarding product quality.

Extracting Unique Keywords

To extract unique keywords from all feedback entries, use the following query:

SELECT DISTINCT 
    value AS UniqueKeyword
FROM 
    CustomerFeedback
    CROSS APPLY STRING_SPLIT(FeedbackText, ',', 1);

This query provides a list of all unique keywords, helping identify the range of topics covered in customer feedback.

📈 Business Impact

By leveraging SQL Server 2022’s improved string splitting and parsing features, TechShop can:

  1. Accelerate Data Processing: The company can quickly analyze large volumes of customer feedback, allowing for timely insights into customer sentiment and trends.
  2. Improve Data Accuracy: The new features reduce the need for manual data cleaning and error handling, ensuring more accurate analysis.
  3. Enhance Customer Experience: By understanding customer feedback more efficiently, TechShop can make informed decisions to improve its services, leading to higher customer satisfaction and retention.

🎉 Conclusion

SQL Server 2022’s advancements in string splitting and parsing offer substantial benefits for data-driven businesses. The enhancements in performance, ordering support, and robust error handling make it easier and faster to analyze complex datasets. For companies like TechShop, these features enable better customer insights and more agile decision-making.

💡 Tip: Always test these features with your specific data and workload to fully understand the performance benefits and implementation considerations.

For more tutorials and tips on  SQL Server, including performance tuning and  database management, be sure to check out our JBSWiki YouTube channel.

Thank You,
Vivek Janakiraman

Disclaimer:
The views expressed on this blog are mine alone and do not reflect the views of my company or anyone else. All postings on this blog are provided “AS IS” with no warranties, and confers no rights.

Leave a Reply