1
查詢我有一個Drupal 7個網站運行到下列服務器:Drupal的過高查詢時間在Drupal
- 的Debian GNU/Linux的7.1
- 2個Intel多核CPU
- 的4Gb的內存
我正在試驗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/
有沒有一種方法來提高查詢的時間呢?可以做些什麼?這降低了網站的加載速度大大(因爲它不是唯一的慢查詢我有...)
非常感謝
這是一個巨大的查詢來排序;我不能管理它,很抱歉地說。它需要進行優化。您可能應該查看如何使用索引範圍掃描對日期範圍進行查詢。每當你做任何類似'WHERE FUNCTION(column)= value'或類似的東西時,你都會阻止MySQL在查詢中使用索引。 –
'field_date_value [2]'是什麼類型的字段?您永遠不需要在存儲日期的字段上使用STR_TO_DATE。或者,相反,如果你需要使用它,那麼你正在存儲你的日期不正確。如果你正確地存儲它們,你可以更快地進行這些比較。 – miyasudokoro
感謝您的回答。我做了幾次嘗試,如果我刪除左邊的2個連接「LEFT JOIN field_data_field_date field_data_field_date2」和「LEFT JOIN field_data_field_date field_data_field_date3」,並將變量重命名爲where子句,我將其放置到0,04sec ...這更好一些...:D – Yannick