Mysql 为什么此组会导致文件排序?
我有一组高度相关的表,我已经将它们展平为两个InnoDB表: mysql> desc WPropertyCube; +--------------------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------------+---------------------+------+-----+---------+----------------+ | id | bigint(20) unsigned | NO | PRI | NULL | auto_increment | | lineOfBusinessId | bigint(20) unsigned | NO | MUL | NULL | | | txtProperty1 | varchar(125) | YES | | NULL | | | txtProperty2 | varchar(125) | YES | | NULL | | | txtProperty3 | varchar(125) | YES | | NULL | | ... | txtProperty20 | varchar(125) | YES | | NULL | | | lookupPropertyId1 | bigint(20) unsigned | YES | | NULL | | | lookupPropertyId2 | bigint(20) unsigned | YES | | NULL | | | lookupPropertyId3 | bigint(20) unsigned | YES | | NULL | | ... | lookupPropertyId30 | bigint(20) unsigned | YES | | NULL | | +--------------------+---------------------+------+-----+---------+----------------+ mysql> show indexes from WPropertyCube; +---------------+------------+---------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +---------------+------------+---------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | WPropertyCube | 0 | PRIMARY | 1 | id | A | 379383 | NULL | NULL | | BTREE | | | | WPropertyCube | 1 | WPropertyCube_idx01 | 1 | lineOfBusinessId | A | 204 | NULL | NULL | | BTREE | | | +---------------+------------+---------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 此查询正在将约30K条记录聚合为约1000条记录。请注意,没有索引用于访问Mysql 为什么此组会导致文件排序?,mysql,Mysql,我有一组高度相关的表,我已经将它们展平为两个InnoDB表: mysql> desc WPropertyCube; +--------------------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------------+
WMeasureCube
,更糟糕的是,还有一个文件排序是sloowww。查询时间范围在30-150秒之间。令人困惑的是,如果我删除了分组BY
:
mysql> explain SELECT wmc.actionDate,
-> SUM(wmc.measure7),
-> SUM(wmc.measure8),
-> SUM(wmc.measure9)
-> FROM WMeasureCube wmc
-> INNER JOIN WPropertyCube wpc
-> ON wmc.propertyCubeId = wpc.id
-> WHERE wpc.lineOfBusinessId IN ( 1, 2, 3, 4 )
-> AND wmc.actionDate BETWEEN '2010-06-28' AND '2010-09-26'
-> ORDER BY wmc.actionDate;
+----+-------------+-------+--------+-----------------------------+---------+---------+---------------------------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+-----------------------------+---------+---------+---------------------------+----------+-------------+
| 1 | SIMPLE | wmc | ALL | PRIMARY,WMeasureCube_idx1 | NULL | NULL | NULL | 81680372 | Using where |
| 1 | SIMPLE | wpc | eq_ref | PRIMARY,WPropertyCube_idx01 | PRIMARY | 8 | db.wmc.propertyCubeId | 1 | Using where |
+----+-------------+-------+--------+-----------------------------+---------+---------+---------------------------+----------+-------------+
mysql>解释选择wmc.actionDate,
->总和(wmc.测量7),
->总和(wmc.测量值8),
->总和(wmc.测量值9)
->来自WMeasureCube wmc
->内连接WPropertyCube wpc
->在wmc.propertyCubeId=wpc.id上
->其中wpc.LineofBusiness ID位于(1、2、3、4)中
->和wmc。行动日期介于“2010-06-28”和“2010-09-26”之间
->wmc订购。行动日期;
+----+-------------+-------+--------+-----------------------------+---------+---------+---------------------------+----------+-------------+
|id |选择|类型|类型|可能的|键|键|列|参考|行|额外|
+----+-------------+-------+--------+-----------------------------+---------+---------+---------------------------+----------+-------------+
|1 | SIMPLE | wmc | ALL | PRIMARY,WMeasureCube_idx1 | NULL | NULL | NULL | 81680372 |使用where|
|1 |简单| wpc |等式|参考|主要,WPropertyCube | idx01 |主要| 8 | db.wmc.propertyCubeId | 1 |使用其中|
+----+-------------+-------+--------+-----------------------------+---------+---------+---------------------------+----------+-------------+
仍然没有使用索引,但至少没有文件排序。此查询在不到一秒钟的时间内持续运行。特别奇怪的是,在执行时间方面,按一组列排序应该与按同一组分组一致
如何加快查询速度
我试图添加所有服务器变量,但它们占用了太多的空间。所以,我添加了一些我认为最有帮助的
mysql> show variables;
+---------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value |
+---------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------+
| big_tables | OFF |
| binlog_cache_size | 4194304 |
| binlog_direct_non_transactional_updates | OFF |
| binlog_format | STATEMENT |
| bulk_insert_buffer_size | 8388608 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| default_storage_engine | InnoDB |
| innodb_adaptive_flushing | ON |
| innodb_adaptive_hash_index | ON |
| innodb_additional_mem_pool_size | 16777216 |
| innodb_autoextend_increment | 8 |
| innodb_autoinc_lock_mode | 1 |
| innodb_buffer_pool_instances | 1 |
| innodb_buffer_pool_size | 6442450944 |
| innodb_change_buffering | all |
| 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 | Barracuda |
| innodb_file_format_check | ON |
| innodb_file_format_max | Barracuda |
| innodb_file_per_table | ON |
| innodb_flush_log_at_trx_commit | 0 |
| innodb_flush_method | |
| innodb_force_recovery | 0 |
| innodb_io_capacity | 200 |
| innodb_lock_wait_timeout | 120 |
| innodb_locks_unsafe_for_binlog | OFF |
| innodb_log_buffer_size | 16777216 |
| innodb_log_file_size | 268435456 |
| innodb_log_files_in_group | 3 |
| innodb_log_group_home_dir | ./ |
| innodb_max_dirty_pages_pct | 90 |
| 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_purge_batch_size | 20 |
| innodb_purge_threads | 0 |
| innodb_read_ahead_threshold | 56 |
| innodb_read_io_threads | 8 |
| innodb_replication_delay | 0 |
| innodb_rollback_on_timeout | OFF |
| innodb_spin_wait_delay | 6 |
| innodb_stats_on_metadata | ON |
| innodb_stats_sample_pages | 8 |
| innodb_strict_mode | OFF |
| innodb_support_xa | OFF |
| innodb_sync_spin_loops | 30 |
| innodb_table_locks | ON |
| innodb_thread_concurrency | 0 |
| innodb_thread_sleep_delay | 10000 |
| innodb_use_native_aio | ON |
| innodb_use_sys_malloc | ON |
| innodb_version | 1.1.2 |
| innodb_write_io_threads | 8 |
| join_buffer_size | 8388608 |
| key_buffer_size | 8388608 |
| large_files_support | ON |
| large_page_size | 0 |
| large_pages | OFF |
| last_insert_id | 0 |
| max_binlog_cache_size | 18446744073709547520 |
| max_binlog_size | 1073741824 |
| max_heap_table_size | 536870912 |
| max_join_size | 18446744073709551615 |
| max_length_for_sort_data | 1024 |
| max_sort_length | 1024 |
| max_sp_recursion_depth | 0 |
| max_tmp_tables | 32 |
| optimizer_prune_level | 1 |
| optimizer_search_depth | 62 |
| optimizer_switch | index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on |
| preload_buffer_size | 32768 |
| profiling | OFF |
| profiling_history_size | 15 |
| protocol_version | 10 |
| pseudo_thread_id | 13 |
| query_alloc_block_size | 8192 |
| query_cache_limit | 8388608 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 134217728 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
| query_prealloc_size | 8192 |
| rand_seed1 | 0 |
| rand_seed2 | 0 |
| range_alloc_block_size | 4096 |
| read_buffer_size | 8388608 |
| sort_buffer_size | 16777216 |
| sql_big_selects | ON |
| sql_big_tables | OFF |
| sql_max_join_size | 18446744073709551615 |
| storage_engine | InnoDB |
| tmp_table_size | 536870912 |
| tmpdir | /dev/shm |
| tx_isolation | REPEATABLE-READ |
| version | 5.5.6-rc-log |
| version_comment | MySQL Community Server (GPL) |
| version_compile_machine | x86_64 |
| version_compile_os | linux2.6 |
+---------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------+
mysql>显示变量;
+---------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------+
|变量名称|值|
+---------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------+
|大桌子|关|
|binlog_缓存|4194304|
|binlog|直接|非|事务|更新|关闭|
|binlog|U格式|语句|
|批量插入缓冲区大小8388608|
|字符集数据库utf8|
|字符集文件系统二进制|
|默认存储引擎InnoDB|
|innodb|u自适应|u刷新|打开|
|innodb_自适应哈希索引|打开|
|innodb_附加_成员_池|16777216|
|innodb_自动扩展|u增量| 8|
|innodb|u autoinc|u锁定模式| 1|
|innodb|u缓冲区|u池|u实例| 1|
|innodb_缓冲池_池|6442450944|
|innodb|u change|u buffering|all|
|innodb|U校验和| ON|
|innodb|u提交|u并发| 0|
|innodb|U并发|U票证| 500
mysql> explain SELECT wmc.actionDate,
-> SUM(wmc.measure7),
-> SUM(wmc.measure8),
-> SUM(wmc.measure9)
-> FROM WMeasureCube wmc
-> INNER JOIN WPropertyCube wpc
-> ON wmc.propertyCubeId = wpc.id
-> WHERE wpc.lineOfBusinessId IN ( 1, 2, 3, 4 )
-> AND wmc.actionDate BETWEEN '2010-06-28' AND '2010-09-26'
-> ORDER BY wmc.actionDate;
+----+-------------+-------+--------+-----------------------------+---------+---------+---------------------------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+-----------------------------+---------+---------+---------------------------+----------+-------------+
| 1 | SIMPLE | wmc | ALL | PRIMARY,WMeasureCube_idx1 | NULL | NULL | NULL | 81680372 | Using where |
| 1 | SIMPLE | wpc | eq_ref | PRIMARY,WPropertyCube_idx01 | PRIMARY | 8 | db.wmc.propertyCubeId | 1 | Using where |
+----+-------------+-------+--------+-----------------------------+---------+---------+---------------------------+----------+-------------+
mysql> show variables;
+---------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value |
+---------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------+
| big_tables | OFF |
| binlog_cache_size | 4194304 |
| binlog_direct_non_transactional_updates | OFF |
| binlog_format | STATEMENT |
| bulk_insert_buffer_size | 8388608 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| default_storage_engine | InnoDB |
| innodb_adaptive_flushing | ON |
| innodb_adaptive_hash_index | ON |
| innodb_additional_mem_pool_size | 16777216 |
| innodb_autoextend_increment | 8 |
| innodb_autoinc_lock_mode | 1 |
| innodb_buffer_pool_instances | 1 |
| innodb_buffer_pool_size | 6442450944 |
| innodb_change_buffering | all |
| 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 | Barracuda |
| innodb_file_format_check | ON |
| innodb_file_format_max | Barracuda |
| innodb_file_per_table | ON |
| innodb_flush_log_at_trx_commit | 0 |
| innodb_flush_method | |
| innodb_force_recovery | 0 |
| innodb_io_capacity | 200 |
| innodb_lock_wait_timeout | 120 |
| innodb_locks_unsafe_for_binlog | OFF |
| innodb_log_buffer_size | 16777216 |
| innodb_log_file_size | 268435456 |
| innodb_log_files_in_group | 3 |
| innodb_log_group_home_dir | ./ |
| innodb_max_dirty_pages_pct | 90 |
| 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_purge_batch_size | 20 |
| innodb_purge_threads | 0 |
| innodb_read_ahead_threshold | 56 |
| innodb_read_io_threads | 8 |
| innodb_replication_delay | 0 |
| innodb_rollback_on_timeout | OFF |
| innodb_spin_wait_delay | 6 |
| innodb_stats_on_metadata | ON |
| innodb_stats_sample_pages | 8 |
| innodb_strict_mode | OFF |
| innodb_support_xa | OFF |
| innodb_sync_spin_loops | 30 |
| innodb_table_locks | ON |
| innodb_thread_concurrency | 0 |
| innodb_thread_sleep_delay | 10000 |
| innodb_use_native_aio | ON |
| innodb_use_sys_malloc | ON |
| innodb_version | 1.1.2 |
| innodb_write_io_threads | 8 |
| join_buffer_size | 8388608 |
| key_buffer_size | 8388608 |
| large_files_support | ON |
| large_page_size | 0 |
| large_pages | OFF |
| last_insert_id | 0 |
| max_binlog_cache_size | 18446744073709547520 |
| max_binlog_size | 1073741824 |
| max_heap_table_size | 536870912 |
| max_join_size | 18446744073709551615 |
| max_length_for_sort_data | 1024 |
| max_sort_length | 1024 |
| max_sp_recursion_depth | 0 |
| max_tmp_tables | 32 |
| optimizer_prune_level | 1 |
| optimizer_search_depth | 62 |
| optimizer_switch | index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on |
| preload_buffer_size | 32768 |
| profiling | OFF |
| profiling_history_size | 15 |
| protocol_version | 10 |
| pseudo_thread_id | 13 |
| query_alloc_block_size | 8192 |
| query_cache_limit | 8388608 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 134217728 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
| query_prealloc_size | 8192 |
| rand_seed1 | 0 |
| rand_seed2 | 0 |
| range_alloc_block_size | 4096 |
| read_buffer_size | 8388608 |
| sort_buffer_size | 16777216 |
| sql_big_selects | ON |
| sql_big_tables | OFF |
| sql_max_join_size | 18446744073709551615 |
| storage_engine | InnoDB |
| tmp_table_size | 536870912 |
| tmpdir | /dev/shm |
| tx_isolation | REPEATABLE-READ |
| version | 5.5.6-rc-log |
| version_comment | MySQL Community Server (GPL) |
| version_compile_machine | x86_64 |
| version_compile_os | linux2.6 |
+---------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------+
+----+-------------+-------+-------+-----------------------------+---------------------+---------+---------------+--------+-----------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+-----------------------------+---------------------+---------+---------------+--------+-----------------------------------------------------------+
| 1 | SIMPLE | wpc | index | PRIMARY,WPropertyCube_idx01 | WPropertyCube_idx01 | 8 | NULL | 377500 | Using where; Using index; Using temporary; Using filesort |
| 1 | SIMPLE | wmc | ref | PRIMARY,WMeasureCube_idx1 | PRIMARY | 8 | db.wpc.id | 49 | Using where |
+----+-------------+-------+-------+-----------------------------+---------------------+---------+---------------+--------+-----------------------------------------------------------+