Hello Everyone,
In case of temp tabes, is it better to go for minimal number of temp tables with joins or go with many temp tabes. As a geneal rule, which is better, to split up tables or
use a single table with many joins.
For example consider the below code
i create 4 temp tables.
CREATE TABLE #ModifiedOrderKeyId(OrderKeyId INT)
CREATE TABLE #ModifiedRtid(Rtid INT)
CREATE TABLE #ModifiedOrderDetailId(OrderKeyId INT)
CREATE TABLE #ModifiedOrderFormId(OrderKeyId INT)
INSERT INTO #ModifiedOrderDetailId
select distinct OD.orderkeyid
from TBLORDERDETAIL OD(NOLOCK)
where OD.MODIFIEDDATETIME >@LastModifiedDate----@LastModifiedDate
INSERT INTO #ModifiedOrderFormId
select distinct OFV.orderkeyid
from TBLORDERFORMACTIVITYVALUE OFV(NOLOCK)
where OFV.MODIFIEDTIME >@LastModifiedDate--'2014-10-25 18:23:45.590'--
INSERT INTO #ModifiedOrderKeyId
select orderkeyid
from #ModifiedOrderDetailId md
union
select orderkeyid
from #ModifiedOrderFormId md
where not exists (select 1 from #ModifiedOrderDetailId mf where md.OrderKeyId=mf.OrderKeyId)
INSERT into #ModifiedRtid
select DISTINCT SS.RTID
from #ModifiedOrderKeyId MK
INNER JOIN TBLORDERDETAIL OD(NOLOCK) ON MK.OrderKeyId=OD.ORDERKEYID
INNER JOIN TBLSTOPSUMMARY SS(NOLOCK) ON OD.STOPID=SS.STOPID
after modification by using only a single temp table
INSERT into #ModifiedRtid
(select OD.orderkeyid
from TBLORDERDETAIL OD(NOLOCK)
where OD.MODIFIEDDATETIME >@LastModifiedDate
union
select OFV.orderkeyid
from TBLORDERFORMACTIVITYVALUE OFV(NOLOCK)
where OFV.MODIFIEDTIME >@LastModifiedDate) a
INNER JOIN TBLORDERDETAIL OD(NOLOCK) ON MK.OrderKeyId=OD.ORDERKEYID
INNER JOIN TBLSTOPSUMMARY SS(NOLOCK) ON OD.STOPID=SS.STOPID
Which is better the first one usng 4 temp tables or the second methd usiing a single temp table and what are the advantages/disadvantage of both the methids.
Regards
Regards