查看mysql查询缓存内容

View mysql query cache contents

我有一个使用 mysqls 查询缓存功能的大型数据库应用程序。我想检查查询缓存的内容以识别不应缓存的条目(例如,通过使用 SELECT SQL_NO_CACHE...),因为我想优化我的应用程序。

有没有办法从查询缓存中获取缓存条目的列表?

-- 编辑:Rick 建议的附加信息 ---

1) RAM 大小为 16GB

2) 显示变量;

auto_increment_increment    1
auto_increment_offset   1
autocommit  ON
automatic_sp_privileges ON
back_log    50
basedir /usr/
big_tables  OFF
binlog_cache_size   32768
binlog_direct_non_transactional_updates OFF
binlog_format   STATEMENT
bulk_insert_buffer_size 4294967296
character_set_client    utf8
character_set_connection    utf8
character_set_database  utf8
character_set_filesystem    binary
character_set_results   utf8
character_set_server    utf8
character_set_system    utf8
character_sets_dir  /usr/share/mysql/charsets/
collation_connection    utf8_general_ci
collation_database  utf8_general_ci
collation_server    utf8_general_ci
completion_type 0
concurrent_insert   1
connect_timeout 10
datadir /var/lib/mysql/
date_format %Y-%m-%d
datetime_format %Y-%m-%d %H:%i:%s
default_week_format 0
delay_key_write ON
delayed_insert_limit    100
delayed_insert_timeout  300
delayed_queue_size  1000
div_precision_increment 4
engine_condition_pushdown   ON
error_count 0
event_scheduler ON
expire_logs_days    0
flush   OFF
flush_time  0
foreign_key_checks  ON
ft_boolean_syntax   + -><()~*:""&|
ft_max_word_len 84
ft_min_word_len 4
ft_query_expansion_limit    20
ft_stopword_file    (built-in)
general_log OFF
general_log_file    /var/run/mysqld/mysqld.log
group_concat_max_len    1024
have_community_features YES
have_compress   YES
have_crypt  YES
have_csv    YES
have_dynamic_loading    YES
have_geometry   YES
have_innodb YES
have_ndbcluster NO
have_openssl    DISABLED
have_partitioning   NO
have_query_cache    YES
have_rtree_keys YES
have_ssl    DISABLED
have_symlink    YES
hostname    www2
identity    0
ignore_builtin_innodb   ON
init_connect     
init_file    
init_slave   
innodb_adaptive_flushing    ON
innodb_adaptive_hash_index  ON
innodb_additional_mem_pool_size 67108864
innodb_autoextend_increment 8
innodb_autoinc_lock_mode    1
innodb_buffer_pool_size 1073741824
innodb_change_buffering inserts
innodb_checksums    ON
innodb_commit_concurrency   0
innodb_concurrency_tickets  500
innodb_data_file_path   ibdata1:10M:autoextend
innodb_data_home_dir     
innodb_doublewrite  ON
innodb_fast_shutdown    1
innodb_file_format  Antelope
innodb_file_format_check    Barracuda
innodb_file_per_table   ON
innodb_flush_log_at_trx_commit  1
innodb_flush_method  
innodb_force_recovery   0
innodb_io_capacity  200
innodb_lock_wait_timeout    50
innodb_locks_unsafe_for_binlog  OFF
innodb_log_buffer_size  67108864
innodb_log_file_size    268435456
innodb_log_files_in_group   2
innodb_log_group_home_dir   ./
innodb_max_dirty_pages_pct  75
innodb_max_purge_lag    0
innodb_mirrored_log_groups  1
innodb_old_blocks_pct   37
innodb_old_blocks_time  0
innodb_open_files   300
innodb_read_ahead_threshold 56
innodb_read_io_threads  4
innodb_replication_delay    0
innodb_rollback_on_timeout  OFF
innodb_spin_wait_delay  6
innodb_stats_method nulls_equal
innodb_stats_on_metadata    ON
innodb_stats_sample_pages   8
innodb_strict_mode  OFF
innodb_support_xa   ON
innodb_sync_spin_loops  30
innodb_table_locks  ON
innodb_thread_concurrency   0
innodb_thread_sleep_delay   10000
innodb_use_sys_malloc   ON
innodb_version  1.0.15
innodb_write_io_threads 4
insert_id   0
interactive_timeout 30
join_buffer_size    2147483648
keep_files_on_create    OFF
key_buffer_size 4294967296
key_cache_age_threshold 300
key_cache_block_size    1024
key_cache_division_limit    100
language    /usr/share/mysql/english/
large_files_support ON
large_page_size 0
large_pages OFF
last_insert_id  0
lc_time_names   en_US
license GPL
local_infile    OFF
locked_in_memory    OFF
log OFF
log_bin OFF
log_bin_trust_function_creators OFF
log_bin_trust_routine_creators  OFF
log_error   /var/log/mysql/mysqld.err
log_output  FILE
log_queries_not_using_indexes   OFF
log_slave_updates   OFF
log_slow_queries    ON
log_warnings    2
long_query_time 2.000000
low_priority_updates    OFF
lower_case_file_system  OFF
lower_case_table_names  0
max_allowed_packet  268435456
max_binlog_cache_size   18446744073709547520
max_binlog_size 1073741824
max_connect_errors  100000
max_connections 120
max_delayed_threads 20
max_error_count 64
max_heap_table_size 268435456
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
min_examined_row_limit  0
multi_range_count   256
myisam_data_pointer_size    6
myisam_max_sort_file_size   9223372036853727232
myisam_mmap_size    18446744073709551615
myisam_recover_options  OFF
myisam_repair_threads   1
myisam_sort_buffer_size 4294967296
myisam_stats_method nulls_unequal
myisam_use_mmap OFF
net_buffer_length   1048576
net_read_timeout    30
net_retry_count 10
net_write_timeout   60
new OFF
old OFF
old_alter_table OFF
old_passwords   OFF
open_files_limit    8322
optimizer_prune_level   1
optimizer_search_depth  62
optimizer_switch    index_merge=on,index_merge_union=on,index_merge_so...
pid_file    /var/run/mysqld/mysqld.pid
plugin_dir  /usr/lib64/mysql/plugin
port    3306
preload_buffer_size 32768
protocol_version    10
pseudo_thread_id    3468076
query_alloc_block_size  8192
query_cache_limit   4294967296
query_cache_min_res_unit    4096
query_cache_size    1073741824
query_cache_type    ON
query_cache_wlock_invalidate    OFF
query_prealloc_size 8192
rand_seed1   
rand_seed2   
range_alloc_block_size  4096
read_buffer_size    131072
read_only   OFF
read_rnd_buffer_size    8388608
relay_log    
relay_log_index  
relay_log_info_file relay-log.info
relay_log_purge ON
relay_log_space_limit   0
report_host  
report_password  
report_port 3306
report_user  
rpl_recovery_rank   0
secure_auth OFF
secure_file_priv     
server_id   0
skip_external_locking   ON
skip_name_resolve   OFF
skip_networking OFF
skip_show_database  OFF
slave_compressed_protocol   OFF
slave_exec_mode STRICT
slave_load_tmpdir   /var/tmp/
slave_net_timeout   3600
slave_skip_errors   OFF
slave_transaction_retries   10
slow_launch_time    2
slow_query_log  ON
slow_query_log_file /var/log/mysql/slowqueries.log
socket  /var/run/mysqld/mysqld.sock
sort_buffer_size    262144
sql_auto_is_null    ON
sql_big_selects ON
sql_big_tables  OFF
sql_buffer_result   OFF
sql_log_bin ON
sql_log_off OFF
sql_log_update  ON
sql_low_priority_updates    OFF
sql_max_join_size   18446744073709551615
sql_mode     
sql_notes   ON
sql_quote_show_create   ON
sql_safe_updates    OFF
sql_select_limit    18446744073709551615
sql_slave_skip_counter   
sql_warnings    OFF
ssl_ca   
ssl_capath   
ssl_cert     
ssl_cipher   
ssl_key  
storage_engine  MyISAM
sync_binlog 0
sync_frm    ON
system_time_zone    CEST
table_definition_cache  256
table_lock_wait_timeout 50
table_open_cache    4096
table_type  MyISAM
thread_cache_size   16
thread_handling one-thread-per-connection
thread_stack    262144
time_format %H:%i:%s
time_zone   SYSTEM
timed_mutexes   OFF
timestamp   1449649902
tmp_table_size  2147483648
tmpdir  /var/tmp/
transaction_alloc_block_size    8192
transaction_prealloc_size   4096
tx_isolation    READ-COMMITTED
unique_checks   ON
updatable_views_with_limit  YES
version 5.1.56-log
version_comment Gentoo Linux mysql-5.1.56
version_compile_machine x86_64
version_compile_os  pc-linux-gnu
wait_timeout    30
warning_count   0

3) 显示全局状态;

Aborted_clients 2094
Aborted_connects    4
Binlog_cache_disk_use   0
Binlog_cache_use    0
Bytes_received  171903979482
Bytes_sent  2515206884666
Com_admin_commands  339464
Com_assign_to_keycache  0
Com_alter_db    0
Com_alter_db_upgrade    0
Com_alter_event 0
Com_alter_function  0
Com_alter_procedure 0
Com_alter_server    0
Com_alter_table 340
Com_alter_tablespace    0
Com_analyze 0
Com_backup_table    0
Com_begin   5508
Com_binlog  0
Com_call_procedure  0
Com_change_db   3205067
Com_change_master   0
Com_check   2735
Com_checksum    0
Com_commit  5508
Com_create_db   1
Com_create_event    0
Com_create_function 0
Com_create_index    0
Com_create_procedure    0
Com_create_server   0
Com_create_table    148
Com_create_trigger  0
Com_create_udf  0
Com_create_user 0
Com_create_view 0
Com_dealloc_sql 2
Com_delete  10465739
Com_delete_multi    15995
Com_do  0
Com_drop_db 0
Com_drop_event  0
Com_drop_function   0
Com_drop_index  0
Com_drop_procedure  0
Com_drop_server 0
Com_drop_table  121
Com_drop_trigger    0
Com_drop_user   0
Com_drop_view   0
Com_empty_query 0
Com_execute_sql 2
Com_flush   342
Com_grant   2
Com_ha_close    0
Com_ha_open 0
Com_ha_read 0
Com_help    0
Com_insert  13583029
Com_insert_select   409
Com_install_plugin  0
Com_kill    0
Com_load    112042
Com_load_master_data    0
Com_load_master_table   0
Com_lock_tables 2068
Com_optimize    44
Com_preload_keys    0
Com_prepare_sql 2
Com_purge   0
Com_purge_before_date   0
Com_release_savepoint   0
Com_rename_table    0
Com_rename_user 0
Com_repair  2
Com_replace 489
Com_replace_select  0
Com_reset   0
Com_restore_table   0
Com_revoke  0
Com_revoke_all  0
Com_rollback    0
Com_rollback_to_savepoint   0
Com_savepoint   0
Com_select  115580660
Com_set_option  6028458
Com_show_authors    0
Com_show_binlog_events  0
Com_show_binlogs    11
Com_show_charsets   128
Com_show_collations 105
Com_show_column_types   0
Com_show_contributors   0
Com_show_create_db  10
Com_show_create_event   0
Com_show_create_func    0
Com_show_create_proc    0
Com_show_create_table   172358
Com_show_create_trigger 0
Com_show_databases  3956
Com_show_engine_logs    0
Com_show_engine_mutex   0
Com_show_engine_status  0
Com_show_events 0
Com_show_errors 0
Com_show_fields 839418
Com_show_function_status    0
Com_show_grants 11
Com_show_keys   3212
Com_show_master_status  0
Com_show_new_master 0
Com_show_open_tables    0
Com_show_plugins    0
Com_show_privileges 0
Com_show_procedure_status   0
Com_show_processlist    2
Com_show_profile    0
Com_show_profiles   0
Com_show_slave_hosts    0
Com_show_slave_status   0
Com_show_status 31038
Com_show_storage_engines    0
Com_show_table_status   207707
Com_show_tables 55951
Com_show_triggers   172340
Com_show_variables  803242
Com_show_warnings   0
Com_slave_start 0
Com_slave_stop  0
Com_stmt_close  627128
Com_stmt_execute    627128
Com_stmt_fetch  0
Com_stmt_prepare    627128
Com_stmt_reprepare  0
Com_stmt_reset  0
Com_stmt_send_long_data 0
Com_truncate    4722
Com_uninstall_plugin    0
Com_unlock_tables   2068
Com_update  1754805
Com_update_multi    1
Com_xa_commit   0
Com_xa_end  0
Com_xa_prepare  0
Com_xa_recover  0
Com_xa_rollback 0
Com_xa_start    0
Compression OFF
Connections 3467815
Created_tmp_disk_tables 2151917
Created_tmp_files   16517
Created_tmp_tables  3928177
Delayed_errors  0
Delayed_insert_threads  0
Delayed_writes  0
Flush_commands  1
Handler_commit  90276170
Handler_delete  9422032
Handler_discover    0
Handler_prepare 0
Handler_read_first  16985165
Handler_read_key    727922287
Handler_read_next   1575370461
Handler_read_prev   1178376
Handler_read_rnd    114364819
Handler_read_rnd_next   40377965120
Handler_rollback    326
Handler_savepoint   0
Handler_savepoint_rollback  0
Handler_update  33675553
Handler_write   137057026
Innodb_buffer_pool_pages_data   65003
Innodb_buffer_pool_pages_dirty  44
Innodb_buffer_pool_pages_flushed    29673396
Innodb_buffer_pool_pages_free   1
Innodb_buffer_pool_pages_misc   531
Innodb_buffer_pool_pages_total  65535
Innodb_buffer_pool_read_ahead   30144135
Innodb_buffer_pool_read_ahead_evicted   4691916
Innodb_buffer_pool_read_requests    5269018735
Innodb_buffer_pool_reads    15291059
Innodb_buffer_pool_wait_free    0
Innodb_buffer_pool_write_requests   196667000
Innodb_data_fsyncs  23264996
Innodb_data_pending_fsyncs  1
Innodb_data_pending_reads   0
Innodb_data_pending_writes  0
Innodb_data_read    745229520896
Innodb_data_reads   15847178
Innodb_data_writes  40737707
Innodb_data_written 1007283227136
Innodb_dblwr_pages_written  29673396
Innodb_dblwr_writes 727171
Innodb_have_atomic_builtins ON
Innodb_log_waits    0
Innodb_log_write_requests   61077243
Innodb_log_writes   17124326
Innodb_os_log_fsyncs    17565254
Innodb_os_log_pending_fsyncs    1
Innodb_os_log_pending_writes    0
Innodb_os_log_written   34746359296
Innodb_page_size    16384
Innodb_pages_created    893340
Innodb_pages_read   45485066
Innodb_pages_written    29673396
Innodb_row_lock_current_waits   0
Innodb_row_lock_time    75432
Innodb_row_lock_time_avg    23
Innodb_row_lock_time_max    10927
Innodb_row_lock_waits   3264
Innodb_rows_deleted 8649356
Innodb_rows_inserted    12313551
Innodb_rows_read    3890223870
Innodb_rows_updated 1493452
Key_blocks_not_flushed  0
Key_blocks_unused   3089739
Key_blocks_used 864035
Key_read_requests   1336336475
Key_reads   2772018
Key_write_requests  105012022
Key_writes  7764232
Last_query_cost 0.000000
Max_used_connections    69
Not_flushed_delayed_rows    0
Open_files  3006
Open_streams    0
Open_table_definitions  2265
Open_tables 3982
Opened_files    16841374
Opened_table_definitions    140845
Opened_tables   327918
Prepared_stmt_count 0
Qcache_free_blocks  56315
Qcache_free_memory  164432360
Qcache_hits 539352560
Qcache_inserts  111849501
Qcache_lowmem_prunes    54731591
Qcache_not_cached   3376334
Qcache_queries_in_cache 182383
Qcache_total_blocks 472186
Queries 697132110
Questions   695877858
Rpl_status  NULL
Select_full_join    30156
Select_full_range_join  4732
Select_range    36065208
Select_range_check  76799
Select_scan 4058097
Slave_open_temp_tables  0
Slave_retried_transactions  0
Slave_running   OFF
Slow_launch_threads 0
Slow_queries    13817
Sort_merge_passes   24563
Sort_range  2573998
Sort_rows   93946492
Sort_scan   1603263
Ssl_accept_renegotiates 0
Ssl_accepts 0
Ssl_callback_cache_hits 0
Ssl_cipher
Ssl_cipher_list
Ssl_client_connects 0
Ssl_connect_renegotiates    0
Ssl_ctx_verify_depth    0
Ssl_ctx_verify_mode 0
Ssl_default_timeout 0
Ssl_finished_accepts    0
Ssl_finished_connects   0
Ssl_session_cache_hits  0
Ssl_session_cache_misses    0
Ssl_session_cache_mode  NONE
Ssl_session_cache_overflows 0
Ssl_session_cache_size  0
Ssl_session_cache_timeouts  0
Ssl_sessions_reused 0
Ssl_used_session_cache_entries  0
Ssl_verify_depth    0
Ssl_verify_mode 0
Ssl_version
Table_locks_immediate   157822475
Table_locks_waited  3513
Tc_log_max_pages_used   0
Tc_log_page_size    0
Tc_log_page_waits   0
Threads_cached  14
Threads_connected   3
Threads_created 2076
Threads_running 3
Uptime  4656056
Uptime_since_flush_status   4656056

您可以知道使用

缓存的查询数量
Show status like 'Qcache_queries_in_cache'

它将在 value 列中为您提供缓存查询的数量。 只有查询的散列存储在 Qcache 中。 无法找到实际缓存了哪些查询。 了解它的一种可能方法是执行您的查询之一并检查 value 列的更改。

如果你有一个 "large" 数据库应用程序,这意味着有很多写入?请记住,当 any 写入 table 时,table 的 all QC 条目将被清除。这意味着您可能无法从 QC 中受益。

此外,清除需要付出努力,从而减慢系统速度。对于query_cache_size下,比如50M,开销不大。但是如果你有1G的QC,你刷QC的时间会比使用QC的时间还长!

无法发现QC 中的实际查询。以下是 可能 的帮助:将 SQL_NO_CACHE 放在任何涉及经常更改的 table(s) 的查询上。将 SQL_CACHE 放在其余部分。然后使用DEMAND。监控 STATUS LIKE Qc%.

如果您想分析您的系统(QC 或其他),请提供 (1) RAM 大小,(2) 显示变量,(3) 在服务器启动至少一天后显示全局状态.

观察:

版本:5.1.56-log
16 GB 内存
正常运行时间 = 53d 21:20:56
您不在 Windows 上 运行。
运行 64 位版本
看来您是 运行 MyISAM 和 InnoDB。

更重要的问题

key_buffer_size = 1500M
innodb_buffer_pool_size = 2000M
join_buffer_size = 1M   -- This may be the most important
tmp_table_size = 256M   -- Also important
query_cache_size = 50M  -- as already mentioned
Use slowlog to find tmp tables spilling to disk

三个设置特别高,可能导致 paging/swapping。 MySQL 性能在 paged/swapped 时受到严重影响。与其发生这种情况,不如降低设置。

细节和小问题

( innodb_buffer_pool_size / _ram ) = 1024M / 16384M = 6.2% -- 用于 InnoDB 的 RAM 百分比 buffer_pool

( Innodb_pages_written / Innodb_buffer_pool_write_requests ) = 29,673,396 / 196667000 = 15.1% -- 必须写入磁盘的写入请求 -- 检查 innodb_buffer_pool_size

( Innodb_buffer_pool_read_ahead_evicted / Innodb_buffer_pool_read_ahead ) = 4,691,916 / 30144135 = 15.6% -- read_ahead 的效用。 -- 关闭预读

( Innodb_os_log_written / (Uptime / 3600) / innodb_log_files_in_group / innodb_log_file_size ) = 34,746,359,296 / (4656056 / 3600) / 2 / 256M = 0.05 -- 比率

( Uptime / 60 * innodb_log_file_size / Innodb_os_log_written ) = 4,656,056 / 60 * 256M / 34746359296 = 599 -- InnoDB 日志轮换之间的分钟数 从 5.6.8 开始,可以动态更改;请务必也更改 my.cnf。 --(轮换间隔 60 分钟的建议有些武断。)

( table_open_cache ) = 4,096 -- 要缓存的 table 个描述符的数量 -- 一般几百就可以了。

( innodb_stats_on_metadata ) = ON -- 触摸统计数据时重新分析 table。 -- ON 可能会减慢某些 SHOW 和 information_schema 访问。

( join_buffer_size ) = 2,147,483,648 = 2048MB -- 每个线程 0-N。可能会加速 JOIN(最好修复 queries/indexes)(所有引擎)用于索引扫描、范围索引扫描、全 table 扫描、每个全 JOIN 等。 -- 使用默认值。

( join_buffer_size / _ram ) = 2,147,483,648 / 16384M = 12.5% -- 每个线程 0-N。可能会加速 JOIN(最好修复 queries/indexes)(所有引擎)用于索引扫描、范围索引扫描、全 table 扫描、每个全 JOIN 等。 -- 减小 join_buffer_size 以避免内存压力。建议小于 1% 的内存。

( min( tmp_table_size, max_heap_table_size ) / _ram ) = min( 2147483648, 256M ) / 16384M = 1.6% -- 需要内存时要分配的 RAM 百分比 table (每个 table),或 SELECT 中的温度 table(每个温度 table 每个 SELECTs)。太高可能导致交换。 -- 将 tmp_table_size 和 max_heap_table_size 减少到 ram 的 1%。

( (key_buffer_size - 1.2 * Key_blocks_used * 1024) / _ram ) = (4294967296 - 1.2 * 864035 * 1024) / 16384M = 18.8% -- [= 中浪费的 RAM 百分比101=]。 -- 减少 key_buffer_size

( Key_blocks_used * 1024 / key_buffer_size ) = 864,035 * 1024 / 4294967296 = 20.6% - key_buffer 的使用百分比。高水位线。 -- 降低 key_buffer_size 以避免不必要的内存使用。

( Key_writes / Key_write_requests ) = 7,764,232 / 105012022 = 7.4% -- key_buffer 写入效率

( query_cache_size ) = 1024M -- QC 的大小 -- 太小 = 没有多大用处。太大 = 开销太大。建议0或不超过50M。

( Created_tmp_disk_tables / (Created_tmp_disk_tables + Created_tmp_tables) ) = 2,151,917 / (2151917 + 3928177) = 35.4% -- 溢出的温度百分比 table到磁盘

( tmp_table_size ) = 2,147,483,648 = 2048MB -- 用于支持 SELECT 的 MEMORY temp tables 的大小限制 -- 减少 tmp_table_size 以避免 运行 内存不足。可能不会超过64M。

( back_log / max_connections ) = 50 / 120 = 41.7%

( max_connect_errors ) = 100,000 -- 针对黑客的小保护。 -- 可能不会超过 200。