MySql更新和删除极慢

标签 mysql database-performance

我在 my.cnf 中启用了 Slow_query_log。

日志中没有选择查询。它们都是更新、删除和插入,在非常小的表上需要几秒钟的时间。

以下是使用 dumpslow 分析的慢速查询日志的几个示例:

Count: 33  Time=2.96s (97s)  Lock=0.00s (0s)  Rows=0.0 (0), dbuser[dbuser]@localhost
  UPDATE `users` SET `lockout_time` = NULL WHERE `users`.`id` = N

Count: 116  Time=2.82s (327s)  Lock=0.00s (0s)  Rows=0.0 (0), dbuser[dbuser]@localhost
  DELETE FROM `client_searches` WHERE `client_searches`.`organisation_id` = N AND (session_id = N )

请注意,表“users”和“client_searches”是非常小的 INNODB 表(分别为 494 行和 206 行)

我不知道我是否在 my.cnf 或其他内容中犯了一个完整的错误(发布在下面)。服务器是Ubuntu 14.04 VPS,内存2Gb。我正在尝试找出如何调试这个问题。

[client]
port        = 3306
socket      = /var/run/mysqld/mysqld.sock

[mysqld_safe]
socket      = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqld]
bind-address = 127.0.0.1
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

bind-address        = 127.0.0.1

key_buffer      = 500K
max_allowed_packet  = 16M
thread_stack        = 192K
thread_cache_size       = 8
myisam-recover         = BACKUP
max_connections        = 40

table_open_cache        = 800
table_definition_cache = 400

query_cache_limit   = 1M
query_cache_size        = 64M

log_error = /var/log/mysql/error.log
log_slow_queries    = /var/log/mysql/mysql-slow.log
long_query_time = 1

expire_logs_days    = 10
max_binlog_size         = 100M

innodb_buffer_pool_size = 600M

tmp_table_size = 32M
max_heap_table_size = 32M


[mysqldump]
quick
quote-names
max_allowed_packet  = 16M

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

[isamchk]
key_buffer      = 16M

!includedir /etc/mysql/conf.d/

客户端搜索的表描述约 300 行,但在用户执行搜索时定期删除并重新插入。始终出现在插入时间为几秒的慢速查询中。

CREATE TABLE `client_searches` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `middle_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `last_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `known_as_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `gender` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `date_of_birth` date DEFAULT NULL,
  `ethnicity_id` int(11) DEFAULT NULL,
  `iwi_id` int(11) DEFAULT NULL,
  `title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `client_number` int(11) DEFAULT NULL,
  `client_status` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `programme_id` int(11) DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL,
  `created_on` date DEFAULT NULL,
  `updated_on` date DEFAULT NULL,
  `entry_date` date DEFAULT NULL,
  `exit_date` date DEFAULT NULL,
  `session_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `relation_last_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `relation_first_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `client_age_from` int(11) DEFAULT NULL,
  `client_age_to` int(11) DEFAULT NULL,
  `entry_date_from` date DEFAULT NULL,
  `entry_date_to` date DEFAULT NULL,
  `suburb_id` int(11) DEFAULT NULL,
  `school_id` int(11) DEFAULT NULL,
  `issue_id` int(11) DEFAULT NULL,
  `exit_date_from` date DEFAULT NULL,
  `exit_date_to` date DEFAULT NULL,
  `referral_source_id` int(11) DEFAULT NULL,
  `sort_order` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `activation_date_from` date DEFAULT NULL,
  `activation_date_to` date DEFAULT NULL,
  `assessment_date_from` date DEFAULT NULL,
  `assessment_date_to` date DEFAULT NULL,
  `is_assessed` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `drafts_exist` tinyint(1) DEFAULT '0',
  `form_process` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `form_process_state` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `exclude_client_status` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `signed_off_by` int(11) DEFAULT NULL,
  `not_signed_off` tinyint(1) DEFAULT '0',
  `no_contact_weeks` int(11) DEFAULT NULL,
  `active_weeks` int(11) DEFAULT NULL,
  `waitlist_weeks` int(11) DEFAULT NULL,
  `breakdown_type` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `secondary_breakdown_type` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `reminder_date` date DEFAULT NULL,
  `waitlist_range_id` int(11) DEFAULT NULL,
  `multi_programmes` mediumtext COLLATE utf8_unicode_ci,
  `active_during_period_start_date` date DEFAULT NULL,
  `active_during_period_end_date` date DEFAULT NULL,
  `organisation_id` int(11) DEFAULT NULL,
  `age_range_id` int(11) DEFAULT NULL,
  `case_duration_range_id` int(11) DEFAULT NULL,
  `entry_to_exit_range_id` int(11) DEFAULT NULL,
  `town_id` int(11) DEFAULT NULL,
  `status_id` int(11) DEFAULT NULL,
  `exclude_status_id` int(11) DEFAULT NULL,
  `multi_status` mediumtext COLLATE utf8_unicode_ci,
  `outcome_id` int(11) DEFAULT NULL,
  `nhi` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `time_spent_range_id` int(11) DEFAULT NULL,
  `territorial_authority_id` int(11) DEFAULT NULL,
  `income_source_id` int(11) DEFAULT NULL,
  `service_type_id` int(11) DEFAULT NULL,
  `population_group_id` int(11) DEFAULT NULL,
  `abuse_type_id` int(11) DEFAULT NULL,
  `abuse_duration_id` int(11) DEFAULT NULL,
  `age_when_abused_id` int(11) DEFAULT NULL,
  `client_role_id` int(11) DEFAULT NULL,
  `client_type_id` int(11) DEFAULT NULL,
  `location_id` int(11) DEFAULT NULL,
  `needs_met_id` int(11) DEFAULT NULL,
  `parent_abuse_history_id` int(11) DEFAULT NULL,
  `perpetrator_id` int(11) DEFAULT NULL,
  `threats_violence_id` int(11) DEFAULT NULL,
  `childcare_provided_id` int(11) DEFAULT NULL,
  `transport_provided_id` int(11) DEFAULT NULL,
  `funding_source_id` int(11) DEFAULT NULL,
  `exclude_statuses` mediumtext COLLATE utf8_unicode_ci,
  `family_status_id` int(11) DEFAULT NULL,
  `multi_family_status` mediumtext COLLATE utf8_unicode_ci,
  `enrolled_with_pho_id` int(11) DEFAULT NULL,
  `child_or_adult_id` int(11) DEFAULT NULL,
  `referral_end_code_id` int(11) DEFAULT NULL,
  `referral_to_id` int(11) DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  `pregnant_id` int(11) DEFAULT NULL,
  `consent_id` int(11) DEFAULT NULL,
  `specific_referrer_id` int(11) DEFAULT NULL,
  `specific_referrer2_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=513018 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

客户端表的表描述约 30,000 行(绝不会出现在慢速查询中)

CREATE TABLE `clients` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `client_number` int(11) DEFAULT NULL,
  `client_status` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `programme_id` int(11) DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL,
  `created_on` date DEFAULT NULL,
  `updated_on` date DEFAULT NULL,
  `entry_date` date DEFAULT NULL,
  `exit_date` date DEFAULT NULL,
  `referral_source_id` int(11) DEFAULT NULL,
  `secondary_user_id` int(11) DEFAULT NULL,
  `school_id` int(11) DEFAULT NULL,
  `activation_date` date DEFAULT NULL,
  `last_updated_by` int(11) DEFAULT NULL,
  `activated_by` int(11) DEFAULT NULL,
  `exited_by` int(11) DEFAULT NULL,
  `related_client_id` int(11) DEFAULT NULL,
  `assessment_date` date DEFAULT NULL,
  `assessed_by` int(11) DEFAULT NULL,
  `core_updated_on` date DEFAULT NULL,
  `signed_off_date` date DEFAULT NULL,
  `signed_off_by` int(11) DEFAULT NULL,
  `reminder_date` date DEFAULT NULL,
  `reminder_text` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `waitlist_range_id` int(11) DEFAULT NULL,
  `organisation_id` int(11) DEFAULT NULL,
  `age_range_id` int(11) DEFAULT NULL,
  `case_duration_range_id` int(11) DEFAULT NULL,
  `entry_to_exit_range_id` int(11) DEFAULT NULL,
  `status_id` int(11) DEFAULT NULL,
  `time_spent_range_id` int(11) DEFAULT NULL,
  `person_id` int(11) DEFAULT NULL,
  `income_source_id` int(11) DEFAULT NULL,
  `territorial_authority_id` int(11) DEFAULT NULL,
  `service_type_id` int(11) DEFAULT NULL,
  `population_group_id` int(11) DEFAULT NULL,
  `abuse_type_id` int(11) DEFAULT NULL,
  `abuse_duration_id` int(11) DEFAULT NULL,
  `age_when_abused_id` int(11) DEFAULT NULL,
  `client_role_id` int(11) DEFAULT NULL,
  `client_type_id` int(11) DEFAULT NULL,
  `location_id` int(11) DEFAULT NULL,
  `needs_met_id` int(11) DEFAULT NULL,
  `parent_abuse_history_id` int(11) DEFAULT NULL,
  `perpetrator_id` int(11) DEFAULT NULL,
  `threats_violence_id` int(11) DEFAULT NULL,
  `linked_client_id` int(11) DEFAULT NULL,
  `outcome_id` int(11) DEFAULT NULL,
  `childcare_provided_id` int(11) DEFAULT NULL,
  `transport_provided_id` int(11) DEFAULT NULL,
  `funding_source_id` int(11) DEFAULT NULL,
  `family_status_id` int(11) DEFAULT NULL,
  `family_client` tinyint(1) DEFAULT NULL,
  `primary_family_client_id` int(11) DEFAULT NULL,
  `enrolled_with_pho_id` int(11) DEFAULT NULL,
  `child_or_adult_id` int(11) DEFAULT NULL,
  `referral_end_code_id` int(11) DEFAULT NULL,
  `referral_to_id` int(11) DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  `pregnant_id` int(11) DEFAULT NULL,
  `consent_id` int(11) DEFAULT NULL,
  `specific_referrer_id` int(11) DEFAULT NULL,
  `specific_referrer2_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `index_clients_on_person_id` (`person_id`),
  KEY `index_clients_on_user_id` (`user_id`),
  KEY `index_clients_on_programme_id` (`programme_id`),
  KEY `index_clients_on_organisation_id` (`organisation_id`)
) ENGINE=InnoDB AUTO_INCREMENT=48099 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

最佳答案

我想到的事情是:

  • 确保您选择的所有列都有索引。我会有以下内容:(users.id)、(organization_id)、(session_id),也许这将成为大型表,(organization_id, session_id),即两列索引,对于表来说,您的大小绝对不是必需的。
  • 确保您运行的任何实例在用于存储数据的磁盘上没有 I/O 上限,因此,例如,如果它是 AWS 微型且性能很重要,您应该真正提高它一点。

你的 my.cnf 看起来不错,因为你有这么小的表:你绝对不需要为此优化你的配置。只需确保您在尝试执行这些操作时没有加载带有大量其他写入操作的光盘。

关于MySql更新和删除极慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26373999/

相关文章:

php - 使用 mysql LIKE 语句

MYSQL插入GB大小的巨大SQL文件

MySQL - 如何有效地获取 ID 最低的行?

mysql - 读取订单的 mysql 查询非常慢(连接和分组依据)

sql - 查询调优 - 建议

mysql - 返回 MySQL 中 select 语句的实际持续时间

php - 在 Laravel 中使用 Mass Assignment 将变量从 Controller 传递到模型

php - 如何从查询中选择不同的值并将其连接到一列中

python mysqldb 不工作 - 没有错误消息

mysql - 查询性能;不确定发生了什么