简体   繁体   English

如何使用庞大的MySQL数据库加速PHP脚本

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

I've been working on getting an RSS feed setup and with the help of some people here I got it done. 我一直在努力获得RSS feed设置,在这里的一些人的帮助下,我完成了它。 However, I really need some advice from some of you more experienced coders to show me what needs to be changed to keep the same functionality but speed up the page load. 但是,我真的需要一些经验丰富的编码员提供一些建议,告诉我需要更改哪些内容以保持相同的功能,但会加快页面加载速度。

It looks 30 RSS items and gets the URLs from my MySQL database. 它查找30个RSS项目并从我的MySQL数据库中获取URL。 The problem is that it randomly selects 30 rows out of over 100 million rows in that table. 问题是它在该表中超过1亿行中随机选择30行。 That is what it's supposed to do, but with their being so many rows in the table, it's really slowing down the script and I need help! 这应该是它应该做的,但是由于它们在表中有如此多的行,它确实减慢了脚本的速度,我需要帮助!

<?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>

Thanks in advance for any help that anyone can give! 在此先感谢任何人都可以给予的帮助!

You can still select all 30 at once. 您仍然可以一次选择所有30个。 It shouldn't be that slow to get 30 records. 获得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).")";

Limit has to do table scans, so what you want to do is use indexes to your advantage. 限制必须进行表扫描,因此您要做的就是使用索引。 So first, let's add an autoincrement ID field to the table named "id". 首先,让我们在名为“id”的表中添加一个自动增量ID字段。

Then, 然后,

<?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
?>

May i suggest a more robust approach. 我可以建议采用更强大的方法。

  1. You have to take in account that the number you go look for may not exist 您必须考虑到您要查找的号码可能不存在
  2. Only using one MySql query is often faster 仅使用一个MySql查询通常更快

Base on url1 and url2 基于url1url2

You can have this php code instead : 您可以使用此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
     }
 }

?>

The values mysql_host,mysql_user,mysql_password,my_database should be replaced by your connection. mysql_host,mysql_user,mysql_password,my_database的值应替换为您的连接。

As long as you have 30 row in your title table your are ok. 只要你的标题表中有30行你就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM