以下两个查询中哪个查询可能更快?
$cposts = mysql_query("SELECT id FROM posts WHERE company_id = ".$dealid." ");
$sum_posts= mysql_num_rows($cposts);
echo $sum_posts;
要么
$cposts2 = mysql_query("SELECT count(id) as myid FROM posts WHERE company_id = ".$dealid." ");
$sum_posts2= mysql_fetch_assoc($cposts2);
echo $sum_posts2['myid'];
最佳答案
我相信计数会更好,并且将使用更少的处理。
我会这样:
$cposts = mysql_query('SELECT COUNT(*) FROM posts WHERE company_id = ' . $dealid);
$num_posts = (int)mysql_result($cposts, 0);
echo $num_posts;