Freelancers Network
 
skill list top cap
Homepage
Join the Freelancer's Network
Update your details
Find a freelancer
Post a project
Find a project
Projects Archive
Post a job
Find a job
Jobs Archive
See Dan's Pages
See Andy's Pages
Link to this site
Resources
Join/Leave Forum
Forum Messages
+Additions+ Adverts
Advertising
Contact Us
Subscribe to our newsletter - enter your email address and hit return
Freelancers.net is owned and operated by Andy Stowell and Dan Winchester
skill list end cap
guru web hostcom

Find me again on Freelancers.net

FN-FORUM: SQL Server Performance Problem

date posted 11th January 2006 09:39

Hi,
Got a SQL problem - a performance problem to be exact;

This is the Stored Procedure;




CREATE PROCEDURE dbo.System_Importer_UpsertRecordProperty

@regid int, @userid int, @key varchar(250), @userpropertyid [EMAIL REMOVED]
varchar(250),=20
@msg varchar(8000) OUTPUT, @added int OUTPUT, @updated int
[EMAIL REMOVED] int, @importlogitemid int

AS

set nocount on
set dateformat dmy

SELECT @msg =3D [EMAIL REMOVED] ''), @added=3D [EMAIL REMOVED] 0), @updated =
=3D
[EMAIL REMOVED] 0)

-- find the item
IF @userpropertyid is null
BEGIN
SELECT @userpropertyid =3D id
FROM
registration.dbo.userproperty
WHERE
[key] =3D @key
AND reg_id =3D @regid
AND deleted=3D'N'

SELECT @userpropertyid =3D [EMAIL REMOVED] 0)

IF @userpropertyid =3D 0
BEGIN
SELECT @msg =3D @msg + ', Key (' + @key +
') could not be found', @added =3D 0, @updated =3D 0
RETURN
END
-- END IF



END
-- END IF




-- ok, attempt to update record

DECLARE @oldvalue varchar(8000)
DECLARE @pendingupdate char(1)

SELECT @oldvalue =3D Datavalue, @pendingupdate =3D PendingUpdate
FROM
registration.dbo.userpropertydata
WHERE reg_id =3D @regid AND user_id =3D @userid AND userproperty_id
=3D @userpropertyid


-- IF THE VALUE HAS CHANGED OR OLD ITEM MARKED AS PENDING THEN UPDATE IT

IF [EMAIL REMOVED] @value) OR [EMAIL REMOVED] =3D @value AND @pendingupdate =3D
'Y') OR @oldvalue is null
BEGIN

UPDATE registration.dbo.userpropertydata
SET DataValue =3D @value, modifiedon =3D getdate(),
modifiedby_id =3D 0, pendingupdate=3D'N'
WHERE reg_id =3D @regid AND user_id =3D @userid AND
userproperty_id =3D @userpropertyid
=09
IF @@ROWCOUNT =3D 0
BEGIN
-- Update failed, so insert
INSERT INTO
registration.dbo.userpropertydata
(reg_id, user_id,
userproperty_id, datavalue, modifiedby_id, modifiedon, pendingupdate)
VALUES [EMAIL REMOVED] @userid,
@userpropertyid, @value, 0, getdate() , 'N')
=09
IF @@ROWCOUNT =3D 1
BEGIN
SELECT @added =3D @added +
1, @msg =3D @msg+', User property (' + @key + ') created'
RETURN
END
ELSE
BEGIN
SELECT @msg =3D @msg + ',
Problem creating new user property (' + @key + ')'
RETURN
END
-- END IF
END
ELSE
BEGIN
-- record updated
SELECT @updated =3D @updated + 1, @msg =3D
@msg + ', User property (' + @key + ') updated'
RETURN
END
-- END IF
END
ELSE
BEGIN
-- Data hasn't changed
END
-- END IF

set nocount off
GO




Starting from "no data" in the userpropertdata table, running the SP
takes 20-30ms

We have 2250 master records each with 59 of these userpropertydata
values meaning 132750 records. By the time we're towards the end of the
import, the SP is taking 300-400ms to run.

Now, the userpropertydata table has an index on it against reg_id,
user_id, userproperty_id. It's not set as Unique or clustered (should it
be set as unique ? I guess it can be if necessary)

There are no other relationships (the only other index being the primary
key against a field called UID which is an identity(1,1) field).


I suspect there's very little that can be done to make this go faster -
it's the old - have an index to make the initial lookup / update go
faster or don't have an index to make the insert quicker.

Suggestions on a post card !

I can't really be going live with a system that takes about 8.5 hours to
import the data.

Cheers
Andy







Messages by Day
January 31st 2006
January 30th 2006
January 29th 2006
January 28th 2006
January 27th 2006
January 26th 2006
January 25th 2006
January 24th 2006
January 23rd 2006
January 22nd 2006
January 21st 2006
January 20th 2006
January 19th 2006
January 18th 2006
January 17th 2006
January 16th 2006
January 15th 2006
January 14th 2006
January 13th 2006
January 12th 2006
January 11th 2006
January 10th 2006
January 9th 2006
January 8th 2006
January 7th 2006
January 6th 2006
January 5th 2006
January 4th 2006
January 3rd 2006
January 2nd 2006
January 1st 2006


Messages by Month
December 2006
November 2006
October 2006
September 2006
August 2006
July 2006
June 2006
May 2006
April 2006
March 2006
February 2006
January 2006


Messages by Year
2008
2007
2006
2005
2004
2003
2002
2001
2000