• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mutiple queries or sort through one large query

#1
[eluser]Otemu[/eluser]
Hi,

When working with database driven websites its common that you could have similar queries on one page, for example a football page might have several modules that display fixtures, live matches and results.

Just say as a simple example there getting this data from one table stored in the database using a simple where clause of fixtures, live and results on one of the columns in the table.

Now my question is how best would be to handle this:

1. Multiple queries?
Code:
$data['upcoming'] = $this->M_Football->footballTbl('fixtures');
$data['current '] = $this->M_Football->footballTbl('live');
$data['results'] = $this->M_Football->footballTbl('results');

Then each view module would use a foreach with each array set

2. Retrieving the result as a single large query?
then using a foreach switch to sort out each set of data for each module
Code:
foreach($array as $key => $value) {
    switch($key) {
        case 'fixtures':
            doSomething($value);
            break;
        case 'live':
            doSomethingElse($value);
            break;
        case 'results':
            doSomethingElse($value);
            break;
    }
}

3. Or is there another better technique I should be using??

Currently I use multiple queries and I wasn't sure if this was best practices

Thanks for any help






#2
[eluser]apodner[/eluser]
There are a lot of variables that come into play like size of each query, etc.

All things being equal, I would say this:

1) Are you going to use the smaller queries to populate other pages where only one dataset is needed? If so, then you might have a better time using multiple queries because there is more reusability and less code to maintain.

2) If this dataset is only used on one page, then I would probably just have 1 database call and sort the rest out with PHP.

I do not think there is a one size fits all answer to your question. You just need to handle it in a way that works best for your application given all the factors at hand.

#3
[eluser]Otemu[/eluser]
Thanks for your response apodner it has helped me a lot, I just needed to be sure I was on the right track and since you didn't give a "that is bad coding or you shouldn't do that" kind of response" now I know am on the right path.

Thank You


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.