Most Optimized Way to Update a Table using STORED PROCEDURE?

Experts,

Which is the most optimized way to update a table using a STORED PROCEDURE?


Like this?:
----------
update a
set col1 = b.col1
from table1 a
inner join --SUBQUERY TABLE
      (select id, desc
            from table3) b on a.id = b.id




OR like this:
-------------
create table #Temp1 (id int, desc varchar(25))
insert into #Temp1
(select id, desc
      from table3)

update a
set col1 = b.col1
from table1 a
inner join #Temp1 b on a.id = b.id
LVL 17
MIKESoftware Solutions ConsultantAsked:
Who is Participating?
 
Scott PletcherSenior DBACommented:
Just join directly.  Loading the temp table just wastes time if there's already a covering index.
0
 
zephyr_hex (Megan)DeveloperCommented:
It depends on factors like if / how columns are indexed, etc.

Why don't you pull up the query plan yourself ?
0
 
Scott PletcherSenior DBACommented:
Because "desc" isn't being used in the query, you can remove it from the query.

Just make sure you can index keyed on id on table3.  Include the "desc" column in the index if it is actually being used in the query.
0
 
MIKESoftware Solutions ConsultantAuthor Commented:
Assume Indexed on Col1 "ID".

I'm seeking more of a "generally speaking" type of feedback.

Is it generally speaking better to have the stored procedure process the update using a "SUB-QUERY" as an inline table...

OR

Use the query to load a TEMP TABLE and use the TEMP TABLE to link to in an UPDATE STATEMENT.....?
0
 
MIKESoftware Solutions ConsultantAuthor Commented:
thanks for all the input
0
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.

All Courses

From novice to tech pro — start learning today.