简体   繁体   中英

PHP MySQL Query Returning Rows Very Slow

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?

<?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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