最新
版块
发主题
搜索
我的

海洋同学,我发两个文件common.file.func.php跟common.redis.func.php你参考下。

fsxoyo 2天前 69

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;

}

当然,海洋同学,你知道我是半桶水,就是摸索中前进,看有用没。

上传的附件:
  • seacms_fixed.zip
    · 上传时间:2026-03-30 | 下载次数:18 次 | 文件大小:232.19K
最新回复 (1)
  • 海洋 1天前
    0 引用 2
    收到,给大佬磕头!
返回