Commit 4b0cce48 authored by unknown's avatar unknown

BUG#17314: Can't use index_merge/intersection for MERGE tables

1. Fix index access costs for MERGE tables, set block_size=myisam_block_size/#underlying_tables 
   instead of 0 which it was before.
2. Make index scans on MERGE table to return records in (key_tuple, merge_table_rowid) order, 
   instead of just (key_tuple) order. This makes an index scan on MERGE table to be truly a ROR-scan
   which is a requirement for index_merge union/intersection.


myisammrg/myrg_queue.c:
  BUG#17314: Make index scans on MERGE table return records ordered by (keytuple, merge_table_rowid).
mysql-test/r/index_merge.result:
  Testcase for BUG#17314
mysql-test/r/merge.result:
  BUG#17314: update testcase result
mysql-test/t/index_merge.test:
  Testcase for BUG#17314
sql/ha_myisammrg.cc:
  BUG#17314: For MERGE tables, set handler::block_size to myisam_block_size/#underlying_tables, and not to 0.
parent 96268d4a
...@@ -18,12 +18,26 @@ ...@@ -18,12 +18,26 @@
static int queue_key_cmp(void *keyseg, byte *a, byte *b) static int queue_key_cmp(void *keyseg, byte *a, byte *b)
{ {
MI_INFO *aa=((MYRG_TABLE *)a)->table; MYRG_TABLE *ma= (MYRG_TABLE *)a;
MI_INFO *bb=((MYRG_TABLE *)b)->table; MYRG_TABLE *mb= (MYRG_TABLE *)b;
MI_INFO *aa= ma->table;
MI_INFO *bb= mb->table;
uint not_used[2]; uint not_used[2];
int ret= ha_key_cmp((HA_KEYSEG *)keyseg, aa->lastkey, bb->lastkey, int ret= ha_key_cmp((HA_KEYSEG *)keyseg, aa->lastkey, bb->lastkey,
USE_WHOLE_KEY, SEARCH_FIND, not_used); USE_WHOLE_KEY, SEARCH_FIND, not_used);
return ret < 0 ? -1 : ret > 0 ? 1 : 0; if (ret < 0)
return -1;
if (ret > 0)
return 1;
/*
If index tuples have the same values, let the record with least rowid
value be "smaller", so index scans return records ordered by (keytuple,
rowid). This is used by index_merge access method, grep for ROR in
sql/opt_range.cc for details.
*/
return (ma->file_offset < mb->file_offset)? -1 : (ma->file_offset >
mb->file_offset) ? 1 : 0;
} /* queue_key_cmp */ } /* queue_key_cmp */
......
...@@ -402,3 +402,25 @@ explain select * from t1 force index(cola,colb) WHERE cola = 'foo' AND colb = 'b ...@@ -402,3 +402,25 @@ explain select * from t1 force index(cola,colb) WHERE cola = 'foo' AND colb = 'b
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge cola,colb cola,colb 3,3 NULL 24 Using intersect(cola,colb); Using where 1 SIMPLE t1 index_merge cola,colb cola,colb 3,3 NULL 24 Using intersect(cola,colb); Using where
drop table t1; drop table t1;
create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t1 (
a int, b int,
filler1 char(200), filler2 char(200),
key(a),key(b)
);
insert into t1 select @v:= A.a, @v, 't1', 'filler2' from t0 A, t0 B, t0 C;
create table t2 like t1;
create table t3 (
a int, b int,
filler1 char(200), filler2 char(200),
key(a),key(b)
) engine=merge union=(t1,t2);
explain select * from t1 where a=1 and b=1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge a,b a,b 5,5 NULL # Using intersect(a,b); Using where
explain select * from t3 where a=1 and b=1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t3 index_merge a,b a,b 5,5 NULL # Using intersect(a,b); Using where
drop table t3;
drop table t0, t1, t2;
...@@ -56,8 +56,8 @@ a b ...@@ -56,8 +56,8 @@ a b
4 Testing 4 Testing
5 table 5 table
5 table 5 table
6 t1
6 t2 6 t2
6 t1
7 Testing 7 Testing
7 Testing 7 Testing
8 table 8 table
......
...@@ -357,3 +357,29 @@ explain select * from t1 WHERE cola = 'foo' AND colb = 'bar'; ...@@ -357,3 +357,29 @@ explain select * from t1 WHERE cola = 'foo' AND colb = 'bar';
explain select * from t1 force index(cola,colb) WHERE cola = 'foo' AND colb = 'bar'; explain select * from t1 force index(cola,colb) WHERE cola = 'foo' AND colb = 'bar';
drop table t1; drop table t1;
#
# BUG#17314: Index_merge/intersection not choosen by the optimizer for MERGE tables
#
create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t1 (
a int, b int,
filler1 char(200), filler2 char(200),
key(a),key(b)
);
insert into t1 select @v:= A.a, @v, 't1', 'filler2' from t0 A, t0 B, t0 C;
create table t2 like t1;
create table t3 (
a int, b int,
filler1 char(200), filler2 char(200),
key(a),key(b)
) engine=merge union=(t1,t2);
--replace_column 9 #
explain select * from t1 where a=1 and b=1;
--replace_column 9 #
explain select * from t3 where a=1 and b=1;
drop table t3;
drop table t0, t1, t2;
...@@ -288,7 +288,27 @@ void ha_myisammrg::info(uint flag) ...@@ -288,7 +288,27 @@ void ha_myisammrg::info(uint flag)
table->s->db_options_in_use= info.options; table->s->db_options_in_use= info.options;
table->s->is_view= 1; table->s->is_view= 1;
mean_rec_length= info.reclength; mean_rec_length= info.reclength;
block_size=0;
/*
The handler::block_size is used all over the code in index scan cost
calculations. It is used to get number of disk seeks required to
retrieve a number of index tuples.
If the merge table has N underlying tables, then (assuming underlying
tables have equal size, the only "simple" approach we can use)
retrieving X index records from a merge table will require N times more
disk seeks compared to doing the same on a MyISAM table with equal
number of records.
In the edge case (file_tables > myisam_block_size) we'll get
block_size==0, and index calculation code will act as if we need one
disk seek to retrieve one index tuple.
TODO: In 5.2 index scan cost calculation will be factored out into a
virtual function in class handler and we'll be able to remove this hack.
*/
block_size= 0;
if (file->tables)
block_size= myisam_block_size / file->tables;
update_time=0; update_time=0;
#if SIZEOF_OFF_T > 4 #if SIZEOF_OFF_T > 4
ref_length=6; // Should be big enough ref_length=6; // Should be big enough
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment