luc*_*uca 2 php mysql redundancy
我在我的网站中有一个部分,我需要计算特定帖子的注释数量.我实际上已经构建了一个函数来检索注释数据库表中的所有数据(函数getComments).
注释DB表结构是:id(PK)| authorId | datetime | postId | 文本
现在,因为我只需要计算它们(评论),我想知道在服务器资源/冗余方面是否更好用:
$comments=new Comment();
$comments->getComments();
echo count($comments);
Run Code Online (Sandbox Code Playgroud)
或者我最好建立另一个功能(除了'getComments'),如:
function countComments($postid)
{
//count comments per post
}
Run Code Online (Sandbox Code Playgroud)
谢谢
卢卡
计算数据库中的注释效率更高,因为
假设你使用Zend_Db这样的东西应该工作:
$query = $db->select()->from('comments', 'count(*)');
$count = $db->fetchOne($query);
Run Code Online (Sandbox Code Playgroud)
代码冗余在这里不是问题恕我直言,它只是一两行.