有没有更有效的方法来做到这一点

时间:2011-10-15 04:14:11

标签: php mysql

function countBrand($brand_id, $brand_name) {
    $sql = "SELECT brand FROM coupons WHERE expire >= CURRENT_DATE AND brand='".$brand_id."'";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    echo '<li>'.$brand_name.'</li>';
}

function brandCount() {
    $sql = "SELECT DISTINCT brand,brand_id,brand_name FROM coupons,coupons_brand WHERE brand=brand_id AND expire >= CURRENT_DATE ORDER BY brand_name";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    $num = mysql_num_rows($result);

    echo '<h3>'.$num.' Brands</h3>';

    echo '<ul>';

    $i = 0;
    while ($i < $num) {
        $brand_id = mysql_result($result, $i, "brand_id");
        $brand_name = mysql_result($result, $i, "brand_name");
        countBrand($brand_id, $brand_name);
        $i++;
    }

    echo '</ul>';
}

它完美运作,并给我我想要的结果。我不像我想的那样强大的sql语句。有没有办法可以做到更有效率,看起来很慢。

基本上,它计算有多少品牌有优惠券,然后优惠券每个品牌有多少优惠券..

我也在同一页面上为类别执行此操作。有几千种类别,可能有20,000张优惠券。

2 个答案:

答案 0 :(得分:1)

你可以做一些PHP优化,但与在正确的列上添加索引到mysql相比,它们可能不会节省你很多时间

我评论了下面的一些php优化,无论如何你可能感兴趣

考虑到函数的简单特性,没有必要有2个函数,这样可以节省调用countBrand()的时间成本,(尽管节省的时间非常少)

   function countBrand($brand_id, $brand_name) {
        $sql = "SELECT brand FROM coupons WHERE expire >= CURRENT_DATE AND brand='".$brand_id."'";
        $result = mysql_query($sql) || die (mysql_error()); // always check for errors

        list($brand) = mysql_fetch_row($result);

// fetch row, returns a more concise array then mysql_fetch_array
//        $row = mysql_fetch_array($result);

        // use commas rather then . when concatenating echo statements
        // dots force concatenation before output
        echo '<li>',$brand,'</li>'; 

    }

    function brandCount() {
        $sql = "SELECT DISTINCT brand,brand_id,brand_name FROM coupons,coupons_brand WHERE brand=brand_id AND expire >= CURRENT_DATE ORDER BY brand_name";
        $result = mysql_query($sql) || die(mysql_error()); // always check for errors

    //    $row = mysql_fetch_array($result); // not sure why this is needed
        $num = mysql_num_rows($result); 

        // use commas rather then . when concatenating echo statements
        // dots force concatenation before output
        echo '<h3>',$num,' Brands</h3>';

        echo '<ul>';

      // fetch all cols at once, rather then using lots of separate calls to mysql_result
      // use mysql_fetch_row as it returns just the ordered values (vs mysql_fetch_assoc, and mysql_fetch_array)
      // 
       while(list($brand, $brand_id, $brand_name) == mysql_fetch_row($result)) {
            countBrand($brand_id, $brand_name);
       }

      // replaced with while loop above
      //  $i = 0;
      //  while ($i < $num) {
      //      $brand_id = mysql_result($result, $i, "brand_id");
      //      $brand_name = mysql_result($result, $i, "brand_name");
     //       countBrand($brand_id, $brand_name);
      //      $i++;
        }

        echo '</ul>';
    }

这些增强功能只能为您带来轻微的速度提升 如果你少打电话给数据库,你将获得的最大速度提升。

目前您选择每个品牌,然后返回并单独计算每个品牌 在不知道你的表结构的情况下,这个sql对我来说很难写,所以这是猜测,但它应该指向正确的方向

SELECT brand, brand_id, brand_name, COUNT(*) as brandcount
FROM coupons
JOIN coupons_brand ON brand=brand_id 
WHERE expire >= CURRENT_DATE 
GROUP BY brand, brand_id, brand_name
ORDER BY brand_name

答案 1 :(得分:0)

mysql_fetch_array($结果,的 MYSQL_ASSOC );

SELECT SQL_CACHE brand FROM

Profiling query