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
Solved

SQL Table update running to long

Posted on 2014-01-31
9
268 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 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

860 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