HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

Nonclustered Index Insert

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
indexinsertnonclustered

Problem

Say I have a table like this:

create table SomeTable
(
    id int identity(1, 1) not null primary key clustered,
    SomeString1 varchar(50) not null,
    SomeString2 varchar(50) not null
)
go

create nonclustered index IX_SomeString1
on SomeTable(SomeString1)
go


If I was to do this:

insert into SomeTable(SomeString1, SomeString2)
values('foo', 'bar')
go


And view the actual execution plan, I only see a Clustered Index Insert. Why am I not seeing a Nonclustered Index Insert in the execution plan?

Solution

For a single row insert you get a narrow/per-row plan

INSERT INTO SomeTable(SomeString1, SomeString2)
SELECT TOP 1 type, type
FROM master..spt_values


If you select the Clustered Index Insert Operator and View the properties window you can see the same information as shown in the XML.

If you try for 1,000 rows

INSERT INTO SomeTable(SomeString1, SomeString2)
SELECT TOP 1000 type, type
FROM master..spt_values


You get a different wide/per-index plan with the operations split out separately

See Wide vs. Narrow Plans or Craig Freedman's blog for more information about the two

Code Snippets

INSERT INTO SomeTable(SomeString1, SomeString2)
SELECT TOP 1 type, type
FROM master..spt_values
INSERT INTO SomeTable(SomeString1, SomeString2)
SELECT TOP 1000 type, type
FROM master..spt_values

Context

StackExchange Database Administrators Q#14720, answer score: 11

Revisions (0)

No revisions yet.