简体   繁体   English

MySQL查询正在减慢页面加载

[英]MySQL Query is slowing down page load

I run this query 我运行此查询

        $result1 = mysql_query("SELECT * FROM 'departments'");
        while($row1 = mysql_fetch_assoc($result1))
        {
            $depts = array("Name" => $row1['Name'], "Value" => $row1['Value'], "ID" => $row['CollegeID']);
        }

with three other simple queries, one loads data into profile text boxes, and another loads a drop down. 与其他三个简单查询一起,一个将数据加载到配置文件文本框中,另一个将下拉菜单加载。 Is something wrong with my Query here or my PHP code that could be causing this. 我的此处查询或可能导致此问题的PHP代码有问题。 It only returns about 51 records. 它仅返回约51条记录。 Do you think it could be the server the MySQL server is running on a 2008 Windows Server. 您是否认为它可能是MySQL服务器在2008 Windows Server上运行的服务器。

Maybe you have many columns in departments table. 也许您在Departments表中有很多列。 Try this: 尝试这个:

    $result1 = mysql_query("SELECT Name, Value, CollegeID  FROM 'departments'");
    while($row1 = mysql_fetch_assoc($result1))
    {
        $depts = array("Name" => $row1['Name'], "Value" => $row1['Value'], "ID" => $row['CollegeID']);
    }

You could also try using the OOP mysqli functions rather than the old mysql versions. 您也可以尝试使用OOP mysqli函数而不是旧的mysql版本。

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$mysqli->query("SELECT Name, Value, CollegeID  FROM departments");

Never use * 请勿使用*

$result1 = mysql_query("SELECT Name, Value, CollegeID  FROM 'departments'");
while($row1 = mysql_fetch_assoc($result1))
{
    $depts = array("Name" => $row1['Name'], "Value" => $row1['Value'], "ID" => $row['CollegeID']);
}

and use LIMIT 并使用LIMIT

$result1 = mysql_query("SELECT Name, Value, CollegeID  FROM 'departments' LIMIT 1");
while($row1 = mysql_fetch_assoc($result1))
{
    $depts = array("Name" => $row1['Name'], "Value" => $row1['Value'], "ID" => $row['CollegeID']);
}

it doesn't matter in your case because you're always overwriting your actual $depts variable with new one row 没关系,因为您总是用新的一行覆盖实际的$depts变量

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

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