Re: php / mysql performance resources

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2007-06-13 at 23:41 -0400, Guillaume Theoret wrote:
> On 6/13/07, Daevid Vincent <daevid@xxxxxxxxxx> wrote:
> >
> > <?= $employeeType[ $row['employee_type_id'] ] ?>
> >
> > I actually use this little optimizing trick as an interview question for
> > new hires. You'd be amazed at how many people don't think of this, as
> > obvious as it seems to me...

Yep, for small sets of id to name mappings where the set won't likely
change much I do the exact same thing. No need for a DB hit for
something like that.

> That's a neat trick, I'll try to keep it in mind. In our current setup
> we'd do something like:
> 
> [pseudo]
> 
> while($row)
> $employee = find::($row['employee_id']); // Return an Employee object
> echo $employee->getType()->getName();
> 
> So here, when we created an employee object, it did a db hit and
> looked in the employees table. The type attribute for employee was
> just a ProxyType object (with an id that was stored as type_id in the
> employees table). When I tried to access the name the ProxyType did a
> db hit and replaced itself with a Type object that had all the columns
> of the types table as its attributes. If we needed to later access
> some other attribute of the employee's type, no db hit would be made
> since it's now loaded.
> 
> In this case, if you're looping over all employees and outputting
> their type names you'd be doing nearly twice as many db hits with my
> method. Thanks for the tip.

When I'm avoiding JOIN and I need related data I usually do something
like the following:

<?php

    $criteria = array
    (
        'offset'  => $offset,
        'limit'   => $limit,
    );

    $posts = $postFactory->getMatches( $criteria );

    $userIds = array();
    foreach( array_keys( $posts ) as $postId )
    {
        $userId = $posts[$postId]->userId();
        $userIds[$userId] = $userId;
    }

    $criteria = array
    (
        'userId' => $userIds,
    );

    $users = $userFactory->getMatches( $criteria );

?>

The getMatches() method when it encounters an array for the 'userId'
criteria entry will use an IN clause for the array of user IDs given. So
it takes two queries -- one to get the topics, one to get the user
information.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux