1

I have a tables with many actions, a field is "created" with the datetime of the action. I need to get back an array with how many actions I registered per HOUR, WEEK DAY, DAY, MONTH in a periode range.

Is better doing a unique db request and parsing all results

    foreach($scans as $s) {
        $data['total']++;
        $t = strtotime($s['Scan']['created']);
        //week day
        @$data['weekday'][date('w',$t)]++;
        @$data['hour'][date('H',$t)]++;
        @$data['day'][date('d',$t)]++;
        @$data['month'][date('n',$t)]++;
    }

or 5 queries with count() function? Thanks

0

2 Answers 2

2

A database is equipped to perform exactly this sort of tasks with the best efficiency. You should offload most of data processing to the database system, which will do all sort of optimizations for you, and leave PHP for managing and presentation. In principle you should avoid looping over data in PHP to do processing, unless you are doing presentation stuff.

So you should do your entire thing in ONE query, and get the final result from your database. Look at your query and try to combine all the things you want into it. Maybe you can try to get help from Stack Overflow if you can't come up with the right query (but open another question for it).

Sign up to request clarification or add additional context in comments.

5 Comments

I think that could be max 10000 rows at time
So you need to come up with one query to do all your stuff in one step. 10.000 rows is a number which could slow your script down.
Sorry how I could do all in ONE query?
I did a test untill 700 rows same time, over 700 queries Win. But I did with 5 queries, I don't understand how to only ONE.
Simplest way? Chain your queries with UNION :)
0

A Unique db request and parsing all results. There would be less network overhead.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.