MikeBronner
Programmer
Hey Everyone!
I have a script that I am using to clean up some legacy data. It runs, and functions exactly like it should. However, there is one problem: it takes much too long to run.
Here's the scenario:
- I have 100,000 records that need to be processed.
- Currently it processes about 10,000 in the first hour, but it slows down more and more the farther it gets along.
- The main query (the one for the cursor) takes only ~ 4 seconds to complete when running by itself.
I've posted the code below, and was wondering if there is something obvious that I have missed regarding optimizing for performance?
Thanks
Take Care,
Mike
I have a script that I am using to clean up some legacy data. It runs, and functions exactly like it should. However, there is one problem: it takes much too long to run.
Here's the scenario:
- I have 100,000 records that need to be processed.
- Currently it processes about 10,000 in the first hour, but it slows down more and more the farther it gets along.
- The main query (the one for the cursor) takes only ~ 4 seconds to complete when running by itself.
I've posted the code below, and was wondering if there is something obvious that I have missed regarding optimizing for performance?
Thanks
Code:
BEGIN
DECLARE @s_account_no VARCHAR(20)
,@s_company_name VARCHAR(255)
,@s_tax_id VARCHAR(30)
,@s_web_url VARCHAR(255)
,@n_original_account_id BIGINT
,@n_existing_company_id BIGINT
,@s_company_reference VARCHAR(255)
DECLARE c_list CURSOR FOR
SELECT c1.accountno
,LTRIM(RTRIM(c1.company)) AS company
,LTRIM(RTRIM(cs_tax.contsupref)) AS tax_id
,CASE
WHEN dbo.regex_match('^(http[s]?://)?([a-zA-Z0-9]\.)?([a-zA-Z0-9]){1}([a-zA-Z0-9\-\.])+(\.[a-zA-Z]+){1}(/([a-zA-Z0-9\-\_])*)*(\.([a-zA-Z0-9\_]+))?$', LTRIM(RTRIM(cs_web.contsupref))) = 1 THEN
LTRIM(RTRIM(cs_web.contsupref))
ELSE
NULL
END AS web_url
,dbo.strip_non_textual(LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(c1.company, 'ltd', ''), 'llc', ''), 'inc', ''), ' ', '')))) AS company_name_reference
FROM goldmine_test..contact1 AS c1
LEFT OUTER JOIN goldmine_test..contsupp AS cs_tax ON c1.accountno = cs_tax.accountno
AND (cs_tax.contact = 'Resale Tax ID'
OR cs_tax.contact = 'Tax ID#')
LEFT OUTER JOIN goldmine_test..contsupp AS cs_web ON c1.accountno = cs_web.accountno AND cs_web.contact = 'Web Site'
WHERE LEN(ISNULL(c1.company, '')) > 0
OPEN c_list
FETCH NEXT FROM c_list
INTO @s_account_no
,@s_company_name
,@s_tax_id
,@s_web_url
,@s_company_reference
WHILE (@@FETCH_STATUS = 0)
BEGIN
SELECT @n_existing_company_id = NULL
SELECT @n_existing_company_id = id
FROM gm_companies
WHERE dbo.strip_non_textual(LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(name, 'ltd', ''), 'llc', ''), 'inc', ''), ' ', '')))) = @s_company_reference
IF (ISNULL(@n_existing_company_id, -1) < 1)
BEGIN
INSERT INTO gm_companies
(
name
,tax_id
,web_url
)
VALUES
(
@s_company_name
,@s_tax_id
,@s_web_url
)
SELECT @n_existing_company_id = SCOPE_IDENTITY()
END
INSERT INTO gm_original_accounts
(
gm_id
,company_id
)
VALUES
(
@s_account_no
,@n_existing_company_id
)
FETCH NEXT FROM c_list
INTO @s_account_no
,@s_company_name
,@s_tax_id
,@s_web_url
,@s_company_reference
END
CLOSE c_list
DEALLOCATE c_list
END
GO
Take Care,
Mike