PHP MySQL查询返回行非常慢

时间:2022-09-19 19:06:37

I am having a problem with a MySQL query. When ever I run this query it takes over 10 seconds to return the rows. However if I change my limit to 29 it returns it in less than 1 second. My Question is my implementation and query in good shape, or am I making a mistake here that would cause this issue?

我遇到了MySQL查询的问题。当我运行此查询时,返回行需要10秒以上。但是,如果我将限制更改为29,则会在不到1秒的时间内将其返回。我的问题是我的实施和查询状况良好,或者我在这里犯了一个会导致这个问题的错误?

<?

try
        {
            $con = mysql_connect("XXX.XXX.XXX.XXX","XXX","XXX");
            if (!$con)
                {
                    die('Could not connect: ' . mysql_error());
                }

            mysql_select_db("bis_co", $con);

            $query = "SELECT Name, Value FROM `bis_co`.`departments` LIMIT 31"; 

            $result = mysql_query($query) or die(mysql_error());

            $row = mysql_fetch_array($result);
            while($row = mysql_fetch_assoc($result)){
                echo $row['Name'] . "<br />";
            }


        mysql_close($con);
        }
    catch(Exception $e)
        {
            echo $e;
        }
?>

1 个解决方案

#1


3  

You could try getting rid of the

你可以尝试摆脱

$row = mysql_fetch_array($result)

statement. The if statement will take care of returning the array. You could also change the query string to

声明。 if语句将负责返回数组。您还可以将查询字符串更改为

"SELECT Name, Value FROM `departments` LIMIT 31" 

since you're already setting the database name in the mysql_select_db statement. Though, none of these should be causing your issues. It seems like it's a table issue. Have you tried it with mysqli and prepared statements?

因为您已经在mysql_select_db语句中设置了数据库名称。但是,这些都不会导致您的问题。这似乎是一个表格问题。你有没有尝试过mysqli和准备好的声明?

#1


3  

You could try getting rid of the

你可以尝试摆脱

$row = mysql_fetch_array($result)

statement. The if statement will take care of returning the array. You could also change the query string to

声明。 if语句将负责返回数组。您还可以将查询字符串更改为

"SELECT Name, Value FROM `departments` LIMIT 31" 

since you're already setting the database name in the mysql_select_db statement. Though, none of these should be causing your issues. It seems like it's a table issue. Have you tried it with mysqli and prepared statements?

因为您已经在mysql_select_db语句中设置了数据库名称。但是,这些都不会导致您的问题。这似乎是一个表格问题。你有没有尝试过mysqli和准备好的声明?