簡體   English   中英

有沒有更有效的方法來做到這一點

[英]Is there a more efficient way to do this

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張優惠券。

您可以進行一些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

mysql_fetch_array($ result, MYSQL_ASSOC );

SELECT SQL_CACHE brand FROM

分析查詢

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM