efficient way to query a list of content, each content need to get list of tags and categories
Here is my tables and my current method. Too much query, so any most efficient way to do this?
Table: contents
content_id
Table: taxonomy_terms
tt_id
type (tag or category)
name
Table: relationships
content_id
tt_id
Pseudo code:
mysql: query limit 20 contents from **contents**
php开发者_如何学JAVA: for each content
mysql: query tags and categories from **relationships** with content_id
Number of queries: 1 + 20 = 21
You can iterate the first query first, and do something like
$ids = array();
$contents = array();
while ($row = $res->fetch_assoc())
{
/* your existing code */
/* such as fill-up an array */
$contents[$row['content_id']] = $row;
$ids[] = $row['content_id'];
}
$sql = 'SELECT ... WHERE tt_id in('.implode(',', $ids).')';
/* fetch the tag/category results */
/* and update $contents with tag/category results */
With this, need two queries.
And an additional PHP loop for 20 contents you have in the first query
精彩评论