MySql更新和删除速度非常慢

MySql更新和删除速度非常慢,mysql,database-performance,Mysql,Database Performance,我已在my.cnf中启用慢速查询日志 日志中没有select查询。它们都是更新、删除和插入,在非常小的表上需要几秒钟的时间 下面是使用dumpslow分析的slow查询日志中的几个示例: 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 Coun

我已在my.cnf中启用慢速查询日志

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

下面是使用dumpslow分析的slow查询日志中的几个示例:

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_search”分别是非常小的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
客户端的表说明表~30000行从不在慢速查询中出现

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\u id、session\u id,如果这将成为一个庞大的表,那么可能会有organization\u id、session\u id,即两列索引,对于表来说,您的大小肯定是不必要的。 确保您运行的任何实例在用于存储数据的光盘上的I/O上都没有上限,因此,例如,如果它是AWS micro,并且性能很重要,那么您应该确实提高一点。
您的my.cnf看起来不错,因为您有这么小的表:您肯定不需要为此优化配置。在尝试执行这些操作时,请确保没有将大量其他写入操作加载到光盘上。

我的问题似乎与非常慢的磁盘IO有关

当我跑步时: dd bs=1M计数=256如果=/dev/zero of=test conv=fdatasync 我得到的结果小于20 MB/s

我换了一台新主机,速度超过了300 MB/s,不再有问题了

我的托管公司建议我对My.cnf进行以下更改

innodb_flush_log_at_trx_commit=2
innodb_file_per_table=ON
innodb_flush_method=O_DSYNC

我做了一些改变,但是没有什么真正的区别,而迁移到一个新服务器就像。。好新服务器

让我困惑的是,slow queries日志中的条目用于更新数百行的表,而selects用于数十万行的表,却没有出现在日志中。这很有趣。你能发布一个显示为用户和客户搜索创建的表,以及一个更快的表吗?编辑问题以添加一些表描述是的,你没有关于客户搜索的索引。组织id和客户搜索。会话id-这些将大大提高性能。您是否已将users.id作为主键?如果你已经做了,并且查询仍然需要很长时间,那么一些非常奇怪的事情正在发生……你在运行什么样的硬件?缓慢的东西是插入到客户端搜索中。索引会使插入变慢。