简体   繁体   English

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

[英]How Can I make this MySQL Count query more efficient?

//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>';
            }
        }

    }


}

It's easier to help if you can describe in non-code terms what you're trying to accomplish. 如果您可以用非代码术语描述您想要完成的任务,那么帮助会更容易。 But one indicator of a problem is seeing a php loop on rows from one query with another query executing for each row. 但问题的一个指标是从一个查询中查看行的php循环,并为每一行执行另一个查询。

There are ways to query for subtotals. 有几种方法可以查询小计。 But it would be easier to explain if you can explain the goal a bit. 但是如果你能解释一下这个目标会更容易解释。

count query would always return 1 row, so you don't need the loop 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'];

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 由于这些都是直接查询,我现在认为任何瓶颈都会出现在MySQL端

The first COUNT query ("get the current member count") should execute almost instantaneously. 第一个COUNT查询(“获取当前成员计数”)应该几乎立即执行。

The second query ("get list of products") may be slow depending on your indexes. 第二个查询(“获取产品列表”)可能很慢,具体取决于您的索引。 You are querying on m_field_name and then ordering on m_field_id so you may need a combined index of the two. 您正在查询m_field_name ,然后在m_field_id上进行排序,因此您可能需要两者的组合索引。

The third query, which is executed repeatedly (once for each product), is querying on m_field_id_* (ie any of a number of possible fields), so you should probably make sure they are indexed. 第三个查询,重复执行(每个产品一次),查询m_field_id_* (即任何一个可能的字段),所以你应该确保它们被索引。

In summary, you need to a) figure out which query is running slow, b) index things that need to be indexed, and c) combine queries if possible. 总之,您需要a)确定哪个查询运行缓慢,b)索引需要编制索引的内容,以及c)尽可能组合查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM