Bulk Updates and Inserts with OPENJSON and CTEs in SQL
Hello, OutSystems developers! If you’ve ever worked with applications handling large datasets, like importing Excel spreadsheets, you know that row-by-row operations can be a performance bottleneck. Endless loops, multiple database connections, and long execution times are common issues. In this article, we’ll explore how to boost performance in OutSystems applications using bulk inserts and bulk updates, leveraging SQL’s JSON functions to create temporary tables via CTEs (Common Table Expressions). I’ll walk through a practical example: creating or updating products from an Excel spreadsheet.
This approach drastically reduces database calls, making your applications faster and more scalable. Let’s dive in!
The Problem: Performance in Large-Scale Data Operations
In OutSystems, it’s common to use actions like CreateOrUpdate in loops to process lists of records. For instance, when importing data from an Excel file, you convert the file into a record list using ExcelToRecordList and iterate over each item. This works fine for small datasets, but for thousands of rows:
Sample
The result? Slow and inefficient applications. The solution lies in bulk operations: inserting or updating multiple records in a single SQL query.
The Solution: Bulk Operations with JSON and CTEs
OutSystems supports advanced SQL queries, allowing us to use functions like OPENJSON (available in SQL Server 2016+, compatible with many OutSystems environments) to parse JSON data into temporary tables. We then use CTEs to manipulate this data and perform bulk inserts or updates.
General Steps:
This happens in a single transaction per batch, minimizing overhead.
Performance Benefits
Practical Example: Creating/Updating Products from an Excel Spreadsheet
Imagine a scenario where users upload an Excel spreadsheet containing product data to create new products or update existing ones. Columns: Code (key), Name, Price, Stock. The provided workflow illustrates this process effectively.
Sample 2
Workflow Overview
SQL Query for Bulk Update
The BulkUpdate action uses:
SQL Query for Bulk Insert
The BulkInsert action uses:
Why Batching Matters
Processing records in batches (e.g., 1000 at a time) is essential for:
The workflow’s GotoSQL decision node ensures batches of 1000 records are processed efficiently.
Implementation Tips
Final Thoughts
Using bulk updates and inserts with JSON, CTEs, and batch processing transforms OutSystems applications from “functional” to “high-performance.” The provided workflow, with BulkInsert and BulkUpdate actions processing 1000 records per batch, exemplifies this efficiency for product imports. Try it in a test environment and see the difference!
Have variations or tips? Share them in the comments below.
Thanks for reading! 🚀
Its very nice of you for sharing this, I will try. thank you.
Thanks for sharing.
Very useful, thanks for sharing!!
Thanks for sharing. 💪
Looks very useful, thanks for the info.