Two requests on one request, performance

In PHP + MySQL + PDO it would be much slower to do

  • Get element id by name
  • Get item data by id

than just

  • Get item data by name

The latter is only one request, so obviously faster. The former makes for cleaner code (because the element ID is often also known in advance), so it would be better if the performance difference was small enough.

The code where I am using this:

public function actionView () {

    // read http request
    ...

    // get item
    $itemModel = new Rust_Model_Item();
    try {
        $id = $itemModel->getItemIdByUrlTitle($urltitle);
        $item = $itemModel->getItem($id); // lots of data
    } catch (Rust_Model_Item_NotFoundException $e) {
        throw new FastMVC_Web_Error(404, 'Item not found');
    }

    ...

}

      

http://code.heukelom.net/filedetails.php?repname=rust&path=%2Ftrunk%2Flib%2Fclasses%2FRust%2FController%2FItem.php

public function getItem ($ id) {

    $item = $this->getItemBasics($id);

    $catModel = new Rust_Model_Category();
    $item['category'] = $catModel->getById($item['category_id']);

    $ratingModel = new Rust_Model_Rating();
    $item['rating'] = $ratingModel->getForItem($id);

    $pageModel = new Rust_Model_Page();
    $item['pages'] = $pageModel->getListForItem($id);

    $tagModel = new Rust_Model_Tag();
    $item['tags'] = $tagModel->getForItem($id);

    return $item;

}

      

http://code.heukelom.net/filedetails.php?repname=rust&path=%2Ftrunk%2Flib%2Fclasses%2FRust%2FModel%2FItem.php

0


a source to share


2 answers


You should design your query so that the fields you use in your WHERE clause have the correct key and index settings on them and only use one query to select them.



+2


a source


Why not create a single query that gets the item data by id, putting the index into the id.



+1


a source







All Articles