View mysql query cache contents
Asked Answered
H

3

6

I have a large database application that uses mysqls query cache ability. I'd like to inspect the contents of the query cache to identify entries that should not get cached (e.g. by using SELECT SQL_NO_CACHE...) because I want to optimize my application.

Is there a way to get a list of cached entries from the query cache?

-- Edit: additional info as suggested by Rick ---

1) RAM size is 16GB

2) show variables;

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) show global status;

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
Halter answered 3/12, 2015 at 7:50 Comment(0)
S
10

You can know the amount of queries cached with

Show status like 'Qcache_queries_in_cache'

It will give you the amount of cached query in value column. Only the hash of queries are stored in Qcache. It is not possible to find which queries are actually cached. One possible way to know it is to execute one of your query and to check changes of value column.

Suchta answered 3/12, 2015 at 8:5 Comment(2)
Do SHOW STATUS LIKE 'Qcache%'; and you will get even more variables to check ;)Thoron
OK, that sounds quite convincing. If all queries would be stored this would require serious amounts of memory, storing only query hashes is probably the only way to go. At leadst Qcache_queries_in_cache will give me a hint if the last issued query is added to the cache.Halter
D
10

Observations:

Version: 5.1.56-log
16 GB of RAM
Uptime = 53d 21:20:56
You are not running on Windows.
Running 64-bit version
It appears that you are running both MyISAM and InnoDB.

The More Important Issues

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

Three settings were especially high and could lead to paging/swapping. MySQL performance suffers terribly when paged/swapped. It is better to lower settings than to have that happen.

Details and Minor Issues

( innodb_buffer_pool_size / _ram ) = 1024M / 16384M = 6.2% -- % of RAM used for InnoDB buffer_pool

( Innodb_pages_written / Innodb_buffer_pool_write_requests ) = 29,673,396 / 196667000 = 15.1% -- Write requests that had to hit disk -- Check innodb_buffer_pool_size

( Innodb_buffer_pool_read_ahead_evicted / Innodb_buffer_pool_read_ahead ) = 4,691,916 / 30144135 = 15.6% -- Utility of read_ahead. -- Turn off 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 -- Ratio

( Uptime / 60 * innodb_log_file_size / Innodb_os_log_written ) = 4,656,056 / 60 * 256M / 34746359296 = 599 -- Minutes between InnoDB log rotations Beginning with 5.6.8, this can be changed dynamically; be sure to also change my.cnf. -- (The recommendation of 60 minutes between rotations is somewhat arbitrary.)

( table_open_cache ) = 4,096 -- Number of table descriptors to cache -- Several hundred is usually good.

( innodb_stats_on_metadata ) = ON -- Re-analyze table when touching stats. -- ON is likely to slow down certain SHOWs and information_schema accesses.

( join_buffer_size ) = 2,147,483,648 = 2048MB -- 0-N per thread. May speed up JOINs (better to fix queries/indexes) (all engines) Used for index scan, range index scan, full table scan, each full JOIN, etc. -- Use the default.

( join_buffer_size / _ram ) = 2,147,483,648 / 16384M = 12.5% -- 0-N per thread. May speed up JOINs (better to fix queries/indexes) (all engines) Used for index scan, range index scan, full table scan, each full JOIN, etc. -- Decrease join_buffer_size to avoid memory pressure. Suggest less than 1% of RAM.

( min( tmp_table_size, max_heap_table_size ) / _ram ) = min( 2147483648, 256M ) / 16384M = 1.6% -- Percent of RAM to allocate when needing MEMORY table (per table), or temp table inside a SELECT (per temp table per some SELECTs). Too high may lead to swapping. -- Decrease tmp_table_size and max_heap_table_size to, say, 1% of ram.

( (key_buffer_size - 1.2 * Key_blocks_used * 1024) / _ram ) = (4294967296 - 1.2 * 864035 * 1024) / 16384M = 18.8% -- Percent of RAM wasted in key_buffer. -- Decrease key_buffer_size

( Key_blocks_used * 1024 / key_buffer_size ) = 864,035 * 1024 / 4294967296 = 20.6% -- Percent of key_buffer used . High-water-mark. -- Lower key_buffer_size to avoid unnecessary memory usage.

( Key_writes / Key_write_requests ) = 7,764,232 / 105012022 = 7.4% -- key_buffer effectiveness for writes

( query_cache_size ) = 1024M -- Size of QC -- Too small = not of much use. Too large = too much overhead. Recommend either 0 or no more than 50M.

( Created_tmp_disk_tables / (Created_tmp_disk_tables + Created_tmp_tables) ) = 2,151,917 / (2151917 + 3928177) = 35.4% -- Percent of temp tables that spilled to disk

( tmp_table_size ) = 2,147,483,648 = 2048MB -- Limit on size of MEMORY temp tables used to support a SELECT -- Decrease tmp_table_size to avoid running out of RAM. Perhaps no more than 64M.

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

( max_connect_errors ) = 100,000 -- A small protection against hackers. -- Perhaps no more than 200.

Dav answered 9/12, 2015 at 18:27 Comment(4)
Wow, that's a whole lot of data to work on. Did you use some kind of tool for analysis? I'm gonna discuss this with my hosting provider as some of those settings don't mean much to me ;-) Thank you!Halter
I forgot to add: 23 issues flagged, out of 144 computed Variables/Status/Expressions. Settings not mentioned are mostly OK. Yeah it is an automated tool, but I am struggling with making it really automated. The "More Important Issues" is a hand-done interpretation of the gibberish put out by the tool. It can't automatically resolve contradictions ("increase X; no decrease X") and interactions. For now, I provide it as a service.Dav
I logged in just to give this answer a thumbsup. This is a lot of information. Thanks @RickJamesOvermatch
@rickydj - If you would like a similar review of your system, start a new Question and provide the two SHOWs, plus ram size.Dav
D
2

If you have a "large" db app, this implies that there are a lot of writes? Keep in mind that all QC entries for a table are purged when any write to that table occurs. That implies that you may not benefit from the QC.

Furthermore, that purging takes effort, thereby slowing down the system. For query_cache_size under, say, 50M, the cost is not big. But if you have a 1G QC, you will spend more time purging the QC than using it!

The actual queries in the QC cannot be discovered. Here is what might help: Put SQL_NO_CACHE on any queries involving table(s) that change frequently. Put SQL_CACHE on the rest. Then use DEMAND. Monitor STATUS LIKE Qc%.

If you would like an analysis of your system (QC and otherwise), provide (1) RAM size, (2) SHOW VARIABLES, (3) SHOW GLOBAL STATUS after the server has been up for at least a day.

Dav answered 9/12, 2015 at 0:27 Comment(1)
Thanks for your help, additional server info attached to my original question. Would be glad if you could analyse the data.Halter

© 2022 - 2024 — McMap. All rights reserved.