In PHP+MySQL+PDO, would it be much slower to do
- Get an item's ID by name
- Get item data by ID
than to just
- Get item data by name
The latter is only one query, so obviously faster. The former makes for cleaner code though (because the item's ID is often also just known beforehand), so it would be better if the performance difference is small enough.
The code where I'm 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;
}