Drupal在drupal中查询的查询时间太长

时间:2014-01-15 21:11:22

标签: mysql database-performance

我有一个drupal 7站点运行到以下服务器上:

  • Debian GNU / Linux 7.1
  • 2英特尔核心CPU
  • 4Gb的RAM

我正在试验SQL查询的时间太长以及从部署的站点触发mysql查询时CPU的负载很高。

例如,以下查询平均需要20秒以上才能提供数据(直接在mysql控制台中运行):

    SELECT DISTINCT node.nid AS nid, domain_source.domain_id AS domain_source_domain_id, field_data_field_date.delta AS field_data_field_date_delta, field_data_field_date.language AS field_data_field_date_language, field_data_field_date.bundle AS field_data_field_date_bundle, field_data_field_date.field_date_value AS field_data_field_date_field_date_value, field_data_field_date.field_date_value2 AS field_data_field_date_field_date_value2, field_data_field_date.field_date_rrule AS field_data_field_date_field_date_rrule, node.title AS node_title, node.language AS node_language, location.name AS location_name, 'node' AS field_data_field_date_node_entity_type, 'node' AS field_data_field_lieux_node_entity_type
FROM 
node node
LEFT JOIN field_data_field_date field_data_field_date ON node.nid = field_data_field_date.entity_id AND (field_data_field_date.entity_type = 'node' AND field_data_field_date.deleted = '0')
LEFT JOIN domain_source domain_source ON node.nid = domain_source.nid
LEFT JOIN field_data_field_date field_data_field_date2 ON node.nid = field_data_field_date2.entity_id AND (field_data_field_date2.entity_type = 'node' AND field_data_field_date2.deleted = '0')
LEFT JOIN field_data_field_date field_data_field_date3 ON node.nid = field_data_field_date3.entity_id AND (field_data_field_date3.entity_type = 'node' AND field_data_field_date3.deleted = '0')
LEFT JOIN location_instance location_instance ON node.vid = location_instance.vid
LEFT JOIN location location ON location_instance.lid = location.lid
INNER JOIN node_access na ON na.nid = node.nid
WHERE (( (node.status = '1') AND (node.type IN  ('activite', 'evenements')) AND (domain_source.domain_id NOT IN  ('6', '-5')) AND (DATE_FORMAT(ADDTIME(STR_TO_DATE(field_data_field_date2.field_date_value, '%Y-%m-%dT%T'), SEC_TO_TIME(3600)), '%Y-%m-%d') >= '2014-01-15' OR DATE_FORMAT(ADDTIME(STR_TO_DATE(field_data_field_date3.field_date_value2, '%Y-%m-%dT%T'), SEC_TO_TIME(3600)), '%Y-%m-%d') >= '2014-01-15') )AND( (DATE_FORMAT(ADDTIME(STR_TO_DATE(field_data_field_date.field_date_value, '%Y-%m-%dT%T'), SEC_TO_TIME(3600)), '%Y-%m-%d') >= '2014-01-15') OR (DATE_FORMAT(ADDTIME(STR_TO_DATE(field_data_field_date.field_date_value2, '%Y-%m-%dT%T'), SEC_TO_TIME(3600)), '%Y-%m-%d') >= '2014-01-15') ))AND(( (na.gid = '0') AND (na.realm = 'all') )OR( (na.gid = '0') AND (na.realm = 'domain_site') )OR( (na.gid = '6') AND (na.realm = 'domain_id') )OR( (na.gid = '1') AND (na.realm = 'view_unpublished_activite_content') )OR( (na.gid = '1') AND (na.realm = 'view_unpublished_image_d_ent_te_content') ))AND (na.grant_view >= '1') 
ORDER BY field_data_field_date_field_date_value ASC
LIMIT 5 OFFSET 0;

节点表的大小是< 1000行以及field_data_date_value一个。

my.cnf的配置如下:

#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
[client]
port            = 3306
socket          = /var/run/mysqld/mysqld.sock

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

# This was formally known as [safe_mysqld]. Both versions are currently parsed.
[mysqld_safe]
socket          = /var/run/mysqld/mysqld.sock
nice            = 0

[mysqld]
#
# * Basic Settings
#
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address            = 127.0.0.1
#
# * Fine Tuning
#
key_buffer              = 128M
max_allowed_packet      = 256M
thread_stack            = 2M
thread_cache_size       = 8
join_buffer_size        = 2M

# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover         = BACKUP
max_connections        = 10
table_cache            = 1028
table_definition_cache  =1028
#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit       = 8M
query_cache_size        = 128M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
general_log_file        = /var/log/mysql/mysql.log
general_log             = 1
#
# Error logging goes to syslog due to /etc/mysql/conf.d/mysqld_safe_syslog.cnf.
#
# Here you can see queries with especially long duration
log_slow_queries        = /var/log/mysql/mysql-slow.log
long_query_time = 1
log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id              = 1
#log_bin                        = /var/log/mysql/mysql-bin.log
expire_logs_days        = 10
max_binlog_size         = 100M
#binlog_do_db           = include_database_name
#binlog_ignore_db       = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem
innodb_buffer_pool_size=512M
innodb_log_file_size=256M
innodb_log_buffer_size=5M
innodb_flush_log_at_trx_commit=2
innodb_thread_concurrency=8
innodb_flush_method=O_DIRECT


[mysqldump]
quick
quote-names
max_allowed_packet      = 64M

[mysql]
#no-auto-rehash # faster start of mysql but no tab completition

[isamchk]
key_buffer              = 64M

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/

有没有办法增加查询时间?可以做些什么?这极大地降低了网站加载速度(因为它不是我唯一的慢查询...)

非常感谢

1 个答案:

答案 0 :(得分:0)

假设这是一个Views查询,请尝试在Views UI中启用缓存,看看它有何帮助。