頁面執行時間太長,請各位大大幫忙看看

來源:互聯網
上載者:User
訂單統計                StartTime = $this->get_microtime();    }     function stop()    {        $this->StopTime = $this->get_microtime();    }     function spent()    {        return round(($this->StopTime - $this->StartTime) * 1000, 1);    } }$runtime= new runtime;$runtime->start();include("conn.php");$to_date=date('Y-m-d');//按封裝地區來統計郵件總數function sale_address_package_statistics($date,$account,$package){$sql="select `number` from `sale_orders` where `account`='$account' and date(`upload_date`) ='$date' GROUP BY `number`";$Result = mysql_query($sql);$number="(`number`='' ";while($rs = mysql_fetch_array($Result)){$number.=" or `number`=".$rs[0];}$number.=")";$sql_order_address="SELECT * FROM `sale_orders` T ";$sql_order_address.="WHERE ".$number;$sql_order_address.="AND `account` ='$account' ";if($package=='MIX'){$sql_order_address.="AND 1<(select count(*) from `sale_orders` where ".$number." AND `account` ='$account' AND `recipient_name`=T.`recipient_name`  AND `ship_address_1`=T.`ship_address_1`) ";}else{$sql_order_address.="AND 1=(select count(*) from `sale_orders` where ".$number." AND `account` ='$account' AND `recipient_name`=T.`recipient_name`  AND `ship_address_1`=T.`ship_address_1`) ";$sql_order_address.="AND `package_area`='$package' ";}$sql_order_address.="GROUP BY `recipient_name` , `ship_address_1`";$result_label = mysql_query($sql_order_address);$order_address=mysql_affected_rows();if(empty($order_address)){return 0;}else{return $order_address;}}?>

訂單統計

PM_AC CO_AC UK_AC UK_FR UK_DE
SZ HK MIX SZ HK MIX SZ HK MIX SZ HK MIX SZ HK MIX
stop(); $runtimea= new runtime;$runtimea->start(); ?>

stop();echo "郵件統計頁面執行時間: ".$runtime->spent()." 毫秒,訂單統計頁面執行時間:".$runtimea->spent()." 毫秒"; ?>


這個頁執行要75秒左右,這還只是顯示一天的資料,實際上我需要顯示一個月的資料


主要是那個查詢語句的問題,以下語句如果查詢的記錄在1700條左右的時候,需要35秒
SELECT * FROM `sale_orders` T
WHERE (`number`='' or `number`=81)
AND 1=(select count(*) from `sale_orders`
where (`number`='' or `number`=81)
AND `recipient_name`=T.`recipient_name`
AND `ship_address_1`=T.`ship_address_1`)
AND `package_area`='SZ'
GROUP BY `recipient_name` , `ship_address_1`


請問怎麼最佳化啊?顯示一天的資料需要75秒,那一個月的資料不是等吃一餐飯還沒顯示完


回複討論(解決方案)

具體情況需具體分析,畢竟不是在做文字遊戲
調試查詢指令應聽從 EXPLAIN 的建議

盡量少用select * ,需要哪個欄位查哪個,還有,加索引

資料表需要最佳化。
可否desc給大家看看呢

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.