Solved

SQL Table update running to long

Posted on 2014-01-31
9
261 Views
Last Modified: 2014-02-03
Below SQL code in a stored procedure is running between 7-8 minutes to complete.
The gpcust table has 81,000 records.

Is there a better way of doing so it does not run so long ?


--update gpcust table with latest lastpay date by custno/collector
      UPDATE [dbo].[tbl_rmcore_gpcust]
      SET    [dbo].[tbl_rmcore_gpcust].lastpayacct = COALESCE((SELECT Max(o.lastpay)
                                                               FROM   [dbo].[tbl_rmcore_gpcust] o
                                                               WHERE  Substring(o.custno, 2, 6) = Substring([dbo].[tbl_rmcore_gpcust].custno, 2, 6)
                                                                      AND o.collector = [dbo].[tbl_rmcore_gpcust].collector), [dbo].[tbl_rmcore_gpcust].lastpayacct)
0
Comment
Question by:thayduck
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 39

Expert Comment

by:lcohan
ID: 39824715
Unless you can get rid of the functions used in the WHERE clause even if you have the exact matching indexes for the WHERE clause in my opinion is not much you can do.
Can you check the query plan in SSMS and try to set OPTION (MAXDOP=1) if parallelism is there?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39824746
if at least there is a index on both tables for the collector field, it might help, unless it doesn't have lots of different values. if the values in that column are mainly the same values for most rows, it won't help.

you may try alternative syntax of the update with join from here:
http://www.experts-exchange.com/Database/Miscellaneous/A_1517-UPDATES-with-JOIN-for-everybody.html
but I doubt it will really help.

do you have some primary key on the table(s)?
can you use temporary tables?

my suggestion would then be to extract the data from the 2 tables to temporary tables, but only the columns which are PK, the collector and the substring() expression value.
creating indexes as needed, and based on that data, do the "join" on these tables, and update back to the table you want to update.
0
 

Author Comment

by:thayduck
ID: 39824748
Not sure how to check query plan or how to change MAXDOP=1.

What is parallelism ?
0
 
LVL 10

Expert Comment

by:PadawanDBA
ID: 39824767
I'd like to play with this a bit, but it would be immensely helpful if you could give the schema and some sample data.

Edit: i'm thinking of it from a direction very similar to what Guy suggested, but there are a few different angles that could be fun to play around with
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39824811
to use MAXDOP = 1, just add this at the end of your query:
OPTION(MAXDOP=1)

Open in new window

0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 200 total points
ID: 39825597
UPDATE gpcust
SET lastpayacct = gpcust_max.max_lastpay
FROM [dbo].[tbl_rmcore_gpcust] gpcust
INNER JOIN (                                
    SELECT Substring(custno, 2, 6) AS custno_2_6, collector, Max(lastpay) AS max_lastpay
    FROM [dbo].[tbl_rmcore_gpcust]
    GROUP BY Substring(custno, 2, 6), collector
    HAVING COUNT(*) > 1 --if there is only one matching row, the max value must be that one row, so it doesn't need UPDATEd
) AS gpcust_max ON
    gpcust_max.custno_2_6 = Substring(gpcust.custno, 2, 6) AND
    gpcust_max.collector = gpcust.collector
0
 

Author Comment

by:thayduck
ID: 39826642
Worked great. Job ran in seconds instead of minutes.
I cannot believe how much faster it runs.

I made small change. Since I do want to update record if only 1 match is found, I removed:
 
 HAVING COUNT(*) > 1 --if there is only one matching row, the max value must be that one row, so it doesn't need UPDATEd


Thanks.....
0
 

Author Closing Comment

by:thayduck
ID: 39826646
Thanks....
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 39829117
D'OH, yeah, different columns, it would still need updated.

I suspect the original code was doing a table scan for every update, while my code should avoid that, and just do two scans of the table.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now