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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:59:40+00:00 2026-05-23T10:59:40+00:00

I normally do most of my work in java, I cannot really find what

  • 0

I normally do most of my work in java, I cannot really find what I’m doing here that is causing it to need to copy to a table and die.

SELECT company.tblusers.userid, 
db.operations.id AS operation_id,
SUM(TIME_TO_SEC(db.batch_log.time_elapsed)) AS time_elapsed, 
SUM(db.tasks.estimated_nonrecurring + db.tasks.estimated_recurring) AS total_elapsed,
COUNT(db2.ncr_ncr.id) AS number_of_ncrs
FROM company.tblusers 
INNER JOIN db.operations 
INNER JOIN db.batch_log ON company.tblusers.userid = batch_log.userid 
INNER JOIN db.batches ON batch_log.batch_id = batches.id 
INNER JOIN db.tasks ON db.batches.id = db.tasks.batch_id
INNER JOIN db2.ncr_ncr ON company.tblusers.sugar_name = db2.ncr_ncr.employee
WHERE company.tblusers.departmentid = 8 
AND db.operations.id = db.batches.operation_id 
AND db.batches.id = db.tasks.batch_id 
AND db.batch_log.userid = company.tblusers.userid
AND db2.ncr_ncr.employee = company.tblusers.sugar_name 
GROUP by company.tblusers.userid, db.batches.operation_id

EDIT: Explain output

"id";"select_type";"table";"type";"possible_keys";"key";"key_len";"ref";"rows";"Extra"
"1";"SIMPLE";"ncr_ncr";"ALL";NULL;NULL;NULL;NULL;"2700";"Using temporary; Using filesort"
"1";"SIMPLE";"batch_log";"ALL";NULL;NULL;NULL;NULL;"78026";""
"1";"SIMPLE";"tblusers";"eq_ref";"PRIMARY";"PRIMARY";"52";"ramses.batch_log.userid";"1";"Using where"
"1";"SIMPLE";"tasks";"ref";"Index 2";"Index 2";"38";"ramses.batch_log.batch_id";"2";""
"1";"SIMPLE";"batches";"eq_ref";"PRIMARY";"PRIMARY";"38";"ramses.tasks.batch_id";"1";"Using where"
"1";"SIMPLE";"operations";"eq_ref";"PRIMARY";"PRIMARY";"4";"ramses.batches.operation_id";"1";"Using where; Using index"
  • 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-05-23T10:59:40+00:00Added an answer on May 23, 2026 at 10:59 am

    If you aggregate a value while grouping on a non-indexed combination of columns, then the SQL engine will create a temporary table as part of its normal functioning in order to hold the partially aggregated values. You have two SUMs and a COUNT that need to be calculated for each combination of company.tblusers.userid and db.batches.operation_id. These values must be stored, since, clearly, there is no index that contains both of those columns, since they are in different tables.

    I don’t know why it would die, though, unless it is actually building the full join as a termporary table, because of the COUNT aggregation.

    Instead of joining to the db2.ncr_ncr table, you should probably just make it a correlated subquery. This may require less storage:

    SELECT
            company.tblusers.userid,
            db.operations.id AS operation_id,
            SUM(TIME_TO_SEC(db.batch_log.time_elapsed)) AS time_elapsed,
            SUM(db.tasks.estimated_nonrecurring + db.tasks.estimated_recurring) AS total_elapsed,
            (SELECT COUNT(*) FROM db2.ncr_ncr WHERE db2.ncr_ncr.employee = company.tblusers.sugar_name) AS number_of_ncrs
    FROM 
            company.tblusers
            INNER JOIN db.batch_log ON batch_log.userid = company.tblusers.userid
            INNER JOIN db.batches ON db.batches.id = db.batch_log.batch_id
            INNER JOIN db.operations ON db.operations.id = db.batches.operation_id
            INNER JOIN db.tasks ON db.tasks.batch_id = db.batch_log.batch_id
    WHERE
            company.tblusers.departmentid = 8
    GROUP BY 
            company.tblusers.userid,
            db.batches.operation_id 
    

    UPDATE

    Based on the explain-plan results, you do not have any index that the query can use on ncr_ncr. This is bad. It is actually creating a new copy of the table that it can sort by employee so that the join can happen in a reasonable amount of time.

    Even though that table is not very big (just 2700 rows), you still need to put an index (preferably correlated, if the rest of the system allows it) on db2.ncr_ncr.employee, otherwise your performance is going to be suffer badly. This table is small enough that making and sorting a temporary copy shouldn’t be a problem, but it will still hurt your performance.

    The same is true for batch_log.userid.

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

Sidebar

Related Questions

I'm doing some work in Processing, which is basically Java. I normally do only
I normally work with jQuery, which takes away most of the cross browser pain
Normally most of the MFC (window based classes) are derived from CObject. What does
Normally, I run Linux in a VM, however, most of my VMs are on
Normally I use imagecreatefromjpeg() and then getimagesize() , but with Firefox 3 I need
I am normally a firm believer of using namespaces (qualified module names) in most
I have a problem with a really slow connection between my Java code and
I normally don't work on Windows development, and am completely unfamiliar with the toolchain
I have a function that is normally called while looping inside a SqlDataReader, that
I have a fairly large PHP codebase (10k files) that I work with using

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.