简体   繁体   中英

MYsql query and DB help

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. What steps can I go about next to fix this issue? Also there is already one index on the database if that matters any. Thanks

> $sql = "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";  

我有一种感觉,这是由于AND旁边的那个>签名,你试图用它做出什么表达?

Could you express in plain English what results you're after with your query? I've got a feeling this query can be rewritten; that might fix your performance issue. A double join on itself might hurt performance badly.

Something like "the number of transactions per employee for today"?

If that's what you're after, please try this and refine from there:

select m.Employee_Name, count(m.ID_Transaction)
from Transaction m
where m.Date_time=CURdate()
and m.Time_Stamp != ''
and m.Activity_Code != '000001'
group by m.Employee_Name

I Could've sworn I had already posted my response here today....

Why not use a double GROUP BY ? I think this is what you were looking for...

SELECT Employee_Name, count(ID_Transaction) FROM Transaction WHERE Date_time=CURdate() AND Time_Stamp != '' AND Activity_Code != '000001' GROUP BY Client_Code, Employee_Name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM