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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:05:32+00:00 2026-05-18T02:05:32+00:00

When trying to execute this query my mysql server cpu usage goes to 100%

  • 0

When trying to execute this query my mysql server cpu usage goes to 100% and the page just stalls. I setup an index on (Client_Code, Date_Time, Time_Stamp, Activity_Code, Employee_Name, ID_Transaction) it doesn’t seem to help. Now I need different way to make this query so its not so taxing on the server. Below is a description of what I need it to do. To explain it looks for the last entry under each client code for the specfic day then once it has found them all. It then counts how many of these entries Eric or Jerry has for example. Ill need to see code I’m not to familiar yet. Thank you for you help.

Here is what this query does

Database info

ID_Transaction | Client_Code | Employee_Name | Date_Time |Time_Stamp| Activity_Code  
1              | 00001       |  Eric         |  11/15/10 | 7:30AM   |  00023  
2              | 00001       |  Jerry        |  11/15/10 | 8:30AM   |  00033  
3              | 00002       |  Amy          |  11/15/10 | 9:45AM   |  00034  
4              | 00003       |  Jim          |  11/15/10 | 10:30AM  |  00063  
5              | 00003       |  Ryan         |  11/15/10 | 12:00PM  |  00063  
6              | 00003       |  bill         |  11/14/10 | 1:00pm   |  00054    
7              | 00004       |  Jim          |  11/15/10 | 1:00pm   |  00045  
8              | 00005       |  Jim          |  11/15/10 | 10:00 AM |  00045  

The query takes the info above and counts it like so. By the most recent entry for each client_code. In this case the query would look like this. After php.

Jerry = 1    
2               | 00001       |  Jerry        |   11/15/10 |   8:30AM |   00033     
Amy = 1   
3               | 00002       |  Amy          |   11/15/10 |   9:45AM |  00034   
Ryan = 1  
5               | 00003       | Ryan          |   11/15/10 |   12:00PM | 00063  
Jim = 2  
7               | 00004       | Jim           |   11/15/10 |   1:00pm  | 00045  
8               | 00005       | Jim           |   11/15/10 |   10:00 AM| 00045      

And the query:

 SELECT m.Employee_Name, count(m.ID_Transaction)   
     FROM ( 
         SELECT DISTINCT Client_Code 
             FROM Transaction
         ) md 
         JOIN Transaction m ON 
             m.ID_Transaction = ( 
                 SELECT ID_Transaction 
                     FROM Transaction mi  
                     WHERE mi.Client_Code = md.Client_Code 
                         AND Date_Time=CURdate() 
                         AND Time_Stamp!='' 
                         AND Activity_Code!='000001'  
                     ORDER BY m.Employee_Name DESC, 
                         mi.Client_Code  DESC, 
                         mi.Date_Time DESC,  
                         mi.ID_Transaction DESC 
                     LIMIT 1 
             )   
         GROUP BY m.Employee_Name    



+----+--------------------+-------------+--------+------------------------+--------------+---------+----------------+--------+----------------------------------------------+
| id | select_type        | table       | type   | possible_keys          | key          | key_len | ref            | rows   | Extra                                        |
+----+--------------------+-------------+--------+------------------------+--------------+---------+----------------+--------+----------------------------------------------+
|  1 | PRIMARY            | <derived2>  | ALL    | [NULL]                 | [NULL]       | [NULL]  | [NULL]         |    347 | Using temporary; Using filesort              |
|  1 | PRIMARY            | m           | index  | [NULL]                 | search index | 924     | [NULL]         |  29255 | Using where; Using index; Using join buffer  |
|  3 | DEPENDENT SUBQUERY | mi          | ref    | search index,secondary | search index | 18      | md.Client_Code |   2926 | Using where; Using temporary; Using filesort |
|  2 | DERIVED            | Transaction | range  | [NULL]                 | search index | 18      | [NULL]         |     10 | Using index for group-by                     |
+----+--------------------+-------------+--------+------------------------+--------------+---------+----------------+--------+----------------------------------------------+
  • 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-18T02:05:33+00:00Added an answer on May 18, 2026 at 2:05 am

    You should try running

    EXPLAIN SELECT m.Employee_Name, count(m.ID_Transaction)   
    FROM ( SELECT DISTINCT Client_Code FROM Transaction) md 
    JOIN Transaction m
      ON m.ID_Transaction = ( SELECT ID_Transaction FROM Transaction mi  
                              WHERE mi.Client_Code = md.Client_Code AND
                                    Date_Time=CURdate() AND 
                                    Time_Stamp!='' AND 
                                    Activity_Code!='000001'  
                              ORDER BY m.Employee_Name DESC,
                                       mi.Client_Code  DESC, 
                                       mi.Date_Time DESC,
                                       mi.ID_Transaction DESC LIMIT 1 
                            )  
    GROUP BY m.Employee_Name
    

    to get an indication of how the query optimizer is processing your query.

    I do notice that you’re hitting the same table 3 times. I’m guessing that’s causing the optimizer some challenges.

    It might make more sense to just write a simpler SQL query and then use PHP to pare it down to what you’re looking for.

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

Sidebar

Related Questions

I am trying to execute this SQL query prior to restoring a .BAK file
I am trying to execute this SQL command: SELECT page.page_namespace, pagelinks.pl_namespace, COUNT(*) FROM page,
I'm trying to execute this query and when it finds a record for $serial,
I hit this error while my web application was trying to execute a SELECT
I am trying to get simple jQuery to execute on my Content page with
I am trying to execute my PHP code, which calls two MySQL queries via
This has annoyed me for a while now. I am trying this query in
I am trying to execute a stored procedured from a linked database in MS
I'm trying to execute a asp.net webservice using jquery. When I pass only one
I'm trying to access the command line and execute a command, and then return

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.