sql server - Change table variable to temp table -
i have stored procedure written years ago. uses table variable. inside code it's doing:
declare @opportunityvs3 sf_opportunitymerge insert @tablevariable select * /*...*/
this inside job runs every 5 minutes. there used no issues in past because select
used collect around 10 1000 records… select trying insert million records.
given issue, think forced change @tablevariable
#temptable
. think? have other option?
at end of sp, it’s passing variable sp:
exec [dbo].[sf_mergeopportunity] @opportunityvs3, @lastupdatedate
i guess have write in same stored procedure, because temporary tables cannot passed, right?
below definition of @tablevariable
being used , code have change:
declare @opportunityvs3 sf_opportunitymerge insert @opportunityvs3 select opportunity_id, salesforce_id, accountid, age__c, -- (continues) merge [bu2].[dbo].[salesforce_opportunity] target using ( select opportunity_id, salesforce_id, accountid, age__c, amount, bill99amount__c, bj_marketing__c -- (continues) @opportunityvs3 ) source on (target.opportunity_id = source.opportunity_id) exec [dbo].[sf_mergeopportunity] @opportunityvs3, @lastupdatedate
[![enter image description here][1]][1]
[![enter image description here][2]][2]
what can try without changing way work:
- force compiler take cardinalities of
table
variables account specifyingoption(recompile)
in queries usingtable
variables - supply suitable
unique index
orprimary key
table
variable
a downside of table
variables there no statistics associated them. sql compiler produce better execution plans when can take statistics account. therefore can improve performance considerably when switch temporary tables because these do have statistics.
when switch temporary tables, no longer pass table
variable stored procedure anymore. stored procedure written using temporary table knows exists beforehand - ie created before stored procedure executed.
you write stored procedure though temporary table ordinary table. when write stored procedure using temporary table, sql server management studio underline parts in red appears though there errors in stored procedure. if syntax correct, creating/altering stored procedure work fine.
read excellent essay on differences between table
variables , temporary tables, see how usage may impact performance. topics no column statistics , indexes.
Comments
Post a Comment