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 9252843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:03:32+00:00 2026-06-18T11:03:32+00:00

I created a query, that collects data from two tables, summs them up and

  • 0

I created a query, that collects data from two tables, summs them up and shows the count of the cases and the total summ:

SELECT
    count(ut.id) AS total
    , ( SUM(internal_account) - SUM(( SELECT
            SUM( ub.bill_summs )
        FROM
            u_billing ub
        WHERE
            ub.bill_types = 'correction'
            AND ub.contract_id = ut.contract_id
      )) ) AS summ
FROM  
    u_transactions ut
WHERE
    ut.nulled = 0
    AND ut.type = 'comission'
    AND ut._status = 'not_paid'
    AND DATE( ut.add_timestamp ) = DATE( '2012-05-11' );

but it is really slow. On test cases it gave this result:

+-------+-------+
| total | summ  |
+-------+-------+
|   182 | 15105 |
+-------+-------+
1 row in set (4.13 sec)

It is 4.13 seconds on 182 cases and for only 1 day, but my live server has over 600k cases, so this will be extremely slow.

Any ideas, how I can rewrite the query for better performance?


Solution with remade query(ies):

DELETE FROM tmpContractSums;
INSERT INTO tmpContractSums
SELECT
    ub.contract_id
    , SUM( ub.bill_summs ) AS bill_summs
FROM
    u_billing ub
WHERE
    ub.bill_types = 'correction'
GROUP BY ub.contract_id;

SELECT
    count(ut.id) AS total
    , ( SUM(internal_account) - SUM(bill_summs) )
FROM  
    u_transactions ut
LEFT JOIN tmpContractSums t ON ut.contract_id = t.contract_id
WHERE
    ut.nulled = 0
    AND ut.type = 'comission'
    AND ut._status = 'not_paid'
    AND ut.add_timestamp BETWEEN '2012-05-11 00:00:00' AND '2012-05-11 23:59:59';

Execution time: 500ms

PS: Since I can’t drop tables with webuser I just created the table:

CREATE TABLE tmpContractSums AS SELECT contract_id, bill_summs FROM u_billing WHERE 1 = 0;

and the I’m deleting the records. Not as fast as drop, but still way faster then original.

  • 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-18T11:03:33+00:00Added an answer on June 18, 2026 at 11:03 am

    How about simply using a “temporary” table?

    DROP TABLE IF EXISTS tmpContractSums;
    CREATE TABLE tmpContractSums AS SELECT contract_id, bill_summs FROM u_billing WHERE 1 = 0;
    INSERT INTO tmpContractSums
    SELECT
        ub.contract_id
        SUM( ub.bill_summs ) AS bill_summs
    FROM
        u_billing ub
    WHERE
        ub.bill_types = 'correction'
    GROUP BY ub.contract_id;
    
    SELECT
        count(ut.id) AS total
        , ( SUM(internal_account) - COALESCE(bill_summs, 0) )
    FROM  
        u_transactions ut
    LEFT JOIN tmpContractSums t ON ut.contract_id = t.contract_id
    WHERE
        ut.nulled = 0
        AND ut.type = 'comission'
        AND ut._status = 'not_paid'
        AND ut.add_timestamp BETWEEN '2012-05-11' AND '2012-05-11 23:59:59';
    

    This should be faster and if you want you can add indexes to the “temporary table” or make it a table with engine=memory if you have enough space.

    Or:

    DROP TABLE IF EXISTS tmpContractSums;
    CREATE TABLE tmpContractSums AS SELECT contract_id, bill_summs FROM u_billing WHERE 1 = 0;
    INSERT INTO tmpContractSums
    SELECT
        ub.contract_id
        SUM( ub.bill_summs ) AS bill_summs
    FROM
        u_billing ub
    WHERE
        ub.bill_types = 'correction'
    GROUP BY ub.contract_id;
    
    SELECT
        count(ut.id) AS total
        , ( SUM(internal_account) - (SELECT bill_summs FROM tmpContractSums t WHERE ut.contract_id = t.contract_id ) )
    FROM  
        u_transactions ut
    WHERE
        ut.nulled = 0
        AND ut.type = 'comission'
        AND ut._status = 'not_paid'
        AND ut.add_timestamp BETWEEN '2012-05-11' AND '2012-05-11 23:59:59';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking to create a SQL query that collects the results from multiple
i have created this query that works OK: $q1 = Doctrine_Query::create() ->from('Usuario u') ->leftJoin('u.AmigoUsuario
I've created a query that groups production data on ISO week by using this
I created a query that takes a database backup at certain specified location. I
I'm trying to create a created_at query range that will capture all records created
How can I query for records that were created at a certain hour? Will
I have created a T-SQL query in SQL Server 2008 R2 that is a
I've been trying now for some time to create a query that would count
Working with a SqlCommand in C# I've created a query that contains a IN
I have a program that collects information from the database. in my previous question

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.