У меня несколько SQL-запросов, которые выполняются намного медленнее (в 2-4 раза) на одном сервере разработки по сравнению с другим. Я использую DigitalOcean, и оба сервера были сделаны из одного и того же снимка, с точки зрения оборудования и данных они идентичны. Dev1 был моим основным сервером разработки, на котором я оптимизировал код, поэтому данные немного отличаются. Я чувствую, что мог изменить или изменить настройку mysql, которая не реплицировалась на Dev2 , но я не могу найти никаких отличий. Запуск diff на всех конфигурационных файлах, о которых я мог подумать, не дал никаких подсказок. Оба сервера работают
- Убунуту 14.04
- 64 ГБ ОЗУ
- MySql 5,6
- PHP 5.5
Мой бэкэнд использует PHP, и в PDO есть ошибка при подготовке оператора с большим количеством связанных значений. Чтобы обойти это, я создавал временные таблицы и использовал их в качестве подзапросов для моих операторов where. Все они были запущены в MySql CLI, чтобы исключить любые проблемы с PHP.
CREATE TEMPORARY TABLE IF NOT EXISTS temp_a LIKE a;
INSERT INTO temp_a SELECT * FROM a WHERE id in (1,2,3,4,8,30,31,128,129,130,197,198,199,200,201,204,206,207,208,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,271,273,284,641,4691,5313,6845,5,7,262,6,528,817,818,819,820,821,9,628,5178;
SELECT t1.pId, t1.*, t2.username, t2.email, t2.customer_id
FROM b_actions t1
JOIN users t2
ON t1.user_id = t2.id
WHERE t1.action_name NOT IN ('action1', 'action2', 'action3') AND t1.user_dismissed = 0
AND t1.pId IN (SELECT id FROM temp_a)
ORDER BY datetime DESC`);
Таблица b_actions схема
CREATE TABLE `b_actions` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`datetime` datetime NOT NULL,
`user_id` int(11) DEFAULT NULL,
`pId` int(11) NOT NULL,
`action_name` varchar(255) CHARACTER SET latin1 DEFAULT NULL,
`data` mediumtext COLLATE utf8_unicode_ci,
`schedule_type` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`schedule_datetime` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`action_state` int(11) NOT NULL,
`user_dismissed` smallint(1) NOT NULL,
`message` varchar(500) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_on` datetime DEFAULT CURRENT_TIMESTAMP,
`updated_on` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
PRIMARY KEY (`id`),
KEY `pId` (`pId`),
KEY `datetime` (`datetime`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Dev1: быстрый запрос (5377 строк в наборе (0,79 с)) - Объяснение:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: temp_a
type: index
possible_keys: PRIMARY,id
key: id
key_len: 4
ref: NULL
rows: 5889
Extra: Using index; Using temporary; Using filesort
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: t1
type: ref
possible_keys: pId,user_id
key: pId
key_len: 4
ref: db.temp_a.id
rows: 15
Extra: Using index condition; Using where
*************************** 3. row ***************************
id: 1
select_type: SIMPLE
table: t2
type: eq_ref
possible_keys: id
key: id
key_len: 4
ref: db.t1.user_id
rows: 1
Extra: Using where
Dev2: более медленный запрос (5386 строк в наборе (1,31 сек)) - Объяснить
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: temp_a
type: index
possible_keys: PRIMARY,id
key: id
key_len: 4
ref: NULL
rows: 8866
Extra: Using index; Using temporary; Using filesort
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: t1
type: ref
possible_keys: pId,user_id
key: pId
key_len: 4
ref: db.temp_a.id
rows: 23
Extra: Using index condition; Using where
*************************** 3. row ***************************
id: 1
select_type: SIMPLE
table: t2
type: eq_ref
possible_keys: id
key: id
key_len: 4
ref: db.t1.user_id
rows: 1
Extra: Using where
mysql / 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 = 4G
max_allowed_packet = 16M
thread_stack = 192K
thread_cache_size = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover = BACKUP
max_connections = 2000
# table_cache = 1024
#thread_concurrency = 10
#
# * Query Cache Configuration
#
query_cache_limit = 10M
query_cache_size = 256M
#
# * 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 log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
#log_slow_queries = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#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
slow-query-log = 1
slow-query-log-file = /var/log/mysql/localhost-slow.log
long_query_time = 1
#log-queries-not-using-indexes
[mysqldump]
quick
quote-names
max_allowed_packet = 16M
[mysql]
#no-auto-rehash # faster start of mysql but no tab completition
[isamchk]
key_buffer = 16M
#
# * 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/