mysql php查询有时非常快,有时非常非常慢

时间:2013-02-28 21:59:51

标签: php mysql sql

我认为是一个复杂的php mysql查询,从各种drupal表中提取数据,并允许我以json格式通过php输出结果。有时这个查询速度非常快,有时速度很慢。如果您转到http://www.serviidb.com/content/data/view/api_access,您可以看到实际的API响应时间。数据库中的内容一直在增长,随着数据库变大,这个问题只会越来越严重。

抱歉忘了添加服务器信息:

Server: Localhost via UNIX socket
Server version: 5.5.29-0ubuntu0.12.04.1-log
Protocol version: 10

我如何调用查询的示例。

mysql_query($sqlApiAccess) or die('Error, insert query failed');

如果有人想看我正在使用的PHP,我会发布它。我已经编程多年了,但是对PHP来说是新手,所以我可能会做很长的事情。

我感谢任何人可以给我的帮助,指导或建议。

以下是查询:

SELECT DISTINCT
   node.title AS "name",
   regions.region AS region,
   field_data_field_media_url.field_media_url_value AS url,
   media_type.media_type AS mediaType,
   repositoryType.repositoryType AS resourceType,
   plugins.plugin,
   media_resource_language.language,
   node.nid,
   resolution.resolution,
   (field_data_field_quality.field_quality_rating / 20) AS quality,
   (field_data_field_reliability.field_reliability_rating / 20)
      AS reliability,
   field_data_field_installs.field_installs_value AS installCount
FROM node
   LEFT JOIN field_data_field_region
      ON field_data_field_region.revision_id = node.vid
   LEFT JOIN regions
      ON regions.tid = field_data_field_region.field_region_tid
   LEFT JOIN field_data_field_media_url
      ON field_data_field_media_url.revision_id = node.vid
   LEFT JOIN field_data_field_required_plugin
      ON field_data_field_required_plugin.revision_id = node.vid
   LEFT JOIN field_data_field_source
      ON field_data_field_source.revision_id = node.vid
   LEFT JOIN repositoryType
      ON repositoryType.tid = field_data_field_source.field_source_tid
   LEFT JOIN plugins
      ON plugins.tid =
            field_data_field_required_plugin.field_required_plugin_tid
   LEFT JOIN field_data_field_media_type
      ON field_data_field_media_type.revision_id = node.vid
   LEFT JOIN media_type
      ON media_type.tid =
            field_data_field_media_type.field_media_type_tid
   LEFT JOIN field_data_field_language
      ON field_data_field_language.revision_id = node.vid
   LEFT JOIN media_resource_language
      ON media_resource_language.tid =
            field_data_field_language.field_language_tid
   LEFT JOIN field_data_field_resolution
      ON field_data_field_resolution.revision_id = node.vid
   LEFT JOIN resolution
      ON resolution.tid =
            field_data_field_resolution.field_resolution_tid
   LEFT JOIN field_data_field_quality
      ON field_data_field_quality.revision_id = node.vid
   LEFT JOIN field_data_field_reliability
      ON field_data_field_reliability.revision_id = node.vid
   LEFT JOIN field_data_field_installs
      ON field_data_field_installs.revision_id = node.vid
WHERE     node.status <> 0
   AND node.type = "media"
   AND (node.title LIKE '%%' OR plugins.plugin LIKE '%%')
   AND node.title LIKE '%%'
   AND plugins.plugin LIKE '%%'
   AND media_resource_language.language LIKE '%%'
   AND (regions.region = "worldwide" OR regions.region LIKE '%%')
   AND media_type.media_type = "video"

我读到如果使用“Like”,它会导致查询运行缓慢,所以我想看几种方法来删除 Like 部分,除非实际变量中有信息。以下是我目前正在开发where部分的方式。

/** setups up sql variable for searches.
     *
     */
    $sqlregion        = "";
    $sqltitle         = "";


            $sqlregion        = 'and (regions.region = "worldwide" or ';
            $sqlregion        = $sqlregion . 'regions.region like ';
            $sqlregion        = $sqlregion . "'%";
            $sqlregion        = $sqlregion . $region;
            $sqlregion        = $sqlregion . "%')";

    $slqLanguage      = "and media_resource_language.language like '%" . $language . "%'";
    $sqltitleOrPlugin = "and (node.title like '%" . $titleOrPlugin . "%' or plugins.plugin like '%" . $titleOrPlugin . "%')";

    $sqlPlugin        = "and plugins.plugin like '%" . $plugin . "%'";

    $sqltitle         = "and node.title like '%" . $title . "%'";

Where语句设置如下。

WHERE node.status<>0 and node.type="media" . $sqltitleOrPlugin . $sqltitle .$sqlPlugin . $slqLanguage . $sqlregion;

where变量实际上是第三方应用程序填充以缩小数据范围的参数。喜欢这样语言= en&amp; region = us&amp; title =&amp; client = test

WHERE node.status<>0 
    and node.type="media"
    and (node.title like '%%' or plugins.plugin  like '%%')
    and node.title like '%%'
    and plugins.plugin like '%%'
    and media_resource_language.language like '%%'
    and (regions.region = "worldwide" or regions.region like '%us%') 
    and media_type.media_type ="video"

1 个答案:

答案 0 :(得分:0)

MySQL完全可以缓存您的查询结果。首先,有通用MySQL query cache。使用InnoDB存储引擎时,还有InnoDB buffer pool。这两个缓存都能够在内存中存储SELECT查询及其结果,从而可以快速响应。

但是,当更新其中一个受影响的表中的数据时,通常会刷新这些缓存(这样做是为了确保MySQL不会提供过时的结果)。此外,当达到最大高速缓存大小时,通常从高速缓存中清除不常使用的结果。

您的查询加入了很多表并包含DISTINCT语句,因此它可能会导致相当多的IO;此外,MySQL可能需要使用临时表来构建结果(这些可能偶尔写入磁盘,导致更多的IO)。因此,构建第一个结果集可能需要相当长的时间,而后续请求可以快速从查询缓存中回答。当数据更改或缓冲区大小耗尽时,缓存的结果将从缓存中刷新,下一次调用将再次变慢。

您可以尝试调整缓冲池配置参数(例如通过增加innodb_buffer_pool_size,尽管我不是真正的专家),以便将结果保留在缓存中更长的时间