doumiyi7063 2011-05-19 00:27
浏览 44
已采纳

如何使这个MySQL Count查询更有效?

//get the current member count
$sql = ("SELECT count(member_id) as total_members from exp_members");
$result = mysql_query($sql) or die(mysql_error());
$num_rows = mysql_num_rows($result);
if ($num_rows != 0) {
    while($row = mysql_fetch_array($result)) {
        $total_members = $row['total_members'];
    }
}

//get list of products
$sql = ("SELECT m_field_id, m_field_label from exp_member_fields where m_field_name like 'cf_member_ap_%' order by m_field_id asc");
$result = mysql_query($sql) or die(mysql_error());
$num_rows = mysql_num_rows($result);

if ($num_rows != 0) {

    while($row = mysql_fetch_array($result)) {

        $m_field_id             = $row['m_field_id'];
        $m_field_label          = $row['m_field_label'];

        $sql2 = ("SELECT count(m_field_id_".$m_field_id.") as count from exp_member_data where m_field_id_".$m_field_id." = 'y'");
        $result2 = mysql_query($sql2) or die(mysql_error());
        $num_rows2 = mysql_num_rows($result2);

        if ($num_rows2 != 0) {
            while($row2 = mysql_fetch_array($result2)) {
                $p = ($row2['count']/$total_members)*100;
                $n = $row2['count'];
                $out .= '<tr><td>'.$m_field_label.'</td><td>'.number_format($p,1).'%</td><td>'.$n.'</td></tr>';
            }
        }

    }


}
  • 写回答

3条回答 默认 最新

  • dongqiuge5435 2011-05-19 00:40
    关注

    count query would always return 1 row, so you don't need the loop

    $sql = ("SELECT count(member_id) as total_members from exp_members");
    $result = mysql_query($sql) or die(mysql_error());
    $row = mysql_fetch_array($result);
    $total_members = $row['total_members'];
    

    Other than that i am not sure how you can make it better. You can do the same for both of your count queries.

    As these are straight forward queries, any bottleneck i guess now would be on the MySQL end

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致