Mysql 8.0.18 hash join测试(推荐)
时间:2020-11-03 13:43 作者:admin610456
Hash Join
Hash Join 不需要任何索引来执行,并且在大多数情况下比当前的块嵌套循环算法更有效。
下面通过实例代码给大家介绍mysql/' target='_blank'>mysql 8.0.18 hash join测试,具体内容如下所示:
CREATE TABLE COLUMNS_hj as select * from information_schema.`COLUMNS`;INSERT INTO COLUMNS SELECT * FROM COLUMNS; -- 最后一次插入25万行CREATE TABLE COLUMNS_hj2 as select * from information_schema.`COLUMNS`;
explain format=treeSELECT COUNT(c1. PRIVILEGES), SUM(c1.ordinal_position)FROM COLUMNS_hj c1, COLUMNS_hj2 c2WHERE c1.table_name = c2.table_nameAND c1.column_name = c2.column_nameGROUP BY c1.table_name, c1.column_nameORDER BY c1.table_name, c1.column_name;
必须使用format=tree(8.0.16的新特性)才能查看hash join的执行计划:
-> Sort: <temporary>.TABLE_NAME, <temporary>.COLUMN_NAME -> Table scan on <temporary> -> Aggregate using temporary table -> Inner hash join (c1.`COLUMN_NAME` = c2.`COLUMN_NAME`), (c1.`TABLE_NAME` = c2.`TABLE_NAME`) (cost=134217298.97 rows=13421218) -> Table scan on c1 (cost=1.60 rows=414619) -> Hash -> Table scan on c2 (cost=347.95 rows=3237)
set join_buffer_size=1048576000;SELECT COUNT(c1. PRIVILEGES), SUM(c1.ordinal_position)FROM COLUMNS_hj c1, COLUMNS_hj2 c2WHERE c1.table_name = c2.table_nameAND c1.column_name = c2.column_nameGROUP BY c1.table_name, c1.column_nameORDER BY c1.table_name, c1.column_name;
1.5秒左右。
再来看BNL,先创建索引(分别优化了,再对比效果才公平)。
alter table columns_hj drop index idx_columns_hj;alter table columns_hj2 drop index idx_columns_hj2;create index idx_columns_hj on columns_hj(table_name,column_name);create index idx_columns_hj2 on columns_hj2(table_name,column_name);-> Sort: <temporary>.TABLE_NAME, <temporary>.COLUMN_NAME -> Table scan on <temporary> -> Aggregate using temporary table -> Nested loop inner join (cost=454325.17 rows=412707) -> Filter: ((c2.`TABLE_NAME` is not null) and (c2.`COLUMN_NAME` is not null)) (cost=347.95 rows=3237) -> Table scan on c2 (cost=347.95 rows=3237) -> Index lookup on c1 using idx_COLUMNS_hj (TABLE_NAME=c2.`TABLE_NAME`, COLUMN_NAME=c2.`COLUMN_NAME`) (cost=127.50 rows=127)
大约4.5秒。可见hash join效果还是杠杠的。
不得不吐槽下MySQL的优化器提示,貌似HASH_JOIN/NO_HASH_JOIN都不生效。
除了hash_join外,mysql 8.0.3引入的SET_VAR优化器提示还是很好用的,可用来设置语句级参数(oracle支持,mariadb记得也支持了的),如下:
mysql> select /*+ set_var(optimizer_switch='index_merge=off') set_var(join_buffer_size=4M) */ c_id from customer limit 1;
SET_VAR支持的变量列表:
auto_increment_incrementauto_increment_offsetbig_tablesbulk_insert_buffer_sizedefault_tmp_storage_enginediv_precision_incrementend_markers_in_jsoneq_range_index_dive_limitforeign_key_checksgroup_concat_max_leninsert_idinternal_tmp_mem_storage_enginejoin_buffer_sizelock_wait_timeoutmax_error_countmax_execution_timemax_heap_table_sizemax_join_sizemax_length_for_sort_datamax_points_in_geometrymax_seeks_for_keymax_sort_lengthoptimizer_prune_leveloptimizer_search_depth variablesoptimizer_switchrange_alloc_block_sizerange_optimizer_max_mem_sizeread_buffer_sizeread_rnd_buffer_sizesort_buffer_sizesql_auto_is_nullsql_big_selectssql_buffer_resultsql_modesql_safe_updatessql_select_limittimestamptmp_table_sizeupdatable_views_with_limitunique_checkswindowing_use_high_precision
总结
以上所述是小编给大家介绍的Mysql 8.0.18 hash join测试,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
(责任编辑:admin)