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 |
+----+--------------------+-------------+--------+------------------------+--------------+---------+----------------+--------+----------------------------------------------+
You should try running
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.