gpt4 book ai didi

mysql - SQL 查询在 "Identical"服务器上变慢

转载 作者:行者123 更新时间:2023-11-30 21:39:45 25 4
gpt4 key购买 nike

我有多个 SQL 查询在一台开发服务器上运行得比另一台开发服务器慢得多(长 2 到 4 倍)。我正在使用 DigitalOcean,两台服务器都是从同一个快照制作的,就硬件和数据而言,它们是相同的。 Dev1 是我优化代码的主要开发服务器,因此数据略有不同。我觉得我可能更改或修改了未在 Dev2 上复制的 mysql 设置,但我找不到任何差异。对我能想到的所有配置文件运行 diffs 并没有得到任何线索。两台服务器都在运行

  • Ubuntu 14.04
  • 64 GB 内存
  • MySql 5.6
  • PHP 5.5

我的后端使用 PHP,在准备具有大量绑定(bind)值的语句时,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/

最佳答案

可能不是您要找的东西,但这些建议可以帮助优化所有服务器上的查询:

首先添加相关索引:

ALTER TABLE `b_actions` ADD INDEX `b_actions_idx_dismiss_id_name_pid` (`user_dismissed`,`user_id`,`action_name`,`pId`);
ALTER TABLE `users` ADD INDEX `users_idx_id_usernam_email_id` (`id`,`username`,`email`,`customer_id`);
ALTER TABLE `temp_a` ADD INDEX `temp_a_idx_id` (`id`);

然后运行此查询转换而不是原始查询。主要变化是我已将 IN 子查询转换为 EXISTS 子查询,因为它通常执行得更好。

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 EXISTS (
SELECT
1
FROM
temp_a
WHERE
t1.pId = temp_a.id
)
ORDER BY
t1.datetime DESC

关于mysql - SQL 查询在 "Identical"服务器上变慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52224679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com