0

I currently have 12 SQL queries (all are COUNT (*) ones) per team member and there are around 100 team members running on a database to pull out some live stats for the managers, however this is killing the system it's running on whenever the page is loaded, what I'd like to do is do 1 query and then filter those results using PHP to take the load off the database but I cannot figure out how to do this.

current query:

select count(*) from opencall use index (logdatex) 
where closedby = '$UsersName' and priority = 'P2' and withinfix = 0 
and logdatex between $startOfLastMonth and $endOfLatMonth 
and status IN (6, 16, 18)

we run this for 2 different time periods (last month & yesterday) each with 3 different priorities and 2 withinfix per user. What I'd like to do is something like this

select logdatex, closedby, withinfix, suppgroup 
from opencall use index (logdatex) 
where closedby = '$UsersName' 
and logdatex between $startOfLastMonth and $midnightYesterday 
and status IN (6, 16, 18)

and then in PHP do a count where username = XXXX && withinfix = 1 && logdatex => yesterday morning && logdatex <= midnightyesterday.

4
  • Doesn't GROUP BY closedby, priority, withinfix do what you want?
    – Barmar
    Mar 10, 2015 at 16:31
  • 3
    you're running 12x100 = 1,200 total queries? That's pretty redundant. it should be pretty easy to run each of those 12 queries once, retrieving results PER user in a single query call.
    – Marc B
    Mar 10, 2015 at 16:31
  • @MarcB can you expand on that for me please?
    – Maff
    Mar 10, 2015 at 16:40
  • 2
    take out the closedby = '$UsersName, and add a group by closedby. you'll get the query results for all of your users in a single query. now you've got from 100 repetitions of the same query to a single one. then in the php code you simply loop on the results and pull out each user's data.
    – Marc B
    Mar 10, 2015 at 16:44

1 Answer 1

2

Try this:

SELECT
    closedby, priority, withinfix
FROM
    opencall
WHERE
    -- Specify the list of users if you want to limit them
    closedby IN ('user1', 'user2', 'user99', 'user100') AND

    -- Specify the possible priorities here
    priority IN ('P1', 'P2', 'P3') AND

    -- Specify the allowed withinfix here
    withinfix IN (0, 1, 2) AND

    logdatex between '$startOfLastMonth' AND '$endOfLastMonth' AND
    status IN (6, 16, 18)

    -- This is where you need the grouping of the results
    GROUP BY closedby, priority, withinfix;

That should group your result set by the filters you need.

I'm not sure what you mean by the two time periods so I left that out from the combined query. However, I'm sure we can put that in if needed.

For your reference: MySQL documentation on GROUP BY.

2
  • added a select count(*) as well, perfect, thank you, cut it all down to 2 queries now (one for each time frame)
    – Maff
    Mar 11, 2015 at 7:50
  • Oopss.. yes, you are right. I forgot that you want to get the count, so naturally adding the column for COUNT(*) is needed. Thanks for the update. :)
    – Sutandiono
    Mar 11, 2015 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.