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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何使这个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>';
            }
        }

    }


}

解决方法

count查询总是会返回1行,所以你不需要循环

$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'];

除此之外我不知道你怎么能让它变得更好.您可以对两个计数查询执行相同的操作.

由于这些都是直接查询,我现在认为任何瓶颈都会出现在MysqL

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何使这个MySQL Count查询更有效?全部内容,希望文章能够帮你解决php – 如何使这个MySQL Count查询更有效?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。