Sunday, 16 July 2017

Execution plan for insert statement in sql server

INSERT statements are always against a single table. This would lead us to believe that execution plans will be equally simplistic. However, in order to take into account rollbacks and data integrity checks, execution plans for insert queries can be quite complicated.
See the example.
I am wring simple insert statement
USE [Test]
GO
INSERT INTO [dbo].[EMP]
           ([Name]
           ,[empAdd]
           ,[Mobile])
     VALUES
           ('Bagesh kumar Singh'
           ,'Pune'
           ,'88888024XX')
GO

See the execution plan

Click on the icon we will get the execution plan


The physical operation of the execution plan starts off, reading right to left, with an operator that is new to us: Constant Scan. This operator introduces a constant number of rows into a query. In our case, it's building a row in order for the next two operators to have a place to add their output. The first of these is a Compute Scalar operator to call a function called get identity. This is the point within the query plan when SQL Server generates an identity value, for the data to follow. Note that this is the first operation within the plan, which helps explain why, when an INSERT fails, you get a gap in the identity values for a table.


No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts