选择count(*)在“大”结果集上给出零

时间:2015-06-30 08:52:59

标签: mysql select count innodb mysql-5.6

我有一个很宽的(很多列)表,里面有大约400,000行,InnoDb,UTF8MB4_Unicode,大约60列和11个索引。

见下面的定义。

当我执行duration时,它仅为表中没有那么多行的用户提供正确的计数。

如果我 RECREATE + ANALYZE ,即当你在InnoDb表上执行SELECT count(*) FROM table_name WHERE user_id = X时MySQL会做什么,它可以很好地计数显示正确的值,直到我们在表中插入新行再次,然后计数关闭。

我在这里遗漏了一些关于SELECT计数的“常识”,或者这是最新MySQL版本中的一个错误,还是因为我的表有很多索引或者其他东西。在升级到5.6.25之前,我从未遇到过这个问题。

我应该提到 my.cnf 只有128 Mb的InnoDb_Pool_Buffer,这可能是针对这个大小的db(不是很大但是从mysqldump gzipped大约300mb)的尺寸。

非常感谢你花时间回答。

编辑从mysqltuner.pl添加输出 - 我非常感谢你们解释它们的任何帮助(我只有Innodb表,没有MyISAM)。请注意它是如何说我有44个碎片表,但我最近刚刚对它们进行了重新创建+分析。这是由于其他参数设置得太低了吗?再次感谢。

删除了Mysql Tuner输出,为更多相关数字提供了位置。

表定义(匿名):

已删除表定义,因为不再相关

EDIT2 根据要求,提供SHOW GLOBAL STATUS和SHOW VARIABLES的输出

OPTIMIZE TABLE

显示变量

Aborted_clients 0
Aborted_connects    5
Binlog_cache_disk_use   0
Binlog_cache_use    0
Binlog_stmt_cache_disk_use  0
Binlog_stmt_cache_use   0
Bytes_received  1392447055
Bytes_sent  20991720246
Com_admin_commands  1739
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 10
Com_alter_tablespace    0
Com_alter_user  0
Com_analyze 0
Com_begin   0
Com_binlog  0
Com_call_procedure  1
Com_change_db   1567913
Com_change_master   0
Com_check   0
Com_checksum    0
Com_commit  0
Com_create_db   0
Com_create_event    0
Com_create_function 0
Com_create_index    0
Com_create_procedure    1
Com_create_server   0
Com_create_table    2
Com_create_trigger  0
Com_create_udf  0
Com_create_user 0
Com_create_view 0
Com_dealloc_sql 0
Com_delete  7717
Com_delete_multi    0
Com_do  0
Com_drop_db 0
Com_drop_event  0
Com_drop_function   0
Com_drop_index  0
Com_drop_procedure  2
Com_drop_server 0
Com_drop_table  2
Com_drop_trigger    0
Com_drop_user   0
Com_drop_view   0
Com_empty_query 0
Com_execute_sql 0
Com_flush   0
Com_get_diagnostics 0
Com_grant   0
Com_ha_close    0
Com_ha_open 0
Com_ha_read 0
Com_help    0
Com_insert  169112
Com_insert_select   37
Com_install_plugin  0
Com_kill    0
Com_load    0
Com_lock_tables 6
Com_optimize    0
Com_preload_keys    0
Com_prepare_sql 0
Com_purge   0
Com_purge_before_date   0
Com_release_savepoint   0
Com_rename_table    0
Com_rename_user 0
Com_repair  0
Com_replace 0
Com_replace_select  0
Com_reset   0
Com_resignal    0
Com_revoke  0
Com_revoke_all  0
Com_rollback    0
Com_rollback_to_savepoint   0
Com_savepoint   0
Com_select  5721646
Com_set_option  3032
Com_signal  0
Com_show_binlog_events  0
Com_show_binlogs    0
Com_show_charsets   0
Com_show_collations 35
Com_show_create_db  86
Com_show_create_event   0
Com_show_create_func    0
Com_show_create_proc    0
Com_show_create_table   587
Com_show_create_trigger 0
Com_show_databases  3
Com_show_engine_logs    0
Com_show_engine_mutex   0
Com_show_engine_status  0
Com_show_events 18
Com_show_errors 0
Com_show_fields 600
Com_show_function_code  0
Com_show_function_status    0
Com_show_grants 0
Com_show_keys   88
Com_show_master_status  0
Com_show_open_tables    0
Com_show_plugins    0
Com_show_privileges 0
Com_show_procedure_code 0
Com_show_procedure_status   0
Com_show_processlist    0
Com_show_profile    0
Com_show_profiles   0
Com_show_relaylog_events    0
Com_show_slave_hosts    0
Com_show_slave_status   0
Com_show_status 1843
Com_show_storage_engines    3
Com_show_table_status   589
Com_show_tables 6
Com_show_triggers   533
Com_show_variables  1956
Com_show_warnings   0
Com_slave_start 0
Com_slave_stop  0
Com_stmt_close  0
Com_stmt_execute    0
Com_stmt_fetch  0
Com_stmt_prepare    0
Com_stmt_reprepare  0
Com_stmt_reset  0
Com_stmt_send_long_data 0
Com_truncate    36
Com_uninstall_plugin    0
Com_unlock_tables   6
Com_update  640052
Com_update_multi    0
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
Connection_errors_accept    0
Connection_errors_internal  0
Connection_errors_max_connections   0
Connection_errors_peer_address  0
Connection_errors_select    0
Connection_errors_tcpwrap   0
Connections 1567347
Created_tmp_disk_tables 247145
Created_tmp_files   20174
Created_tmp_tables  386918
Delayed_errors  0
Delayed_insert_threads  0
Delayed_writes  0
Flush_commands  1
Handler_commit  6537068
Handler_delete  33452
Handler_discover    0
Handler_external_lock   20879224
Handler_mrr_init    0
Handler_prepare 0
Handler_read_first  1751492
Handler_read_key    5384829832
Handler_read_last   211053
Handler_read_next   19630786637
Handler_read_prev   6110703982
Handler_read_rnd    73007565
Handler_read_rnd_next   11782800964
Handler_rollback    477
Handler_savepoint   0
Handler_savepoint_rollback  0
Handler_update  3632409114
Handler_write   20236858098
Innodb_buffer_pool_dump_status  not started
Innodb_buffer_pool_load_status  not started
Innodb_buffer_pool_pages_data   45561
Innodb_buffer_pool_bytes_data   746471424
Innodb_buffer_pool_pages_dirty  10
Innodb_buffer_pool_bytes_dirty  163840
Innodb_buffer_pool_pages_flushed    2075723
Innodb_buffer_pool_pages_free   1024
Innodb_buffer_pool_pages_misc   4614
Innodb_buffer_pool_pages_total  51199
Innodb_buffer_pool_read_ahead_rnd   0
Innodb_buffer_pool_read_ahead   337427
Innodb_buffer_pool_read_ahead_evicted   4272
Innodb_buffer_pool_read_requests    50351166042
Innodb_buffer_pool_reads    2496152
Innodb_buffer_pool_wait_free    0
Innodb_buffer_pool_write_requests   25099776
Innodb_data_fsyncs  680776
Innodb_data_pending_fsyncs  0
Innodb_data_pending_reads   0
Innodb_data_pending_writes  0
Innodb_data_read    46916046848
Innodb_data_reads   2863706
Innodb_data_writes  3201512
Innodb_data_written 69819479040
Innodb_dblwr_pages_written  2075723
Innodb_dblwr_writes 103028
Innodb_have_atomic_builtins ON
Innodb_log_waits    0
Innodb_log_write_requests   3054739
Innodb_log_writes   914722
Innodb_os_log_fsyncs    244890
Innodb_os_log_pending_fsyncs    0
Innodb_os_log_pending_writes    0
Innodb_os_log_written   1752017408
Innodb_page_size    16384
Innodb_pages_created    59717
Innodb_pages_read   2863523
Innodb_pages_written    2075723
Innodb_row_lock_current_waits   0
Innodb_row_lock_time    7419
Innodb_row_lock_time_avg    390
Innodb_row_lock_time_max    1422
Innodb_row_lock_waits   19
Innodb_rows_deleted 33451
Innodb_rows_inserted    5432696
Innodb_rows_read    29436124622
Innodb_rows_updated 596060
Innodb_num_open_files   118
Innodb_truncated_status_writes  0
Innodb_available_undo_logs  128
Key_blocks_not_flushed  0
Key_blocks_unused   6698
Key_blocks_used 6698
Key_read_requests   45635462325
Key_reads   6589434
Key_write_requests  12131142501
Key_writes  9617220
Last_query_cost 0.000000
Last_query_partial_plans    0
Max_used_connections    17
Not_flushed_delayed_rows    0
Open_files  46
Open_streams    0
Open_table_definitions  166
Open_tables 287
Opened_files    1009110
Opened_table_definitions    226
Opened_tables   397
Performance_schema_accounts_lost    0
Performance_schema_cond_classes_lost    0
Performance_schema_cond_instances_lost  0
Performance_schema_digest_lost  0
Performance_schema_file_classes_lost    0
Performance_schema_file_handles_lost    0
Performance_schema_file_instances_lost  0
Performance_schema_hosts_lost   0
Performance_schema_locker_lost  0
Performance_schema_mutex_classes_lost   0
Performance_schema_mutex_instances_lost 0
Performance_schema_rwlock_classes_lost  0
Performance_schema_rwlock_instances_lost    0
Performance_schema_session_connect_attrs_lost   0
Performance_schema_socket_classes_lost  0
Performance_schema_socket_instances_lost    0
Performance_schema_stage_classes_lost   0
Performance_schema_statement_classes_lost   0
Performance_schema_table_handles_lost   0
Performance_schema_table_instances_lost 0
Performance_schema_thread_classes_lost  0
Performance_schema_thread_instances_lost    0
Performance_schema_users_lost   0
Prepared_stmt_count 0
Qcache_free_blocks  1
Qcache_free_memory  1031352
Qcache_hits 0
Qcache_inserts  0
Qcache_lowmem_prunes    0
Qcache_not_cached   5720393
Qcache_queries_in_cache 0
Qcache_total_blocks 1
Queries 9771703
Questions   9640606
Select_full_join    4608
Select_full_range_join  0
Select_range    385947
Select_range_check  0
Select_scan 1745605
Slave_heartbeat_period   
Slave_last_heartbeat     
Slave_open_temp_tables  0
Slave_received_heartbeats    
Slave_retried_transactions   
Slave_running   OFF
Slow_launch_threads 0
Slow_queries    16
Sort_merge_passes   17244
Sort_range  214894
Sort_rows   199889850
Sort_scan   260475
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_server_not_after     
Ssl_server_not_before    
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   10362193
Table_locks_waited  49
Table_open_cache_hits   10367123
Table_open_cache_misses 396
Table_open_cache_overflows  0
Tc_log_max_pages_used   0
Tc_log_page_size    0
Tc_log_page_waits   0
Threads_cached  7
Threads_connected   3
Threads_created 30
Threads_running 1
Uptime  520269
Uptime_since_flush_status   520269

1 个答案:

答案 0 :(得分:0)

忽略“碎片化” - InnoDB表基本上不是问题。不要运行OPTIMIZE TABLE。 (如果你这样做,请在之前和之后做SHOW TABLE STATUS - 看看它没有多大帮助。)

你有4GB的内存? innodb_buffer_pool_size可能会提升到600M。

禁用质量控制 - 对于生产系统来说,这通常是“好的”。

很少需要

BIGINT(8字节),除非你期望有超过40亿的东西(INT UNSIGNED - 4字节 - 最多40亿)。

如果您想要更多mysqltuner提供的分析,请向我提供SHOW VARIABLES;SHOW GLOBAL STATUS;(在服务器启动至少一天后)。

回到主要问题......我认为这个特定查询没有理由错:

SELECT count(*) FROM table_name WHERE user_id = X

如果您执行EXPLAIN SELECT ...,您会看到它将使用索引。 InnoDB中的索引永远不会被破坏。

您建议5.6.25可能有问题。是的,总有这样的机会。 (MySQL因没有回归而享有良好的声誉,这当然也是如此。)请关注bugs.mysql.com。在那里提交一个bug。考虑降级。

VARIABLES / STATUS分析

在设置中找不到很多:

(Key_reads)= 6,589,434 / 520269 = 13 / sec - MyISAM索引读取(从磁盘)速率
(Key_reads + Key_writes)=(6589434 + 9617220)/ 520269 = 31 / sec - MyISAM索引I / O率
你正在使用很多MyISAM; key_buffer_size (8M)应该增加到100M。

(Created_tmp_disk_tables /(Created_tmp_disk_tables + Created_tmp_tables))= 247,145 /(247145 + 386918)= 39.0% - 溢出到磁盘的临时表的百分比
- 也许增加tmp_table_size和max_heap_table_size;避免blob等。
(Select_scan)= 1,745,605 / 520269 = 3.4 /秒 - 全表扫描
- 添加索引/优化查询(除非它们是小表) (Select_scan / Com_select)= 1,745,605 / 5721646 = 30.5% - 执行全表扫描的选择的百分比。 (可能会受到存储例程的愚弄。)
- 添加索引/优化查询

(open_files_limit)= 1,024 - ulimit -n
- 要允许更多文件,请更改 ulimit 或/etc/security/limits.conf或sysctl.conf(kern.maxfiles& kern.maxfilesperproc)或其他内容(取决于操作系统)

(连接)= 1,567,347 / 520269 = 3 /秒 - 连接
- 增加wait_timeout;使用汇集?

您有半个查询缓存。您应该同时设置query_cache_type = OFF和query_cache_size = 0。 (根据谣言)QC代码中的“错误”会留下一些代码,除非您关闭这两个设置。