php - MYSQL Query locking up server -


when trying execute query mysql server cpu usage goes 100% , page stalls. setup index on (client_code, date_time, time_stamp, activity_code, employee_name, id_transaction) doesn't seem help. steps can go next fix issue? there 1 index on database if matters any. thanks

here 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 info above , counts so. recent entry each client_code. in case query 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        $sql = "select m.employee_name, count(m.id_transaction)    ( select distinct client_code transaction)     md join transaction m on     m.id_transaction = ( select   id_transaction transaction mi   mi.client_code = md.client_code , date_time=curdate() , time_stamp!='' ,   activity_code!='000001'   order m.employee_name desc, mi.client_code  desc, mi.date_time desc,   mi.id_transaction desc limit 1 )   group m.employee_name";  

is there better way write query doesnt bog down system? query works fine 10 database entries locks server when database has 300,000 entries.

thanks eric

+----+--------------------+-------------+--------+------------------------+--------------+---------+----------------+------+----------+----------------------------------------------+ | id | select_type        | table       | type   | possible_keys          | key          | key_len | ref            | rows | filtered |                                        | +----+--------------------+-------------+--------+------------------------+--------------+---------+----------------+------+----------+----------------------------------------------+ |  1 | primary            | <derived2>  |    | [null]                 | [null]       | [null]  | [null]         |    8 |   100.00 | using temporary; using filesort              | |  1 | primary            | m           | index  | [null]                 | search index | 924     | [null]         |   21 |   100.00 | using where; using index; using join buffer  | |  3 | dependent subquery | mi          | ref    | search index,secondary | search index | 18      | md.client_code |    3 |   100.00 | using where; using temporary; using filesort | |  2 | derived            | transaction | index  | [null]                 | secondary    | 918     | [null]         |   21 |    38.10 | using index                                  | +----+--------------------+-------------+--------+------------------------+--------------+---------+----------------+------+----------+----------------------------------------------+ 

what going multiple group by's instead of sub queries simplify things.... like:

select * transaction date_time=curdate() , time_stamp!='' , activity_code != '000001' group client_code, employee_name 

if i'm understanding query correctly solve issues , prevent need sub queries.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -