This is support code for the lightning talk I gave for New England SQL Server group in May, 2023.
It compares performance of JSON and Table Variables as a means to pass tabular data to SQL Server stored procedures.
- VSCode
- SQL Database Projects extension
- Docker
- .NET 7.0 SDK
- In the VSCode side menu, locate Database Projects
- Right-click on the project
sql
, select "Publish" - Select "Publish to new SQL server local development container"
- Select the default port (1433)
- For the admin password, select
Password123
. If you choose another password, you will have to change it in theappsettings.json
as well. - Keep selecting default values
- The publish process will start and complete in several seconds.
- VSCode should show a toast message saying "Database project published successfully"
cd src
dotnet tool restore
Inside the src
folder:
# Measure Table Variable performance
./runTv.sh
# Measure JSON performance
./runJson.sh
- The test scripts start 8 parallel worker processes.
- Each worker process runs the stored procedures sequentially in a loop. At any given point in time 8 procedures are running concurrently (less negligible time spent on running C# code between invocations for populating parameters, parsing results and measuring time).
- Every 20 invocations of the stored procedure, a worker prints hash character (
#
) on the screen. - The test scripts run until stopped with a Ctrl-C.
- If it doesn't work, check
appsettings.json
. It assumes that you followed the steps in the section "Install the database" above.
Open PlainQuery.sql
and run it.
It takes a JSON array with two objects in it and convert it to a recordset with two records. Exactly how it should be.
Open ZalgoQuery.sql
and run it.
Oh no! This is not a valid JSON.
It starts as an array of two objects, but then it looks like Zalgo, the chthonic extradimensional being, is trying to creep into our world through a database query.
Despite the fact that it is not a valid JSON and doesn't parse, the query runs and returns the same two-record result as the previous one.
This is because OPENJSON
is working with the JSON data in a streaming fashion. The SELECT
clause of the Zalgo query has the TOP 2
modifier, which means that OPENJSON
will only need to retrieve two records from the underlying JSON.
Once it has retrieved the two records, it stops reading and discards Zalgo-infected portion of the text. Our world is safe.
Have fun!