You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the table has any IDENTITY column, OUTPUT isn't used at all.
Benchmark code
BenchmarkRunner.Run<SequenceBenchmark>();publicclassSequenceBenchmark{conststringConnectionString="Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false";privateSqlConnection_connection;[GlobalSetup]publicasyncTaskSetup(){_connection=newSqlConnection(ConnectionString);await_connection.OpenAsync();awaitusingvarcmd=newSqlCommand(@"DROP TABLE IF EXISTS [Foo];DROP SEQUENCE IF EXISTS [FooSeq];CREATE SEQUENCE [FooSeq] AS int START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE NO CYCLE;CREATE TABLE [Foo] ( [Id] int PRIMARY KEY NOT NULL DEFAULT (NEXT VALUE FOR FooSeq), [BAR] int);",_connection);awaitcmd.ExecuteNonQueryAsync();}[Benchmark]publicasyncTaskNoOutput(){awaitusingvarcmd=newSqlCommand("INSERT INTO [Foo] ([Bar]) VALUES (8)",_connection);_=awaitcmd.ExecuteScalarAsync();}[Benchmark]publicasyncTaskOutput(){awaitusingvarcmd=newSqlCommand("INSERT INTO [Foo] ([Bar]) OUTPUT INSERTED.[Id] VALUES (8)",_connection);_=awaitcmd.ExecuteScalarAsync();}[Benchmark(Baseline=true)]publicasyncTaskOutputInto(){awaitusingvarcmd=newSqlCommand(@"DECLARE @inserted TABLE ([Id] int);INSERT INTO [Foo] ([Bar]) OUTPUT INSERTED.[Id] INTO @inserted VALUES (8);SELECT [i].[Id] FROM @inserted i;",_connection);_=awaitcmd.ExecuteScalarAsync();}[GlobalCleanup]publicValueTaskCleanup()=>_connection.DisposeAsync();}
When inserting an entity with database-generated columns, we currently generate the following (as long as there's no IDENTITY column):
This could be simplified into this:
The roundabout through the
inserted0TVP is probably because the OUTPUT clause won't work if there's a trigger defined, unless it's anOUTPUT INTO(??) (@AndriySvyryd it this right, any more context?).Unfortunately, using
OUTPUT INTOinstead ofOUTPUTadds a lot of overhead:That's over 3ms just for passing through a TVP! Also, mysteriously the version with no OUTPUT clause at all performs worse...
Remarks:
OUTPUT, and switch back toOUTPUT INTOif the user tells us the table has triggers (via metadata).Benchmark code