dongxi8993 2015-08-20 20:25
浏览 98
已采纳

PHP-MYSQL SELECT中查询时间长

I have a PHP script which is sending queries to an Amazon RDS instance using the mysqli method. I'm noticing the below code is taking about a minute to execute. I wanted to see where it was getting hung up.

The table is very large - over 30 million rows. It is about 8GB according to phpMyAdmin. It is running on a db.r3.large RDS instance in the same availability zone and area as the webserver. I figure db.r3.large is overkill for this but wanted to make sure it wasn't an issue.

My script does a search on usernames (whole or partial) and returns matches to a jQuery frontend. Nothing is timing out - the client browser holds on "waiting for [sitename]..." then returns the timing info as well as the result. Results are generally in the vicinity of a dozen to a couple hundred matched rows.

Is the long execution time just due to the size of the database? Am I retrieving and processing the matches correctly?

When I run the query manually, phpMyAdmin makes my browser wait about the same time (a minute or so) with the yellow "Loading" box then returns the same matches, along with "Showing rows 0 - 8 (9 total, Query took 53.1656 sec)".

Here is my code:

$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
$output = array();

if (mysqli_connect_errno()) {
  printf("Connect failed: %s
", mysqli_connect_error());
  exit();
}

echo "Connected at " . getCurrentTime() . "<br><br>";

if ($result = $mysqli->query("SELECT * FROM tablename WHERE last_name LIKE \"%$query%\"")) {

echo "Loaded result at " . getCurrentTime() . "<br><br>";

$selected = $result->num_rows;

echo "Results ready at " . getCurrentTime() . "<br><br>";

while($row = $result->fetch_array(MYSQL_ASSOC)) {
  $output[] = $row;

  echo "Loaded into array at " . getCurrentTime() . "<br><br>";

/* close result set */
$result->close();

echo "Closed result at " . getCurrentTime() . "<br><br>";

}

} else {
  echo "No result at " . getCurrentTime() . "<br><br>";
}

/* close connection */
$mysqli->close();

echo "Closed mysqli at " . getCurrentTime() . "<br><br>";

Here is what my script is outputting:

>Started at Thu Aug 20 19:56:08 2015
>
>Connected at Thu Aug 20 19:56:08 2015
>
>Loaded result at Thu Aug 20 19:57:01 2015
>
>Results ready at Thu Aug 20 19:57:01 2015
>
>Loaded into array at Thu Aug 20 19:57:01 2015
>
>Closed result at Thu Aug 20 19:57:01 2015
>
>Closed mysqli at Thu Aug 20 19:57:01 2015

(The script then returns JSON encoded-object of results).

I have access to the RDS console and phpMyAdmin for troubleshooting.

  • 写回答

3条回答 默认 最新

  • duanjiwang2927 2015-08-20 20:33
    关注

    Your query is running long because it is not using indexes because of the wildcard and LIKE comparison.

    LIKE "%$query%"
    

    Read more here: http://dev.mysql.com/doc/refman/5.6/en/index-btree-hash.html

    If it is acceptable you may change your query to

    LIKE "$query%"
    

    Although this will produce different results it will (at least it should) create a much quicker query.

    Wildcards are far from ideal!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?