Solved

SQL Table update running to long

Posted on 2014-01-31
9
264 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

867 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

18 Experts available now in Live!

Get 1:1 Help Now