Insert Values VS Insert Select SQL

Hello

Is Insert Values Faster than Insert Select?

I have a Procedure that inserts the same values 3 times into tables.
Should I use Insert Select 3 times or select the values into local variables then use insert values 3 times?

Which would be Faster?

Also if the Insert Select has one or more inner joins does that have a further negative impact on the speed?
(I'm using SQL 2014)
p-platerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
since you have joins, it would be better that you do your own benchmark. there are so many factors that could affect performance and that might vary from one environment to the other. may be you can consider the output clause: https://msdn.microsoft.com/en-us/library/ms177564.aspx
0
p-platerAuthor Commented:
Hello Eric

I don't have joins in the current situation (I was just asking that for future.)

I thought there would be a general rule which should be faster (Local variables or insert select with multiple inserts)
0
Éric MoreauSenior .Net ConsultantCommented:
the real answers is: it depends. there is not a single answer valid for all circumstances.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Vitor MontalvãoMSSQL Senior EngineerCommented:
Should I use Insert Select 3 times or select the values into local variables then use insert values 3 times?
If the SELECT is the same you won't win nothing by storing the values into variables. Is just an extra step that won't help.

If you can give us an example for these two options we may give you a more assertive answer.
0
p-platerAuthor Commented:
Here is an example

Using Insert Select (Data from table 6 is selected 3 times)
insert table1(col1,col2,col3,col4)
select 'R',col6,col7,col15
from table6 where id = 10

insert table2(col1,col2,col3,col4)
select 'T',col6,col7,col15
from table6 where id = 10

insert table3(col1,col2,col3,col4)
select 'L',col6,col7,col15
from table6 where id = 10

Open in new window


Using insert values and Variables (Date from table 6 is selected once)
declare @val1 as varchar(10)
declare @val2 as varchar(10)
declare @val3 as varchar(10)

select @val1 = col6,@val2 = col7, @val3 = col15
from table6 where id = 10

insert table1(col1,col2,col3,col4)
values('R',@val1,@val2,@val3)

insert table2(col1,col2,col3,col4)
values('R',@val1,@val2,@val3)

insert table3(col1,col2,col3,col4)
values('R',@val1,@val2,@val3)

Open in new window

0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Ok, with the sample is more easy to give you a better solution. For this particular case I'ld use variables like this:
declare @val1 as varchar(10)
declare @val2 as varchar(10)
declare @val3 as varchar(10)

select @val1 = col6,@val2 = col7, @val3 = col15
from table6 where id = 10

insert table1(col1,col2,col3,col4)
values ('R',@val1,@val2,@val3),
        ('T',@val1,@val2,@val3),
        ('L',@val1,@val2,@val3)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
You're much better off using SELECT to provide the values if they are coming from an existing table.

Thus, for this specific case, I think this is the best approach:


insert table1(col1,col2,col3,col4)
select col1_value,t6.col6,t6.col7,t6.col15
from table6 t6
cross join (
    select 'R' as col1_value union all
    select 'T' union all
    select 'L'
) as col1_values
where t6.id = 10
0
p-platerAuthor Commented:
Thanks
Top Advice

Much Appreciated
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.