Solved

SQL Table update running to long

Posted on 2014-01-31
9
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 40

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 143

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 143

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:
Scott Pletcher 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:Scott Pletcher
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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

695 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