Posts

Showing posts from December, 2022

Stored Procedure Performance of Table Valued Parameters vs STRING_SPLIT

Image
As soon as I finished working with a bulk upsert stored procedure using table valued parameters , my brain jumped to "What about if it's just a list of string values? Would it be simpler to just use STRING_SPLIT?" OK, thanks brain, now I need to spend some time on that or I won't sleep well tonight. Setup was simple, created a stored procedure for each approach that takes the input of a DisplayName list, and queries the StackOverflow2010 database Users table for those records. I also added an index on the DisplayName to avoid a table scan. Test Query:  USE [StackOverflow2010] GO SET STATISTICS IO ON GO DECLARE @DisplayNamesString nvarchar(max) = 'Jeff Atwood,user262577,Alberto A. Medina'; DECLARE @DisplayNamesTVP [dbo].[StringSplitTestTVP]; INSERT INTO @DisplayNamesTVP VALUES  ('Jeff Atwood'), ('user262577'), ('Alberto A. Medina'); EXECUTE [dbo].[GetUsersByDisplayNameUsingStringSplit]     @DisplayNamesString EXECUTE [dbo].[GetUsersByDi...

SQL Server Bulk Upsert using Table Valued Parameters

Not much background to this post. Had an interest in improving a part of some work code that did multiple upsert operations, and came up with a bulk insert stored procedure using the pattern described by Aaron Bertand . Uploading an example that uses the StackOverflow2010 database and Users table. Performance is greatly improved by using this over individual upsert statements. Users table valued parameter:  USE [StackOverflow2010] GO /****** Object:  Table Valued Parameter [dbo].[UsersTVP] ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TYPE [dbo].[UsersTVP] AS TABLE ( [Id] [int] NULL, [AboutMe] [nvarchar](max) NULL, [Age] [int] NULL, [CreationDate] [datetime] NOT NULL DEFAULT GETDATE(), [DisplayName] [nvarchar](40) NOT NULL DEFAULT '', [DownVotes] [int] NOT NULL DEFAULT 0, [EmailHash] [nvarchar](40) NULL, [LastAccessDate] [datetime] NOT NULL DEFAULT GETDATE(), [Location] [nvarchar](100) NULL, [Reputation] [int] NOT NULL DEFAULT 1, [UpVot...