平民 website
performance
TonyQ
What's performance
Fast Feedback.
When to tune performance
Design ?
Implement ?
QA ?
Production ?
Maintain ?
Scenario
-
Traffic increased
- New service released
Profiling,
Profiling,
PROFILING
Never write code
for performance ?
NEVER WRITE CODE
FOR PERFORMANCE
BEFORE PROFILING
Slow query log
Ex. MySQL
[mysqld]
long_query_time=1
log-slow-queries=/var/log/mysql/log-slow-queries.log
$GLOBAL_TIME_START = microtime(TRUE);
$GLOBAL_TIME_LAST = microtime(TRUE);
function watch($MSG){
global $GLOBAL_TIME_START;
global $GLOBAL_TIME_LAST;
$now = MICROTIME(TRUE);
echo(",during:" .round($now - $GLOBAL_TIME_LAST,4) .",past:".round($now - $GLOBAL_TIME_START,4)).",DETAIL: $MSG
";
$GLOBAL_TIME_LAST = $now;
}
front-end
function stopwatch(cb){
var start =
new Date().getTime();
var ret = cb();
var end = new Date().getTime();
log(end-start );
return ret;
}
Order by rand()
ex. mysql (with 91693 records)
SELECT *
FROM `KeyWords`
ORDER BY RAND( )
LIMIT 4
//查詢花費 1.7222 秒
youtube
facebook
email(smtp)
.
.
.
And others we didn't find
MEthod
- Refine Process
- Cache
- Index
- Separate Long Operations
Refine process
KNOW YOUR PROCESS First.
ex. mysql (with 91693 records)
SELECT *
from KeyWords
ORDER BY RAND( )
LIMIT 4
//查詢花費 1.7222 秒
vs
SELECT a . * FROM (
SELECT a.No FROM KeyWords a ORDER BY RAND( ) LIMIT 4
) AS a2 INNER JOIN KeyWords a ON a.No = a2.No
//查詢花費 0.1377 秒
Cache
Counter cache
select sum(nums) from orderitems where UserID = 5;
vs
select shopNums from user where UserID = 5;
Cache
Page cache
Index "NEWS" / "HOT" Info ,
NOT REALLY required to be so HOT.
10 mins cache to save your database.
Cache
Cache third-party info if possible.
DB Index
好的 Index 帶你上天堂
沒做 Index 帶你住套房
search index
Use search engine to do the search job.
- Google custom search.
- Lucense search engine.
Email
Don't send mail during a form submit.
Facebook api
getFriendList...etc
Summary
Profile your system!
Finding issue is way more difficult than fixing it.
special thanks
資料使用授權:
Mamibuy 華人第一嬰兒用品推薦網站
5945
JSDC.tw
五月中!
JSDC rocks! yep!
積極尋找贊助中!歡迎來信!
(hq@jsdc.tw)