Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7859491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:55:12+00:00 2026-06-02T21:55:12+00:00

This seems to be taking a very long time on large data sets. Will

  • 0

This seems to be taking a very long time on large data sets. Will combining the first and last 3 queries into 1 make it faster? Does anyone have any input on what might make it faster? I appreciate it.

update "detail" set bal = (units * amount) where code_type = 'AB'; 
update "detail" set bal = (units * amount) where code_type = 'CD';
update "detail" set bal = (units * amount) where code_type = 'EF';
update "detail" set bal = (units * amount * -1) where code_type = 'GH';
update "detail" set bal = (units * amount * -1) where code_type = 'IK';
update "detail" set bal = (units * amount * -1) where code_type = 'LM';
update "detail" set bal = 0 where code_type = 'NO';

Additionally –

update bill set pay = 
  (select round(sum(bd1.bal),2) from "detail" bd1 where 
  bd1.inv = bill.inv and 
  (bd1.code_type = 'AB' or bd1.code_type = 'CD')); 
update bill set pay = 0 where pay is null;
update bill set cost = 
  (select round(sum(bd2.bal),2) from "detail" bd2 where 
  bd2.inv = bill.inv and 
  (not (bd2.code_type = 'AB' or bd2.code_type = 'CD'))); 
update bill set cost = 0 where cost is null;
update bill set balance = round(cost + pay,2);

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:55:13+00:00Added an answer on June 2, 2026 at 9:55 pm

    Performance probably stinks because you are updating the entire table, and you are updating it twelve times. If the table is seriously big, that’s going to take
    time. Also, those two embedded subqueries will each get run once per row. Ouch.

    The following frankenquery rolls everything into a single statement. It still has to hit the entire table, but at least it only does it once. I can’t check syntax
    or test it against data, but this or something very much like it should work.

    EDITED, split this into two updates (thus, two table scans requried)

    UPDATE Detail
     set
       bal = case
               when code_type in ('AB','CD','EF') then  bi.units * bi.amount
               when code_type in ('gh','ik','lm') then -bi.units * bi.amount
               when code_type = 'NO' then 0
               else bal  --  If none of the above, change nothing
             end
    

    and

    UPDATE Bill
     set
       payments = isnull(bd1.amount, payments)  --  This changes nothing if nothing was calculated
      ,pay = case
               when pay is null then 0
               else pay
             end
       --  Ok, problem with cost: what if calculated amount is 0 and current value is non-zero?
       --  I've insufficient data here to correctly resolve all the possible permutations
      ,cost = case
                when bd2.amount is not null then cost
                when cost is null then 0
                else cost
              end
      ,balance = round(charges + isnull(bd1.amount, bi.payments), 2)
     from Bill bi
      --  These subqueries could be combined into one using similar CASE logic,
      --  and it would probably perform better (one table scan vs. two).  I'm
      --  leaving it this way for demonstration, and to keep the overall query
      --  a bit simpler.
      left outer join (select
                          inv
                         ,round(sum(bd1.bal), 2) amount
                        from detail
                        where code_type = 'AB'
                         or code_type = 'CD'
                        group by inv) bd1
       on bd1.inv = bi.inv  --  ADDED in second edit
      left outer join (select 
                          inv  --  RENAMED in second edit
                         ,round(sum(bd2.bal), 2) amount
                        from detail
                        where code_type <> 'AB'
                         and code_type <> 'CD'
                        group by inv) bd2  --  RENAMED in second edit
       on bd2.invoive = bi.inv  --  ADDED in second edit
    

    Moral: the CASE statement can be the SQL developer’s best friend.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like a pretty softball question, but I always have a hard time
This seems very noisy to me. Five lines of overhead is just too much.
This seems to me like a very simple question, but I can't seem to
This is very odd I'm reading some (admittedly very large: ~2GB each) binary files
Hmm. I found this which seems promising: http://sourceforge.net/projects/mjpg-streamer/ Ok. I will try to explain
This seems to be a non-issue for many people (read: I can't find an
This seems to be a simple question but nevertheless I haven't found an answer
This seems like it would be a pretty wide-spread use for simplemodal but I
This seems to be an overlooked area that could really use some insight. What
This seems trivial, but I've never had to worry about it before and my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.