MySQL查询随机取“永远”

Jul*_*oux 5 mysql optimization performance innodb

我们有一个 XMPP 应用程序,它使用 MySQL 来存储信息。到目前为止,我们还没有遇到任何特定的负载问题,但我正在努力为最坏的情况(或最好的,就用户而言;))做好准备。

安装此 MySQL 服务器的主机是具有 2GB RAM 的 Slicehost 片。

昨天,我激活了慢查询日志以确保我们实际上没有慢。不幸的是,似乎实际上发现了很多慢查询:

从 /var/log/mysql/mysql-slow.log 读取 mysql 慢查询日志
Count: 109 Time=25.57s (2787s) Lock=0.00s (0s) Rows=1.0 (109), xxxxx[xxxxx]@[172.21.xxx.xxx]
  SELECT * FROM `feeds` WHERE (`id` = 'S') LIMIT N

这对我来说真的很奇怪,因为 id 实际上是一个主键......表是 InnoDB

我做了一个解释:

mysql> EXPLAIN SELECT * FROM `feeds` WHERE (`id` = '2650') LIMIT 1;

 +----+-------------+-------+-------+-------------- -+---------+---------+-------+------+-------+
 | 身份证 | 选择类型 | 表| 类型 | 可能的密钥| 关键| 密钥长度 | 参考 | 行 | 额外 |
 +----+-------------+-------+-------+-------------- -+---------+---------+-------+------+-------+
 | 1 | 简单 | 饲料| 常量 | 主要 | 主要 | 4 | 常量 | 1 | |
 +----+-------------+-------+-------+-------------- -+---------+---------+-------+------+-------+
 1 行(0.00 秒)

发生这种情况一定有另一个原因。我们的日志中实际上有很多类似的慢查询(使用主键的查询)。

我认为在这里发布 MySQL 设置是有意义的:

mysql> 显示变量;
+---------------------------------+--------------- --------------+
| 变量名 | 价值 |
+---------------------------------+--------------- --------------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
| automatic_sp_privileges | 开 |
| back_log | 50 |
| 基础 | /usr/ |
| binlog_cache_size | 32768 |
| 批量插入缓冲区大小| 8388608 |
| character_set_client | 拉丁语1 |
| 字符集连接| 拉丁语1 |
| 字符集数据库| 拉丁语1 |
| character_set_filesystem | 二进制 |
| character_set_results | 拉丁语1 |
| character_set_server | 拉丁语1 |
| 字符集系统| utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
| collat​​ion_connection | latin1_swedish_ci |
| collat​​ion_database | latin1_swedish_ci |
| collat​​ion_server | latin1_swedish_ci |
| 完成类型| 0 |
| concurrent_insert | 1 |
| 连接超时| 10 |
| 数据目录 | /var/lib/mysql/ |
| 日期格式| %Y-%m-%d |
| 日期时间格式| %Y-%m-%d %H:%i:%s |
| default_week_format | 0 |
| delay_key_write | 开 |
| delay_insert_limit | 100 |
| delay_insert_timeout | 300 |
| delay_queue_size | 1000 |
| div_precision_increment | 4 |
| keep_files_on_create | 关闭 |
| engine_condition_pushdown | 关闭 |
| expire_logs_days | 10 |
| 冲洗 | 关闭 |
| 冲洗时间 | 0 |
| ft_boolean_syntax | + -><()~*:""&| |
| ft_max_word_len | 84 |
| ft_min_word_len | 4 |
| ft_query_expansion_limit | 20 |
| ft_stopword_file | (内置) |
| group_concat_max_len | 1024 |
| have_archive | 是 |
| have_bdb | 否 |
| have_blackhole_engine | 是 |
| have_compress | 是 |
| have_crypt | 是 |
| have_csv | 是 |
| have_dynamic_loading | 是 |
| have_example_engine | 否 |
| have_federated_engine | 残疾人 |
| have_geometry | 是 |
| have_innodb | 是 |
| have_isam | 否 |
| have_merge_engine | 是 |
| have_ndbcluster | 残疾人 |
| have_openssl | 残疾人 |
| have_ssl | 残疾人 |
| have_query_cache | 是 |
| have_raid | 否 |
| have_rtree_keys | 是 |
| have_symlink | 是 |
| 主机名 | 超级饲料数据库|
| init_connect | |
| init_file | |
| init_slave | |
| innodb_additional_mem_pool_size | 1048576 |
| innodb_autoextend_increment | 8 |
| innodb_buffer_pool_awe_mem_mb | 0 |
| innodb_buffer_pool_size | 1073741824 |
| innodb_checksums | 开 |
| innodb_commit_concurrency | 0 |
| innodb_concurrency_tickets | 500 |
| innodb_data_file_path | ibdata1:10M:autoextend |
| innodb_data_home_dir | |
| innodb_adaptive_hash_index | 开 |
| innodb_doublewrite | 开 |
| innodb_fast_shutdown | 1 |
| innodb_file_io_threads | 4 |
| innodb_file_per_table | 开 |
| innodb_flush_log_at_trx_commit | 2 |
| innodb_flush_method | O_DIRECT |
| innodb_force_recovery | 0 |
| innodb_lock_wait_timeout | 50 |
| innodb_locks_unsafe_for_binlog | 关闭 |
| innodb_log_arch_dir | |
| innodb_log_archive | 关闭 |
| innodb_log_buffer_size | 4194304 |
| innodb_log_file_size | 5242880 |
| innodb_log_files_in_group | 2 |
| innodb_log_group_home_dir | ./ |
| innodb_max_dirty_pages_pct | 90 |
| innodb_max_purge_lag | 0 |
| innodb_mirrored_log_groups | 1 |
| innodb_open_files | 300 |
| innodb_rollback_on_timeout | 关闭 |
| innodb_support_xa | 开 |
| innodb_sync_spin_loops | 20 |
| innodb_table_locks | 开 |
| innodb_thread_concurrency | 8 |
| innodb_thread_sleep_delay | 10000 |
| Interactive_timeout | 28800 |
| join_buffer_size | 131072 |
| key_buffer_size | 16777216 |
| key_cache_age_threshold | 300 |
| key_cache_block_size | 1024 |
| key_cache_division_limit | 100 |
| 语言 | /usr/share/mysql/english/ |
| large_files_support | 开 |
| large_page_size | 0 |
| large_pages | 关闭 |
| lc_time_names | zh_CN |
| 许可证 | GPL |
| local_infile | 开 |
| 锁定内存| 关闭 |
| 日志 | 关闭 |
| log_bin | 关闭 |
| log_bin_trust_function_creators | 关闭 |
| 日志错误| |
| log_queries_not_using_indexes | 开 |
| log_slave_updates | 关闭 |
| log_slow_queries | 开 |
| log_warnings | 1 |
| long_query_time | 3 |
| low_priority_updates | 关闭 |
| lower_case_file_system | 关闭 |
| lower_case_table_names | 0 |
| max_allowed_pa​​cket | 16777216 |
| max_binlog_cache_size | 18446744073709547520 |
| max_binlog_size | 104857600 |
| max_connect_errors | 10 |
| max_connections | 2000 |
| max_delayed_threads | 20 |
| max_error_count | 64 |
| max_heap_table_size | 16777216 |
| max_insert_delayed_threads | 20 |
| max_join_size | 18446744073709551615 |
| max_length_for_sort_data | 1024 |
| max_prepared_stmt_count | 16382 |
| max_relay_log_size | 0 |
| max_seeks_for_key | 18446744073709551615 |
| max_sort_length | 1024 |
| max_sp_recursion_depth | 0 |
| max_tmp_tables | 32 |
| max_user_connections | 0 |
| max_write_lock_count | 18446744073709551615 |
| multi_range_count | 第256话
| myisam_data_pointer_size | 6 |
| myisam_max_sort_file_size | 9223372036853727232 |
| myisam_recover_options | 备份 |
| myisam_repair_threads | 1 |
| myisam_sort_buffer_size | 8388608 |
| myisam_stats_method | nulls_unequal |
| ndb_autoincrement_prefetch_sz | 1 |
| ndb_force_send | 开 |
| ndb_use_exact_count | 开 |
| ndb_use_transactions | 开 |
| ndb_cache_check_time | 0 |
| ndb_connectstring | |
| net_buffer_length | 16384 |
| net_read_timeout | 30 |
| net_retry_count | 10 |
| net_write_timeout | 60 |
| 新 | 关闭 |
| old_passwords | 关闭 |
| open_files_limit | 10000 |
| optimizer_prune_level | 1 |
| optimizer_search_depth | 62 |
| pid_file | /var/run/mysqld/mysqld.pid |
| plugin_dir | |
| 港口| 3306 |
| preload_buffer_size | 32768 |
| 分析 | 关闭 |
| profiling_history_size | 15 |
| 协议版本| 10 |
| query_alloc_block_size | 8192 |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 16777216 |
| query_cache_type | 开 |
| query_cache_wlock_invalidate | 关闭 |
| query_prealloc_size | 8192 |
| range_alloc_block_size | 4096 |
| read_buffer_size | 131072 |
| 只读 | 关闭 |
| read_rnd_buffer_size | 262144 |
| 中继日志| |
| 中继日志索引| |
| relay_log_info_file | 中继日志信息|
| relay_log_purge | 开 |
| 中继日志空间限制| 0 |
| rpl_recovery_rank | 0 |
| 安全认证| 关闭 |
| secure_file_priv | |
| server_id | 0 |
| skip_external_locking | 开 |
| 跳过网络| 关闭 |
| skip_show_database | 关闭 |
| slave_compressed_protocol | 关闭 |
| slave_load_tmpdir | /tmp/ |
| slave_net_timeout | 3600 |
| slave_skip_errors | 关闭 |
| slave_transaction_retries | 10 |
| slow_launch_time | 2 |
| 插座 | /var/run/mysqld/mysqld.sock |
| sort_buffer_size | 2097144 |
| sql_big_selects | 开 |
| sql_mode | |
| sql_notes | 开 |
| sql_warnings | 关闭 |
| ssl_ca | |
| ssl_capath | |
| ssl_cert | |
| ssl_cipher | |
| ssl_key | |
| storage_engine | 我的ISAM |
| sync_binlog | 0 |
| sync_frm | 开 |
| system_time_zone | UTC |
| table_cache | 64 |
| table_lock_wait_timeout | 50 |
| 表类型 | 我的ISAM |
| thread_cache_size | 8 |
| 线程堆栈| 131072 |
| 时间格式| %H:%i:%s |
| 时区 | 系统 |
| timed_mutexes | 关闭 |
| tmp_table_size | 33554432 |
| tmpdir | /tmp |
| transaction_alloc_block_size | 8192 |
| transaction_prealloc_size | 4096 |
| tx_isolation | 读提交 |
| updatetable_views_with_limit | 是 |
| 版本| 5.0.67-0ubuntu6-log |
| version_comment | (Ubuntu) |
| version_compile_machine | x86_64 |
| version_compile_os | debian-linux-gnu |
| 等待超时| 28800 |
+---------------------------------+--------------- --------------+
237 行(0.00 秒)

我们的大多数请求都是“基本的”,但是,我们需要极快的速度!

关于什么实际上会使 MySQL 如此缓慢的任何想法?

[总结]总结各种答案:

  • 去掉“LIMIT”,把WHERE id = “X”改成WHERE id = X
  • 确保我没有任何不时运行会消耗大量资源的脚本(备份或其他)
  • 确保“主机”实际上不是罪魁祸首。

Jos*_*hua 3

查看速度减慢的均值和方差,您的 VM 主机存在问题(不幸的是,这不在您的控制之下)。

对于那些指出内存/磁盘 I/O 的人来说,这些数字太大了。磁盘应在 100 毫秒内返回,而不是几秒钟。