gpt4 book ai didi

PHP/MYSQL排名代码导致PHP/MYSQL加载页面缓慢

转载 作者:行者123 更新时间:2023-11-29 06:07:02 24 4
gpt4 key购买 nike

我有一个运行 #-tag 趋势的测试页面,该页面存储在我的数据库中。该页面需要很长时间才能加载(最多 2 分钟)才能显示内容,该页面仅使用了一项功能

网址:http://www.sudanesetweeps.com/trendingtopics.php

如何调整语句以便在更短的时间内加载页面?

这是我的代码:

<?php 

require_once("dbconnect.php");
require_once("lib_isarabic.php");

$query = "SELECT COUNT( * ) cnt, hashtags
FROM tweets
WHERE tweeted_at >= DATE_SUB( NOW( ) , INTERVAL 2 DAY )
AND hashtags != ''
GROUP BY hashtags
ORDER BY cnt DESC LIMIT 100";

$res = mysql_query($query);

while($row = mysql_fetch_assoc($res) ) {

$count = $row['cnt'];
$hashtags = explode( " ", $row['hashtags'] );

foreach($hashtags as $hashtag ) {
if( strtolower($hashtag) != 'sudan' && strtolower($hashtag) != 'new' && strtolower($hashtag) != 'new' )
if( is_arabic($hashtag) )
$topics_ara[strtolower( trim($hashtag) )] += $count;
else
$topics_eng[strtolower( trim($hashtag) )] += $count;

}
}

array_multisort($topics_ara, SORT_DESC);
array_multisort($topics_eng, SORT_DESC);

$index = 0;
foreach($topics_eng as $key=>$value) {

$query = "SELECT count(*) cnt FROM (
SELECT count(*), tweeted_by FROM tweets
WHERE hashtags like '%$key%'
AND tweeted_at >= DATE_SUB( NOW( ) , INTERVAL 2 DAY )
GROUP BY tweeted_by
) AS T";


/* $query = "
SELECT count(*) FROM tweets
WHERE hashtags like '%$key%'
AND tweeted_at > DATE_SUB( NOW( ) , INTERVAL 1 DAY ) ";
*/

$res = mysql_query($query);
$row = mysql_fetch_assoc($res);

if($row['cnt'] > 1) {

$index++;
if($key != "" ) {
$trending_eng[$key] = $value;
}
}

if($index > 30)
break;
}


$index = 0;
foreach($topics_ara as $key=>$value) {

$query = "SELECT count(*) cnt FROM (
SELECT count(*), tweeted_by FROM tweets
WHERE hashtags like '%$key%'
AND tweeted_at >= DATE_SUB( NOW( ) , INTERVAL 2 DAY )
GROUP BY tweeted_by
) AS T";
$res = mysql_query($query);
$row = mysql_fetch_assoc($res);

if($row['cnt'] > 1) {

$index++;
if($key != "" ) {
$trending_ara[$key] = $value;
}
}

if($index > 30)
break;
}


//var_dump($trending_eng) ;
//var_dump($trending_ara) ;

?>

最佳答案

抱歉,您的数据模型有缺陷。

您不对推文进行标准化,而是对主题标签进行全文搜索(通过“%$key%”等主题标签),这意味着时间间隔内所有推文的完整文本必须运行一个 CPU 密集型进程 - 不仅是一次,而是通过两个 foreach() 循环,每个循环 30 次迭代。

因此您进行了 60 次全文扫描 - 祝您好运。

正确的方法是在收到推文时对其进行标准化,拆分主题标签并创建一个类似于hashtag | 的表格。用户 |计数

关于PHP/MYSQL排名代码导致PHP/MYSQL加载页面缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11034965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com