Showing posts with label efficient. Show all posts
Showing posts with label efficient. Show all posts

Sunday, February 19, 2012

Concurrent SQL Tranasactions Fail

Hi all,
I am having trouble trying to get an efficient way of checking if a
value pair exists in a table, and if not inserting the value pair and
returning the new id in the identity column. The old method I have used
has been fine for some time but the table has grown to a very large
size and the current code is causing a bottleneck.
The table consists the three columns, An identity column and two
nvarchar(15) columns. Instead of replicating the nvarchar(15) columns
over the database I have placed them in a lookup table and use the ID
from the identity column to reference the nvarchar values. I do store
multiple languages in the database so an nvarchar is necessary.
I admit the current code I use below is poor, but all of the
enhancements I have tried to make have resulted in the system failing,
as multiple copies of the procedure run and two (or more) procedures
try and insert the same data into the table (there is a unique
clustered index on the two varchar columns, and the identity column is
the primary key).
As our server application is multithreaded, it is highly likely
(certain) that two or more of these procedures will be running at once
so I have to ensure data integrity whilst checking and updating. Im
just not sure that a TABLOCKX,HOLDLOCK lock hint is the best way.
CREATE PROCEDURE dbo.CreateNewContextItem
@.CONTEXT NVARCHAR(15),
@.DATAITEM NVARCHAR(15),
@.ID INTEGER OUTPUT
AS
SET NOCOUNT ON
BEGIN TRANSACTION
SET @.ID = (SELECT id FROM contextitem WITH (TABLOCKX,HOLDLOCK) WHERE
context = @.CONTEXT AND dataitem=@.DATAITEM)
IF @.ID IS NULL
BEGIN
INSERT INTO
contextitem (context,dataitem)
VALUES
(@.CONTEXT,@.DATAITEM)
IF @.@.ERROR<>0
GOTO Errhandler
SET @.ID = Scope_Identity()
IF @.@.ERROR<>0
GOTO Errhandler
END
COMMIT TRANSACTION
RETURN 0
Errhandler:
ROLLBACK TRANSACTION
RETURN 50063 -- application specific error code
GO
Is there a way of speeding up this procedure whilst still allowing
multiple copies of it to be run at once safely? Any help would be
greatly appreciated.
Thanks in advance.How about UPDLOCK instead of the TABLOCKX hint?
ML
http://milambda.blogspot.com/|||That seems to have worked a treat!
Thanks for your help ML

Friday, February 10, 2012

Concatenating a field while grouping records

All,

Given multiple records with identical values in all fields except a
single varchar field, is there an efficient query that will group the
records into a single record and concatenate the aforementioned
varchar field into a single field with each of the source records'
values separated by commas?

Example:
Record 1 'Doug' , '1'
Record 2 'Doug' , '2'

Output record 'Doug' , '1,2'

Thanks in advance,
DougSELECT col1,
MIN(CASE seq WHEN 1 THEN col2 END)+
COALESCE(', '+MIN(CASE seq WHEN 2 THEN col2 END),'')+
COALESCE(', '+MIN(CASE seq WHEN 3 THEN col2 END),'')+
COALESCE(', '+MIN(CASE seq WHEN 4 THEN col2 END),'')+
COALESCE(', '+MIN(CASE seq WHEN 5 THEN col2 END),'')
FROM
(SELECT S1.col1, S2.col2, COUNT(*) AS seq
FROM Sometable AS S1
JOIN Sometable AS S2
ON S1.col1 = S2.col1
AND S1.col2 <= S2.col2
GROUP BY S1.col1, S2.col2) AS X
GROUP BY col1

--
David Portas
----
Please reply only to the newsgroup
--|||This is trivial with the RAC utility for S2k.
No cursors,no complicated code and no hassles.

More info @.
http://www.rac4sql.net/onlinehelp.asp?topic=236

RAC v2.2 and QALite released.
www.rac4sql.net

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!