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');
}
...
}
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;
}
0
a source to share
2 answers