include/common.file.func.php 和 include/common.redis.func.php 中的 getDataCount() 和 getNewsDataCount()
函数使用了极差的 SQL:
// 对列套函数 → 索引完全失效 → 每次全表扫描 12万+ 行
WHERE DATE_FORMAT(FROM_UNIXTIME(v_addtime),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')
如果一台服务器多个站点在共享数据库同时反复执行此查询,每条耗时 3~11 秒,容易直接压垮 MySQL,造成cpu跟负载100%,修改后不再飘红。
原代码:
function getDataCount($countType){
global $dsql;
$whereStr=" where DATE_FORMAT(FROM_UNIXTIME(v_addtime),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')";
switch ($countType) {
case "all":
$row = $dsql->GetOne("select count(*) as dd From `sea_data`");
$DataCount=$row['dd'];
break;
case "day":
$row = $dsql->GetOne("select count(*) as dd From `sea_data` $whereStr");
$DataCount=$row['dd'];
break;
}
return $DataCount;
}
function getNewsDataCount($countType){
global $dsql;
$whereStr=" where DATE_FORMAT(FROM_UNIXTIME(n_addtime),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')";
switch ($countType) {
case "all":
$row = $dsql->GetOne("select count(*) as dd From `sea_news`");
$DataCount=$row['dd'];
break;
case "day":
$row = $dsql->GetOne("select count(*) as dd From `sea_news` $whereStr");
$DataCount=$row['dd'];
break;
}
return $DataCount;
}
修改为:
function getDataCount($countType){
global $dsql;
$todayStart = strtotime("today");
$todayEnd = $todayStart + 86400;
$whereStr=" where v_addtime >= $todayStart and v_addtime < $todayEnd";
switch ($countType) {
case "all":
$row = $dsql->GetOne("select count(*) as dd From `sea_data`");
$DataCount=$row['dd'];
break;
case "day":
$row = $dsql->GetOne("select count(*) as dd From `sea_data` $whereStr");
$DataCount=$row['dd'];
break;
}
return $DataCount;
}
function getNewsDataCount($countType){
global $dsql;
$todayStart = strtotime("today");
$todayEnd = $todayStart + 86400;
$whereStr=" where n_addtime >= $todayStart and n_addtime < $todayEnd";
switch ($countType) {
case "all":
$row = $dsql->GetOne("select count(*) as dd From `sea_news`");
$DataCount=$row['dd'];
break;
case "day":
$row = $dsql->GetOne("select count(*) as dd From `sea_news` $whereStr");
$DataCount=$row['dd'];
break;
}
return $DataCount;
}
当然,海洋同学,你知道我是半桶水,就是摸索中前进,看有用没。