Showing posts with label statements. Show all posts
Showing posts with label statements. Show all posts

Wednesday, March 28, 2012

Running UPDATE statements in parallel on the same table

Hi all,

Does an UPDATE statment lock the entire table or just the rows that will be affected by the UPDATE?

I ask because -

Can I run UPDATE statements in parallel on the same table on the same column. The need for doing this is because the table is a large fact table. I plan to execute the same UPDATE statements on different time sections of the data to expedite the processing.

If the UPDATE statment lock the entire table then I cannot run an UPDATE in parallel. If the UPDATE statement just locks the rows that will be affected then maybe I can because rows affected will be different for each UPDATE.

Let me know.

Thanks,

VivekIt all depends on the lock escalation

Imaging if you ad hundereds of current connections and the lock always ascended to table locks..that wouldn't be a very good RDBMS, now would it.

BOL has a good description of this if you want to read up on it.

Tuesday, February 21, 2012

Running SP versus SQL Statements

Folks:

I need some help with this. I have a stored procedure which if I run as :

exec test_sptma34 @.asofdate = '10/31/2007', @.analysisid = 'TVE-2004-10'

would NOT Complete at all and goes in a loop looks like (waited for 12 mins and then cancelled). Whereas If I run the same statements which are in SP then I get around 41 rows and that too within 1 to 2 seconds. Am I missing anything here?

STORED PROCEDURE:
==================
Code: ( sql )

    CREATE PROCEDURE test_spTMA34(@.asofdate DATETIME, @.analysisid VARCHAR(50))AS SET NOCOUNT ON BEGIN declare @.ShiftData TABLE([BP Shift] float, [Elasticity] float, [Convexity] float) INSERT INTO @.ShiftData ([BP Shift], Elasticity, Convexity)SELECT cast(substring(D1.datatype, 12, len(D1.datatype)-13) AS float) AS '[BP Shift]', SUM(S.Weight*D1.value) AS Elasticity, SUM(S.Weight*D2.Value) AS ConvexityFROM tblData D1JOIN tblData D2 ON D1.asofdate = D2.asofdate AND D1.ticker = D2.ticker AND RIGHT(D1.datatype,LEN(D1.DataType) - 10) = RIGHT(D2.datatype, LEN(D2.DataType) - 9)JOIN tblPortfolio P ON P.ticker = D1.tickerJOIN tblSBMIWeights S ON S.ticker = D1.tickerWHERE P.portfolio = 'SBMI-TBA' AND P.asofdate = (SELECT MAX(asofdate) FROM tblPortfolio WHERE portfolio = 'SBMI-TBA' AND asofdate <= @.asofdate)AND S.asofdate = (SELECT MAX(asofdate) FROM tblSBMIWeights WHERE asofdate <=@.asofdate)AND D1.datatype LIKE 'Elasticity@.%' AND D1.DataSource = @.analysisidAND D2.datatype LIKE 'Convexity@.%' AND D2.DataSource = @.analysisidAND D1.asofdate = @.asofdateGROUP BY D1.datatype, D2.datatype INSERT INTO @.ShiftData ([BP Shift], Elasticity, Convexity)SELECT 0 AS '[BP Shift]', SUM(S.Weight*D1.value) AS Elasticity, SUM(S.Weight*D2.Value) AS ConvexityFROM tblData D1JOIN tblData D2 ON D1.asofdate = D2.asofdate AND D1.ticker = D2.ticker AND RIGHT(D1.datatype,LEN(D1.DataType) - 10) = RIGHT(D2.datatype, LEN(D2.DataType) - 9)JOIN tblPortfolio P ON P.ticker = D1.tickerJOIN tblSBMIWeights S ON S.ticker = D1.tickerWHERE P.portfolio = 'SBMI-TBA' AND P.asofdate = (SELECT MAX(asofdate) FROM tblPortfolio WHERE portfolio = 'SBMI-TBA' AND asofdate <= @.asofdate)AND S.asofdate = (SELECT MAX(asofdate) FROM tblSBMIWeights WHERE asofdate <=@.asofdate)AND D1.datatype = 'Elasticity' AND D1.DataSource = @.analysisidAND D2.datatype = 'Convexity' AND D2.DataSource = @.analysisidAND D1.asofdate = @.asofdateGROUP BY D1.datatype, D2.datatype SELECT * FROM @.ShiftDataORDER BY [BP Shift] END

================================================== =======

SAME SQL STATEMENTS (this runs within 1 to 2 secs and returs around 41 rows which is right)
================================================== =====
Code: ( sql )

    SET NOCOUNT ONBEGINdeclare @.asofdate DATETIME, @.analysisid VARCHAR(50) SELECT @.asofdate = '10/31/2007'SELECT @.analysisid = 'TVE-2004-10' declare @.ShiftData TABLE([BP Shift] float, [Elasticity] float, [Convexity] float) INSERT INTO @.ShiftData ([BP Shift], Elasticity, Convexity)SELECT cast(substring(D1.datatype, 12, len(D1.datatype)-13) AS float) AS '[BP Shift]', SUM(S.Weight*D1.value) AS Elasticity, SUM(S.Weight*D2.Value) AS ConvexityFROM tblData D1JOIN tblData D2 ON D1.asofdate = D2.asofdate AND D1.ticker = D2.ticker AND RIGHT(D1.datatype,LEN(D1.DataType) - 10) = RIGHT(D2.datatype, LEN(D2.DataType) - 9)JOIN tblPortfolio P ON P.ticker = D1.tickerJOIN tblSBMIWeights S ON S.ticker = D1.tickerWHERE P.portfolio = 'SBMI-TBA' AND P.asofdate = (SELECT MAX(asofdate) FROM tblPortfolio WHERE portfolio = 'SBMI-TBA' AND asofdate <= @.asofdate)AND S.asofdate = (SELECT MAX(asofdate) FROM tblSBMIWeights WHERE asofdate <=@.asofdate)AND D1.datatype LIKE 'Elasticity@.%' AND D1.DataSource = @.analysisidAND D2.datatype LIKE 'Convexity@.%' AND D2.DataSource = @.analysisidAND D1.asofdate = @.asofdateGROUP BY D1.datatype, D2.datatype INSERT INTO @.ShiftData ([BP Shift], Elasticity, Convexity)SELECT 0 AS '[BP Shift]', SUM(S.Weight*D1.value) AS Elasticity, SUM(S.Weight*D2.Value) AS ConvexityFROM tblData D1JOIN tblData D2 ON D1.asofdate = D2.asofdate AND D1.ticker = D2.ticker AND RIGHT(D1.datatype,LEN(D1.DataType) - 10) = RIGHT(D2.datatype, LEN(D2.DataType) - 9)JOIN tblPortfolio P ON P.ticker = D1.tickerJOIN tblSBMIWeights S ON S.ticker = D1.tickerWHERE P.portfolio = 'SBMI-TBA' AND P.asofdate = (SELECT MAX(asofdate) FROM tblPortfolio WHERE portfolio = 'SBMI-TBA' AND asofdate <= @.asofdate)AND S.asofdate = (SELECT MAX(asofdate) FROM tblSBMIWeights WHERE asofdate <=@.asofdate)AND D1.datatype = 'Elasticity' AND D1.DataSource = @.analysisidAND D2.datatype = 'Convexity' AND D2.DataSource = @.analysisidAND D1.asofdate = @.asofdateGROUP BY D1.datatype, D2.datatype SELECT * FROM @.ShiftDataORDER BY [BP Shift]END

====================Question moved to SQL Server Forum.