249
251
static bool is_key_scan_ror(optimizer::Parameter *param, uint32_t keynr, uint8_t nparts);
251
static ha_rows check_quick_select(Session *session,
252
optimizer::Parameter *param,
253
static ha_rows check_quick_select(optimizer::Parameter *param,
255
256
optimizer::SEL_ARG *tree,
256
257
bool update_tbl_stats,
257
258
uint32_t *mrr_flags,
258
259
uint32_t *bufsize,
259
optimizer::CostVector *cost);
261
static optimizer::RangeReadPlan *get_key_scans_params(Session *session,
262
optimizer::Parameter *param,
262
static optimizer::RangeReadPlan *get_key_scans_params(optimizer::Parameter *param,
263
263
optimizer::SEL_TREE *tree,
264
264
bool index_read_must_be_used,
265
265
bool update_tbl_stats,
464
459
MAX_KEY if no such index was found.
467
uint32_t optimizer::get_index_for_order(Table *table, Order *order, ha_rows limit)
462
uint32_t optimizer::get_index_for_order(Table *table, order_st *order, ha_rows limit)
470
465
uint32_t match_key= MAX_KEY, match_key_len= MAX_KEY_LENGTH + 1;
473
468
for (ord= order; ord; ord= ord->next)
477
for (idx= 0; idx < table->getShare()->sizeKeys(); idx++)
472
for (idx= 0; idx < table->s->keys; idx++)
479
474
if (!(table->keys_in_use_for_query.test(idx)))
481
KeyPartInfo *keyinfo= table->key_info[idx].key_part;
476
KEY_PART_INFO *keyinfo= table->key_info[idx].key_part;
482
477
uint32_t n_parts= table->key_info[idx].key_parts;
483
478
uint32_t partno= 0;
543
538
static int fill_used_fields_bitmap(optimizer::Parameter *param)
545
540
Table *table= param->table;
547
param->tmp_covered_fields.clear();
548
param->needed_fields.resize(table->getShare()->sizeFields());
549
param->needed_fields.reset();
551
param->needed_fields|= *table->read_set;
552
param->needed_fields|= *table->write_set;
554
pk= param->table->getShare()->getPrimaryKey();
543
param->tmp_covered_fields.setBitmap(0);
544
param->fields_bitmap_size= table->s->column_bitmap_size;
545
if (!(tmp= (my_bitmap_map*) alloc_root(param->mem_root,
546
param->fields_bitmap_size)) ||
547
param->needed_fields.init(tmp, table->s->fields))
550
param->needed_fields= *table->read_set;
551
bitmap_union(¶m->needed_fields, table->write_set);
553
pk= param->table->s->primary_key;
555
554
if (pk != MAX_KEY && param->table->cursor->primary_key_is_clustered())
557
556
/* The table uses clustered PK and it is not internally generated */
558
KeyPartInfo *key_part= param->table->key_info[pk].key_part;
559
KeyPartInfo *key_part_end= key_part +
557
KEY_PART_INFO *key_part= param->table->key_info[pk].key_part;
558
KEY_PART_INFO *key_part_end= key_part +
560
559
param->table->key_info[pk].key_parts;
561
560
for (;key_part != key_part_end; ++key_part)
562
param->needed_fields.reset(key_part->fieldnr-1);
561
param->needed_fields.clearBit(key_part->fieldnr-1);
1177
typedef struct st_ror_scan_info
1179
uint32_t idx; /* # of used key in param->keys */
1180
uint32_t keynr; /* # of used key in table */
1181
ha_rows records; /* estimate of # records this scan will return */
1183
/* Set of intervals over key fields that will be used for row retrieval. */
1184
optimizer::SEL_ARG *sel_arg;
1186
/* Fields used in the query and covered by this ROR scan. */
1187
MyBitmap covered_fields;
1188
uint32_t used_fields_covered; /* # of set bits in covered_fields */
1189
int key_rec_length; /* length of key record (including rowid) */
1192
Cost of reading all index records with values in sel_arg intervals set
1193
(assuming there is no need to access full table records)
1195
double index_read_cost;
1196
uint32_t first_uncovered_field; /* first unused bit in covered_fields */
1197
uint32_t key_components; /* # of parts in the key */
1190
Create optimizer::RorScanInfo* structure with a single ROR scan on index idx using
1202
Create ROR_SCAN_INFO* structure with a single ROR scan on index idx using
1191
1203
sel_arg set of intervals.
1218
1232
ror_scan->sel_arg= sel_arg;
1219
1233
ror_scan->records= param->table->quick_rows[keynr];
1221
ror_scan->covered_fields_size= param->table->getShare()->sizeFields();
1222
boost::dynamic_bitset<> tmp_bitset(param->table->getShare()->sizeFields());
1225
KeyPartInfo *key_part= param->table->key_info[keynr].key_part;
1226
KeyPartInfo *key_part_end= key_part +
1235
if (!(bitmap_buf= (my_bitmap_map*) alloc_root(param->mem_root,
1236
param->fields_bitmap_size)))
1239
if (ror_scan->covered_fields.init(bitmap_buf,
1240
param->table->s->fields))
1242
ror_scan->covered_fields.clearAll();
1244
KEY_PART_INFO *key_part= param->table->key_info[keynr].key_part;
1245
KEY_PART_INFO *key_part_end= key_part +
1227
1246
param->table->key_info[keynr].key_parts;
1228
for (; key_part != key_part_end; ++key_part)
1247
for (;key_part != key_part_end; ++key_part)
1230
if (param->needed_fields.test(key_part->fieldnr-1))
1231
tmp_bitset.set(key_part->fieldnr-1);
1249
if (param->needed_fields.isBitSet(key_part->fieldnr-1))
1250
ror_scan->covered_fields.setBit(key_part->fieldnr-1);
1233
1252
double rows= rows2double(param->table->quick_rows[ror_scan->keynr]);
1234
1253
ror_scan->index_read_cost=
1235
1254
param->table->cursor->index_only_read_time(ror_scan->keynr, rows);
1236
ror_scan->covered_fields= tmp_bitset.to_ulong();
1242
Compare two optimizer::RorScanInfo** by E(#records_matched) * key_record_length.
1260
Compare two ROR_SCAN_INFO** by E(#records_matched) * key_record_length.
1244
1262
cmp_ror_scan_info()
1245
1263
a ptr to first compared value
1337
1331
} ROR_INTERSECT_INFO;
1335
Allocate a ROR_INTERSECT_INFO and initialize it to contain zero scans.
1338
ror_intersect_init()
1339
param Parameter from test_quick_select
1347
ROR_INTERSECT_INFO* ror_intersect_init(const optimizer::Parameter *param)
1349
ROR_INTERSECT_INFO *info;
1351
if (!(info= (ROR_INTERSECT_INFO*)alloc_root(param->mem_root,
1352
sizeof(ROR_INTERSECT_INFO))))
1355
if (!(buf= (my_bitmap_map*) alloc_root(param->mem_root,
1356
param->fields_bitmap_size)))
1358
if (info->covered_fields.init(buf, param->table->s->fields))
1360
info->is_covering= false;
1361
info->index_scan_costs= 0.0;
1362
info->index_records= 0;
1363
info->out_rows= (double) param->table->cursor->stats.records;
1364
info->covered_fields.clearAll();
1340
1368
static void ror_intersect_cpy(ROR_INTERSECT_INFO *dst,
1341
1369
const ROR_INTERSECT_INFO *src)
1443
1471
static double ror_scan_selectivity(const ROR_INTERSECT_INFO *info,
1444
const optimizer::RorScanInfo *scan)
1472
const ROR_SCAN_INFO *scan)
1446
1474
double selectivity_mult= 1.0;
1447
KeyPartInfo *key_part= info->param->table->key_info[scan->keynr].key_part;
1475
KEY_PART_INFO *key_part= info->param->table->key_info[scan->keynr].key_part;
1448
1476
unsigned char key_val[MAX_KEY_LENGTH+MAX_FIELD_WIDTH]; /* key values tuple */
1449
1477
unsigned char *key_ptr= key_val;
1450
1478
optimizer::SEL_ARG *sel_arg= NULL;
1451
1479
optimizer::SEL_ARG *tuple_arg= NULL;
1452
1480
key_part_map keypart_map= 0;
1453
1481
bool cur_covered;
1454
bool prev_covered= test(info->covered_fields.test(key_part->fieldnr-1));
1482
bool prev_covered= test(info->covered_fields.isBitSet(key_part->fieldnr-1));
1455
1483
key_range min_range;
1456
1484
key_range max_range;
1457
1485
min_range.key= key_val;
1577
1605
info->index_records += info->param->table->quick_rows[ror_scan->keynr];
1578
1606
info->index_scan_costs += ror_scan->index_read_cost;
1579
boost::dynamic_bitset<> tmp_bitset= ror_scan->bitsToBitset();
1580
info->covered_fields|= tmp_bitset;
1581
if (! info->is_covering && info->param->needed_fields.is_subset_of(info->covered_fields))
1607
bitmap_union(&info->covered_fields, &ror_scan->covered_fields);
1608
if (!info->is_covering && bitmap_is_subset(&info->param->needed_fields,
1609
&info->covered_fields))
1583
1611
info->is_covering= true;
1587
1615
info->total_cost= info->index_scan_costs;
1588
if (! info->is_covering)
1616
if (!info->is_covering)
1590
optimizer::CostVector sweep_cost;
1591
Join *join= info->param->session->lex->select_lex.join;
1618
COST_VECT sweep_cost;
1619
JOIN *join= info->param->session->lex->select_lex.join;
1592
1620
bool is_interrupted= test(join && join->tables == 1);
1593
1621
get_sweep_read_cost(info->param->table, double2rows(info->out_rows),
1594
1622
is_interrupted, &sweep_cost);
1602
Get best covering ROR-intersection.
1604
get_best_covering_ror_intersect()
1605
param Parameter from test_quick_select function.
1606
tree optimizer::SEL_TREE with sets of intervals for different keys.
1607
read_time Don't return table read plans with cost > read_time.
1610
Best covering ROR-intersection plan
1611
NULL if no plan found.
1614
get_best_ror_intersect must be called for a tree before calling this
1616
This function invalidates tree->ror_scans member values.
1618
The following approximate algorithm is used:
1619
I=set of all covering indexes
1620
F=set of all fields to cover
1625
Order I by (#covered fields in F desc,
1627
number of first not covered component asc);
1628
F=F-covered by first(I);
1631
} while F is not empty.
1635
optimizer::RorIntersectReadPlan *get_best_covering_ror_intersect(optimizer::Parameter *param,
1636
optimizer::SEL_TREE *tree,
1639
optimizer::RorScanInfo **ror_scan_mark;
1640
optimizer::RorScanInfo **ror_scans_end= tree->ror_scans_end;
1642
for (optimizer::RorScanInfo **scan= tree->ror_scans; scan != ror_scans_end; ++scan)
1643
(*scan)->key_components=
1644
param->table->key_info[(*scan)->keynr].key_parts;
1647
Run covering-ROR-search algorithm.
1648
Assume set I is [ror_scan .. ror_scans_end)
1651
/*I=set of all covering indexes */
1652
ror_scan_mark= tree->ror_scans;
1654
boost::dynamic_bitset<> *covered_fields= ¶m->tmp_covered_fields;
1655
if (covered_fields->empty())
1657
covered_fields->resize(param->table->getShare()->sizeFields());
1659
covered_fields->reset();
1661
double total_cost= 0.0f;
1668
Update changed sorting info:
1670
number of first not covered component
1671
Calculate and save these values for each of remaining scans.
1673
for (optimizer::RorScanInfo **scan= ror_scan_mark; scan != ror_scans_end; ++scan)
1675
/* subtract one bitset from the other */
1676
(*scan)->subtractBitset(*covered_fields);
1677
(*scan)->used_fields_covered=
1678
(*scan)->getBitCount();
1679
(*scan)->first_uncovered_field= (*scan)->findFirstNotSet();
1682
internal::my_qsort(ror_scan_mark, ror_scans_end-ror_scan_mark,
1683
sizeof(optimizer::RorScanInfo*),
1684
(qsort_cmp)cmp_ror_scan_info_covering);
1687
total_cost += (*ror_scan_mark)->index_read_cost;
1688
records += (*ror_scan_mark)->records;
1689
if (total_cost > read_time)
1691
/* F=F-covered by first(I) */
1692
boost::dynamic_bitset<> tmp_bitset= (*ror_scan_mark)->bitsToBitset();
1693
*covered_fields|= tmp_bitset;
1694
all_covered= param->needed_fields.is_subset_of(*covered_fields);
1695
} while ((++ror_scan_mark < ror_scans_end) && ! all_covered);
1697
if (!all_covered || (ror_scan_mark - tree->ror_scans) == 1)
1701
Ok, [tree->ror_scans .. ror_scan) holds covering index_intersection with
1704
/* Add priority queue use cost. */
1705
total_cost += rows2double(records)*
1706
log((double)(ror_scan_mark - tree->ror_scans)) /
1707
(TIME_FOR_COMPARE_ROWID * M_LN2);
1709
if (total_cost > read_time)
1712
optimizer::RorIntersectReadPlan *trp= NULL;
1713
if (! (trp= new (param->mem_root) optimizer::RorIntersectReadPlan))
1718
uint32_t best_num= (ror_scan_mark - tree->ror_scans);
1719
if (!(trp->first_scan= (optimizer::RorScanInfo**)param->mem_root->alloc_root(sizeof(optimizer::RorScanInfo*)* best_num)))
1721
memcpy(trp->first_scan, tree->ror_scans, best_num*sizeof(optimizer::RorScanInfo*));
1722
trp->last_scan= trp->first_scan + best_num;
1723
trp->is_covering= true;
1724
trp->read_cost= total_cost;
1725
trp->records= records;
1726
trp->cpk_scan= NULL;
1727
set_if_smaller(param->table->quick_condition_rows, records);
1734
1630
Get best ROR-intersection plan using non-covering ROR-intersection search
1735
1631
algorithm. The returned plan may be covering.
1810
Step1: Collect ROR-able SEL_ARGs and create optimizer::RorScanInfo for each of
1706
Step1: Collect ROR-able SEL_ARGs and create ROR_SCAN_INFO for each of
1811
1707
them. Also find and save clustered PK scan if there is one.
1813
optimizer::RorScanInfo **cur_ror_scan= NULL;
1814
optimizer::RorScanInfo *cpk_scan= NULL;
1709
ROR_SCAN_INFO **cur_ror_scan= NULL;
1710
ROR_SCAN_INFO *cpk_scan= NULL;
1815
1711
uint32_t cpk_no= 0;
1816
1712
bool cpk_scan_used= false;
1818
if (! (tree->ror_scans= (optimizer::RorScanInfo**)param->mem_root->alloc_root(sizeof(optimizer::RorScanInfo*)* param->keys)))
1714
if (! (tree->ror_scans= (ROR_SCAN_INFO**)alloc_root(param->mem_root,
1715
sizeof(ROR_SCAN_INFO*)*
1822
1718
cpk_no= ((param->table->cursor->primary_key_is_clustered()) ?
1823
param->table->getShare()->getPrimaryKey() : MAX_KEY);
1719
param->table->s->primary_key : MAX_KEY);
1825
1721
for (idx= 0, cur_ror_scan= tree->ror_scans; idx < param->keys; idx++)
1827
optimizer::RorScanInfo *scan;
1723
ROR_SCAN_INFO *scan;
1828
1724
if (! tree->ror_scans_map.test(idx))
1830
if (! (scan= make_ror_scan(param, idx, tree->keys[idx])))
1726
if (!(scan= make_ror_scan(param, idx, tree->keys[idx])))
1832
1728
if (param->real_keynr[idx] == cpk_no)
1841
1737
tree->ror_scans_end= cur_ror_scan;
1843
1739
Ok, [ror_scans, ror_scans_end) is array of ptrs to initialized
1844
optimizer::RorScanInfo's.
1845
1741
Step 2: Get best ROR-intersection using an approximate algorithm.
1847
internal::my_qsort(tree->ror_scans, tree->n_ror_scans, sizeof(optimizer::RorScanInfo*),
1743
internal::my_qsort(tree->ror_scans, tree->n_ror_scans, sizeof(ROR_SCAN_INFO*),
1848
1744
(qsort_cmp)cmp_ror_scan_info);
1850
optimizer::RorScanInfo **intersect_scans= NULL; /* ROR scans used in index intersection */
1851
optimizer::RorScanInfo **intersect_scans_end= NULL;
1852
if (! (intersect_scans= (optimizer::RorScanInfo**)param->mem_root->alloc_root(sizeof(optimizer::RorScanInfo*) * tree->n_ror_scans)))
1746
ROR_SCAN_INFO **intersect_scans= NULL; /* ROR scans used in index intersection */
1747
ROR_SCAN_INFO **intersect_scans_end= NULL;
1748
if (! (intersect_scans= (ROR_SCAN_INFO**)alloc_root(param->mem_root,
1749
sizeof(ROR_SCAN_INFO*)*
1750
tree->n_ror_scans)))
1854
1752
intersect_scans_end= intersect_scans;
1856
1754
/* Create and incrementally update ROR intersection. */
1857
ROR_INTERSECT_INFO intersect(param);
1858
ROR_INTERSECT_INFO intersect_best(param);
1755
ROR_INTERSECT_INFO *intersect= NULL;
1756
ROR_INTERSECT_INFO *intersect_best= NULL;
1757
if (! (intersect= ror_intersect_init(param)) ||
1758
! (intersect_best= ror_intersect_init(param)))
1860
1761
/* [intersect_scans,intersect_scans_best) will hold the best intersection */
1861
optimizer::RorScanInfo **intersect_scans_best= NULL;
1762
ROR_SCAN_INFO **intersect_scans_best= NULL;
1862
1763
cur_ror_scan= tree->ror_scans;
1863
1764
intersect_scans_best= intersect_scans;
1864
while (cur_ror_scan != tree->ror_scans_end && ! intersect.is_covering)
1765
while (cur_ror_scan != tree->ror_scans_end && !intersect->is_covering)
1866
1767
/* S= S + first(R); R= R - first(R); */
1867
if (! ror_intersect_add(&intersect, *cur_ror_scan, false))
1768
if (!ror_intersect_add(intersect, *cur_ror_scan, false))
1869
1770
cur_ror_scan++;
1889
*are_all_covering= intersect.is_covering;
1790
*are_all_covering= intersect->is_covering;
1890
1791
uint32_t best_num= intersect_scans_best - intersect_scans;
1891
ror_intersect_cpy(&intersect, &intersect_best);
1792
ror_intersect_cpy(intersect, intersect_best);
1894
1795
Ok, found the best ROR-intersection of non-CPK key scans.
1895
1796
Check if we should add a CPK scan. If the obtained ROR-intersection is
1896
1797
covering, it doesn't make sense to add CPK scan.
1898
if (cpk_scan && ! intersect.is_covering)
1799
if (cpk_scan && !intersect->is_covering)
1900
if (ror_intersect_add(&intersect, cpk_scan, true) &&
1901
(intersect.total_cost < min_cost))
1801
if (ror_intersect_add(intersect, cpk_scan, true) &&
1802
(intersect->total_cost < min_cost))
1903
1804
cpk_scan_used= true;
1904
1805
intersect_best= intersect; //just set pointer here
1912
1813
if (! (trp= new (param->mem_root) optimizer::RorIntersectReadPlan))
1915
if (! (trp->first_scan=
1916
(optimizer::RorScanInfo**)param->mem_root->alloc_root(sizeof(optimizer::RorScanInfo*)*best_num)))
1918
memcpy(trp->first_scan, intersect_scans, best_num*sizeof(optimizer::RorScanInfo*));
1919
trp->last_scan= trp->first_scan + best_num;
1920
trp->is_covering= intersect_best.is_covering;
1921
trp->read_cost= intersect_best.total_cost;
1816
trp->ror_range_scans.assign(intersect_scans, intersect_scans + best_num);
1817
trp->setRowRetrievalNecessary(intersect_best->is_covering);
1818
trp->read_cost= intersect_best->total_cost;
1922
1819
/* Prevent divisons by zero */
1923
ha_rows best_rows = double2rows(intersect_best.out_rows);
1820
ha_rows best_rows = double2rows(intersect_best->out_rows);
1924
1821
if (! best_rows)
1926
1823
set_if_smaller(param->table->quick_condition_rows, best_rows);
1927
1824
trp->records= best_rows;
1928
trp->index_scan_costs= intersect_best.index_scan_costs;
1825
trp->setCostOfIndexScans(intersect_best->index_scan_costs);
1929
1826
trp->cpk_scan= cpk_scan_used? cpk_scan: NULL;
1833
Get best covering ROR-intersection.
1835
get_best_covering_ror_intersect()
1836
param Parameter from test_quick_select function.
1837
tree optimizer::SEL_TREE with sets of intervals for different keys.
1838
read_time Don't return table read plans with cost > read_time.
1841
Best covering ROR-intersection plan
1842
NULL if no plan found.
1845
get_best_ror_intersect must be called for a tree before calling this
1847
This function invalidates tree->ror_scans member values.
1849
The following approximate algorithm is used:
1850
I=set of all covering indexes
1851
F=set of all fields to cover
1856
Order I by (#covered fields in F desc,
1858
number of first not covered component asc);
1859
F=F-covered by first(I);
1862
} while F is not empty.
1866
optimizer::RorIntersectReadPlan *get_best_covering_ror_intersect(optimizer::Parameter *param,
1867
optimizer::SEL_TREE *tree,
1870
ROR_SCAN_INFO **ror_scan_mark;
1871
ROR_SCAN_INFO **ror_scans_end= tree->ror_scans_end;
1873
for (ROR_SCAN_INFO **scan= tree->ror_scans; scan != ror_scans_end; ++scan)
1874
(*scan)->key_components=
1875
param->table->key_info[(*scan)->keynr].key_parts;
1878
Run covering-ROR-search algorithm.
1879
Assume set I is [ror_scan .. ror_scans_end)
1882
/*I=set of all covering indexes */
1883
ror_scan_mark= tree->ror_scans;
1885
MyBitmap *covered_fields= ¶m->tmp_covered_fields;
1886
if (! covered_fields->getBitmap())
1888
my_bitmap_map *tmp_bitmap= (my_bitmap_map*)alloc_root(param->mem_root,
1889
param->fields_bitmap_size);
1890
covered_fields->setBitmap(tmp_bitmap);
1892
if (! covered_fields->getBitmap() ||
1893
covered_fields->init(covered_fields->getBitmap(),
1894
param->table->s->fields))
1896
covered_fields->clearAll();
1898
double total_cost= 0.0f;
1905
Update changed sorting info:
1907
number of first not covered component
1908
Calculate and save these values for each of remaining scans.
1910
for (ROR_SCAN_INFO **scan= ror_scan_mark; scan != ror_scans_end; ++scan)
1912
bitmap_subtract(&(*scan)->covered_fields, covered_fields);
1913
(*scan)->used_fields_covered=
1914
(*scan)->covered_fields.getBitsSet();
1915
(*scan)->first_uncovered_field=
1916
(*scan)->covered_fields.getFirst();
1919
internal::my_qsort(ror_scan_mark, ror_scans_end-ror_scan_mark,
1920
sizeof(ROR_SCAN_INFO*),
1921
(qsort_cmp)cmp_ror_scan_info_covering);
1924
total_cost += (*ror_scan_mark)->index_read_cost;
1925
records += (*ror_scan_mark)->records;
1926
if (total_cost > read_time)
1928
/* F=F-covered by first(I) */
1929
bitmap_union(covered_fields, &(*ror_scan_mark)->covered_fields);
1930
all_covered= bitmap_is_subset(¶m->needed_fields, covered_fields);
1931
} while ((++ror_scan_mark < ror_scans_end) && !all_covered);
1933
if (!all_covered || (ror_scan_mark - tree->ror_scans) == 1)
1937
Ok, [tree->ror_scans .. ror_scan) holds covering index_intersection with
1940
/* Add priority queue use cost. */
1941
total_cost += rows2double(records)*
1942
log((double)(ror_scan_mark - tree->ror_scans)) /
1943
(TIME_FOR_COMPARE_ROWID * M_LN2);
1945
if (total_cost > read_time)
1948
optimizer::RorIntersectReadPlan *trp= NULL;
1949
if (! (trp= new (param->mem_root) optimizer::RorIntersectReadPlan))
1954
uint32_t best_num= (ror_scan_mark - tree->ror_scans);
1955
trp->ror_range_scans.assign(tree->ror_scans, tree->ror_scans + best_num);
1956
trp->setRowRetrievalNecessary(true);
1957
trp->read_cost= total_cost;
1958
trp->records= records;
1959
trp->cpk_scan= NULL;
1960
set_if_smaller(param->table->quick_condition_rows, records);
1936
1967
Get best "range" table read plan for given optimizer::SEL_TREE, also update some info
1939
1970
get_key_scans_params()
1941
1971
param Parameters from test_quick_select
1942
1972
tree Make range select for this optimizer::SEL_TREE
1943
1973
index_read_must_be_used true <=> assume 'index only' option will be set
3935
3949
static bool is_key_scan_ror(optimizer::Parameter *param, uint32_t keynr, uint8_t nparts)
3937
KeyInfo *table_key= param->table->key_info + keynr;
3938
KeyPartInfo *key_part= table_key->key_part + nparts;
3939
KeyPartInfo *key_part_end= (table_key->key_part +
3951
KEY *table_key= param->table->key_info + keynr;
3952
KEY_PART_INFO *key_part= table_key->key_part + nparts;
3953
KEY_PART_INFO *key_part_end= (table_key->key_part +
3940
3954
table_key->key_parts);
3941
3955
uint32_t pk_number;
3943
for (KeyPartInfo *kp= table_key->key_part; kp < key_part; kp++)
3957
for (KEY_PART_INFO *kp= table_key->key_part; kp < key_part; kp++)
3945
3959
uint16_t fieldnr= param->table->key_info[keynr].
3946
3960
key_part[kp - table_key->key_part].fieldnr - 1;
3947
if (param->table->getField(fieldnr)->key_length() != kp->length)
3961
if (param->table->field[fieldnr]->key_length() != kp->length)
4237
4251
table_reference_st *ref,
4238
4252
ha_rows records)
4240
memory::Root *old_root= NULL;
4241
memory::Root *alloc= NULL;
4242
KeyInfo *key_info = &table->key_info[ref->key];
4254
memory::Root *old_root, *alloc;
4255
optimizer::QuickRangeSelect *quick= NULL;
4256
KEY *key_info = &table->key_info[ref->key];
4243
4257
KEY_PART *key_part;
4244
4258
optimizer::QuickRange *range= NULL;
4246
optimizer::CostVector cost;
4260
bool create_err= false;
4248
4263
old_root= session->mem_root;
4249
4264
/* The following call may change session->mem_root */
4250
optimizer::QuickRangeSelect *quick= new optimizer::QuickRangeSelect(session, table, ref->key, 0, 0);
4265
quick= new optimizer::QuickRangeSelect(session, table, ref->key, 0, 0, &create_err);
4251
4266
/* save mem_root set by QuickRangeSelect constructor */
4252
4267
alloc= session->mem_root;
4402
static inline uint32_t get_field_keypart(KeyInfo *index, Field *field);
4417
static inline uint32_t get_field_keypart(KEY *index, Field *field);
4404
4419
static inline optimizer::SEL_ARG * get_index_range_tree(uint32_t index,
4405
4420
optimizer::SEL_TREE *range_tree,
4406
4421
optimizer::Parameter *param,
4407
4422
uint32_t *param_idx);
4409
static bool get_constant_key_infix(KeyInfo *index_info,
4424
static bool get_constant_key_infix(KEY *index_info,
4410
4425
optimizer::SEL_ARG *index_range_tree,
4411
KeyPartInfo *first_non_group_part,
4412
KeyPartInfo *min_max_arg_part,
4413
KeyPartInfo *last_part,
4426
KEY_PART_INFO *first_non_group_part,
4427
KEY_PART_INFO *min_max_arg_part,
4428
KEY_PART_INFO *last_part,
4414
4429
Session *session,
4415
4430
unsigned char *key_infix,
4416
4431
uint32_t *key_infix_len,
4417
KeyPartInfo **first_non_infix_part);
4432
KEY_PART_INFO **first_non_infix_part);
4419
4434
static bool check_group_min_max_predicates(COND *cond, Item_field *min_max_arg_item);
4422
4437
cost_group_min_max(Table* table,
4423
KeyInfo *index_info,
4424
4439
uint32_t used_key_parts,
4425
4440
uint32_t group_key_parts,
4426
4441
optimizer::SEL_TREE *range_tree,
4563
4578
get_best_group_min_max(optimizer::Parameter *param, optimizer::SEL_TREE *tree)
4565
4580
Session *session= param->session;
4566
Join *join= session->lex->current_select->join;
4581
JOIN *join= session->lex->current_select->join;
4567
4582
Table *table= param->table;
4568
4583
bool have_min= false; /* true if there is a MIN function. */
4569
4584
bool have_max= false; /* true if there is a MAX function. */
4570
4585
Item_field *min_max_arg_item= NULL; // The argument of all MIN/MAX functions
4571
KeyPartInfo *min_max_arg_part= NULL; /* The corresponding keypart. */
4586
KEY_PART_INFO *min_max_arg_part= NULL; /* The corresponding keypart. */
4572
4587
uint32_t group_prefix_len= 0; /* Length (in bytes) of the key prefix. */
4573
KeyInfo *index_info= NULL; /* The index chosen for data access. */
4588
KEY *index_info= NULL; /* The index chosen for data access. */
4574
4589
uint32_t index= 0; /* The id of the chosen index. */
4575
4590
uint32_t group_key_parts= 0; // Number of index key parts in the group prefix.
4576
4591
uint32_t used_key_parts= 0; /* Number of index key parts used for access. */
4650
4665
(GA1,GA2) are all true. If there is more than one such index, select the
4651
4666
first one. Here we set the variables: group_prefix_len and index_info.
4653
KeyInfo *cur_index_info= table->key_info;
4654
KeyInfo *cur_index_info_end= cur_index_info + table->getShare()->sizeKeys();
4655
KeyPartInfo *cur_part= NULL;
4656
KeyPartInfo *end_part= NULL; /* Last part for loops. */
4668
KEY *cur_index_info= table->key_info;
4669
KEY *cur_index_info_end= cur_index_info + table->s->keys;
4670
KEY_PART_INFO *cur_part= NULL;
4671
KEY_PART_INFO *end_part= NULL; /* Last part for loops. */
4657
4672
/* Last index part. */
4658
KeyPartInfo *last_part= NULL;
4659
KeyPartInfo *first_non_group_part= NULL;
4660
KeyPartInfo *first_non_infix_part= NULL;
4673
KEY_PART_INFO *last_part= NULL;
4674
KEY_PART_INFO *first_non_group_part= NULL;
4675
KEY_PART_INFO *first_non_infix_part= NULL;
4661
4676
uint32_t key_infix_parts= 0;
4662
4677
uint32_t cur_group_key_parts= 0;
4663
4678
uint32_t cur_group_prefix_len= 0;
5165
get_constant_key_infix(KeyInfo *,
5179
get_constant_key_infix(KEY *,
5166
5180
optimizer::SEL_ARG *index_range_tree,
5167
KeyPartInfo *first_non_group_part,
5168
KeyPartInfo *min_max_arg_part,
5169
KeyPartInfo *last_part,
5181
KEY_PART_INFO *first_non_group_part,
5182
KEY_PART_INFO *min_max_arg_part,
5183
KEY_PART_INFO *last_part,
5171
5185
unsigned char *key_infix,
5172
5186
uint32_t *key_infix_len,
5173
KeyPartInfo **first_non_infix_part)
5187
KEY_PART_INFO **first_non_infix_part)
5175
5189
optimizer::SEL_ARG *cur_range= NULL;
5176
KeyPartInfo *cur_part= NULL;
5190
KEY_PART_INFO *cur_part= NULL;
5177
5191
/* End part for the first loop below. */
5178
KeyPartInfo *end_part= min_max_arg_part ? min_max_arg_part : last_part;
5192
KEY_PART_INFO *end_part= min_max_arg_part ? min_max_arg_part : last_part;
5180
5194
*key_infix_len= 0;
5181
5195
unsigned char *key_ptr= key_infix;
5570
uint32_t optimizer::RorScanInfo::findFirstNotSet() const
5572
boost::dynamic_bitset<> map= bitsToBitset();
5573
for (boost::dynamic_bitset<>::size_type i= 0; i < map.size(); i++)
5584
size_t optimizer::RorScanInfo::getBitCount() const
5586
boost::dynamic_bitset<> tmp_bitset= bitsToBitset();
5587
return tmp_bitset.count();
5591
void optimizer::RorScanInfo::subtractBitset(const boost::dynamic_bitset<>& in_bitset)
5593
boost::dynamic_bitset<> tmp_bitset= bitsToBitset();
5594
tmp_bitset-= in_bitset;
5595
covered_fields= tmp_bitset.to_ulong();
5599
boost::dynamic_bitset<> optimizer::RorScanInfo::bitsToBitset() const
5602
uint64_t conv= covered_fields;
5605
res.push_back((conv & 1) + '0');
5610
std::reverse(res.begin(), res.end());
5612
string final(covered_fields_size - res.length(), '0');
5614
return (boost::dynamic_bitset<>(final));
5618
5584
} /* namespace drizzled */