簡體   English   中英

如何使用龐大的MySQL數據庫加速PHP腳本

[英]How to Speed Up PHP Script With Huge MySQL Database

我一直在努力獲得RSS feed設置,在這里的一些人的幫助下,我完成了它。 但是,我真的需要一些經驗豐富的編碼員提供一些建議,告訴我需要更改哪些內容以保持相同的功能,但會加快頁面加載速度。

它查找30個RSS項目並從我的MySQL數據庫中獲取URL。 問題是它在該表中超過1億行中隨機選擇30行。 這應該是它應該做的,但是由於它們在表中有如此多的行,它確實減慢了腳本的速度,我需要幫助!

<?php header("Content-type: text/xml"); ?>
<?php echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; ?>
<?php include('directory/database.php'); ?>
<rss version="2.0">
<channel>
  <title>Website Reviews</title>
  <link>http://www.mywebsite.com</link>
  <description>Professional Services</description>
  <pubDate><?echo date('Y/m/d H:i:s');?></pubDate>

<?php
    foreach( range( 1, 30 ) as $i ):
$number = mt_rand( 1, 141754641 );
$query="SELECT * FROM `list` LIMIT $number , 1";
$result = mysql_query($query);
if($result == false)
{
   user_error("Query failed: " . mysql_error() . "<br />\n$query");
}
elseif(mysql_num_rows($result) == 0)
{
   echo "<p>Sorry, we're updating this section of our website right now!</p>\n";
}
else
{
   while($query_row = mysql_fetch_assoc($result))
   {
      foreach($query_row as $key => $domain)
      {
         echo "$value";
      }
   }
}  
?>
<item>
    <title><?php echo $domain; ?> REVIEW</title>
    <pubDate><?echo date('Y/m/d H:i:s');?></pubDate>
    <link>http://www.mywebsite.com/review/<?php echo $domain; ?></link>
    <description>Looking for a review on <?php echo $domain; ?>?  We've got it!</description>
</item>
<?php endforeach; ?>

</channel>
</rss>

在此先感謝任何人都可以給予的幫助!

您仍然可以一次選擇所有30個。 獲得30條記錄的速度應該不會那么慢。

$numbers=array();
foreach( range( 1, 30 ) as $i ):
    $numbers[] = mt_rand( 1, 141754641 );
endforeach;

$query="SELECT * FROM `list` WHERE `whatever_primary_key_is` IN (".implode(',', $numbers).")";

限制必須進行表掃描,因此您要做的就是使用索引。 首先,讓我們在名為“id”的表中添加一個自動增量ID字段。

然后,

<?php
$result = array();
$maxRow = mysql_fetch_assoc(mysql_query("SHOW TABLE STATUS LIKE 'list';"));
$max = $maxRow["Auto_increment"];
$minRow = mysql_fetch_assoc(mysql_query("SELECT id FROM 'list' LIMIT 1;"));
$min = $minRow["id"];
while (count($result) < 30) {
    $ids = array();
    while (count($ids) < 100) {
        $id = mt_rand($min, $max);
        $ids[$id] = 1;
    }
    $res = mysql_query("SELECT * from 'list' WHERE id IN (" . join(',', array_keys($ids)) . ") LIMIT 30");
    while (($row = mysql_fetch_assoc($result)) && (count($result) < 30)) {
        $result[] = array( ... ); // stuff results here
    }
}

// output
?>

我可以建議采用更強大的方法。

  1. 您必須考慮到您要查找的號碼可能不存在
  2. 僅使用一個MySql查詢通常更快

基於url1url2

您可以使用此PHP代碼:

<?php
// Connecting, selecting database
   $link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error());
   mysql_select_db('my_database') or die('Could not select database');

$query = "SELECT `url`
          FROM `liste`
          ORDER BY RAND()
         LIMIT 30" ;

$result = mysql_query($query);

if($result == false)
{
   user_error("Query failed: " . mysql_error() . "<br />\n$query");
}
elseif(mysql_num_rows($result) == 0)
{
   echo "<p>Sorry, we're updating this section of our website right now!</p>\n";
}
else
{  $query_row = array();
   while($query_row = mysql_fetch_assoc($result))
    { 
        echo $query_row['url']; // no need to do the extra foreach
     }
 }

?>

mysql_host,mysql_user,mysql_password,my_database的值應替換為您的連接。

只要你的標題表中有30行你就可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM