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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:39:38+00:00 2026-06-11T00:39:38+00:00

I need ideas to optimize this query: SELECT U.Id , U.Name , SUM(P.Credit) AS

  • 0

I need ideas to optimize this query:

SELECT U.Id
    , U.Name
    , SUM(P.Credit) AS CreditAmount
    , SUM(P.Debit) AS DebitAmount
    FROM users U
    LEFT JOIN payments P ON P.UserId = U.Id
    WHERE U.StatusId = 1 /* Active Users Only */
    GROUP BY U.Id

Some information about the tables:

  1. users table: 10,000+ records
  2. payments table: 2,000,000+ records, indexed on UserId column

The query takes up to 2 minutes. By investigating the query, I found out that the SUM function is the reason why the query is slow. Tried executing the query without the SUM takes less than 2 seconds.

Is there any room to improve this query? Thanks

EDIT 1

This is what I got when using the EXPLAIN:

id  select_type table   type    possible_keys   key     key_len     ref             rows    Extra
1   SIMPLE      U       ALL                                                         8304    Using where; Using temporary; Using filesort
1   SIMPLE      P       ref     UserId          UserId  5           Database.U.Id   361     ""

The index payments.UserId is being used in the query. Any thoughts on this?

EDIT 2
Table Information:

users   "CREATE TABLE `users` (
  `Id` int(11) NOT NULL auto_increment,
  `StatusId` int(11) default NULL,
  `Name` varchar(60) default NULL,
  `TimeZone` varchar(100) default NULL,
  PRIMARY KEY  (`Id`),
  KEY `StatusId` (`StatusId`),
  CONSTRAINT `FK_User_Status` FOREIGN KEY (`StatusId`) REFERENCES `userStatus` (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8"

payments    "CREATE TABLE `payments` (
  `Id` int(11) NOT NULL auto_increment,
  `UserId` int(11) default NULL,  
  `Description` varchar(200) default NULL,
  `Debit` decimal(11,2) default NULL,
  `Credit` decimal(11,2) default NULL,
  `Date` datetime default NULL,
  PRIMARY KEY  (`Id`),
  KEY `UserId` (`UserId`),
  CONSTRAINT `FK_Payment_User` FOREIGN KEY (`UserId`) REFERENCES `users` (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8"
  • 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-11T00:39:39+00:00Added an answer on June 11, 2026 at 12:39 am

    According this link, I can’t speed up the SUM function. The main issue here is that I’m selecting a huge number of records.

    I thought of redesigning the users table, adding a new column like TotalPayment which will hold the total credit minus debit amount from the payments table. I know it disobeys the database normalization rules, but I guess this redesign is for saving performance.

    Thanks a lot for your help guys.

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

Sidebar

Related Questions

I have no ideas how to do this, so I need your advice. I
I need to optimize a query for a ranking that is taking forever (the
SCENARIO I need to select records from test_userData based on a 1-to-1 match from
I need to acquire some data from a questions table and then LEFT JOIN
I have written the following query. select distinct(table3.*), (select count(*) from table2 where table2.cus_id
I need ideas for structuring and processing data with revisions. For example, I have
I need topic ideas related to AI on which I will base my thesis.
OK, here's what I want to implement and I need your ideas : Input
I have repaired .net 4.0 but (still) cannot find: Ilasm.exe Any ideas? I need
I need to archive data from a CRM system. Does anyone have any idea

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.