Reputation: 44124
In PHP+MySQL+PDO, would it be much slower to do
than to just
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;
}
Upvotes: 0
Views: 310
Reputation: 10346
Why not create a single query that gets the item data by id, putting an index on the id.
Upvotes: 1
Reputation: 16509
You should design your query so that the fields that you use in your WHERE clause have proper keys and indexes set-up on them, and only use one query to select them.
Upvotes: 2