2011-10-19 21:45:18 +02:00
|
|
|
/* Copyright (C) 2010, 2011 Monty Program Ab
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; version 2 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
Update FSF address
This commit is based on the work of Michal Schorm, rebased on the
earliest MariaDB version.
Th command line used to generate this diff was:
find ./ -type f \
-exec sed -i -e 's/Foundation, Inc., 59 Temple Place, Suite 330, Boston, /Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, /g' {} \; \
-exec sed -i -e 's/Foundation, Inc. 59 Temple Place.* Suite 330, Boston, /Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, /g' {} \; \
-exec sed -i -e 's/MA.*.....-1307.*USA/MA 02110-1335 USA/g' {} \; \
-exec sed -i -e 's/Foundation, Inc., 59 Temple/Foundation, Inc., 51 Franklin/g' {} \; \
-exec sed -i -e 's/Place, Suite 330, Boston, MA.*02111-1307.*USA/Street, Fifth Floor, Boston, MA 02110-1335 USA/g' {} \; \
-exec sed -i -e 's/MA.*.....-1307/MA 02110-1335/g' {} \;
2019-05-10 20:49:46 +03:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
|
2011-10-19 21:45:18 +02:00
|
|
|
|
2017-06-18 06:42:16 +03:00
|
|
|
#include "mariadb.h"
|
2011-04-25 17:22:25 +02:00
|
|
|
#include "sql_parse.h"
|
2010-06-22 23:26:11 +04:00
|
|
|
#include <my_bit.h>
|
2009-12-15 10:16:46 +03:00
|
|
|
#include "sql_select.h"
|
2011-10-19 21:45:18 +02:00
|
|
|
#include "key.h"
|
2018-08-17 14:27:42 -07:00
|
|
|
#include "sql_statistics.h"
|
2020-01-18 13:26:03 -08:00
|
|
|
#include "rowid_filter.h"
|
Changing all cost calculation to be given in milliseconds
This makes it easier to compare different costs and also allows
the optimizer to optimizer different storage engines more reliably.
- Added tests/check_costs.pl, a tool to verify optimizer cost calculations.
- Most engine costs has been found with this program. All steps to
calculate the new costs are documented in Docs/optimizer_costs.txt
- User optimizer_cost variables are given in microseconds (as individual
costs can be very small). Internally they are stored in ms.
- Changed DISK_READ_COST (was DISK_SEEK_BASE_COST) from a hard disk cost
(9 ms) to common SSD cost (400MB/sec).
- Removed cost calculations for hard disks (rotation etc).
- Changed the following handler functions to return IO_AND_CPU_COST.
This makes it easy to apply different cost modifiers in ha_..time()
functions for io and cpu costs.
- scan_time()
- rnd_pos_time() & rnd_pos_call_time()
- keyread_time()
- Enhanched keyread_time() to calculate the full cost of reading of a set
of keys with a given number of ranges and optional number of blocks that
need to be accessed.
- Removed read_time() as keyread_time() + rnd_pos_time() can do the same
thing and more.
- Tuned cost for: heap, myisam, Aria, InnoDB, archive and MyRocks.
Used heap table costs for json_table. The rest are using default engine
costs.
- Added the following new optimizer variables:
- optimizer_disk_read_ratio
- optimizer_disk_read_cost
- optimizer_key_lookup_cost
- optimizer_row_lookup_cost
- optimizer_row_next_find_cost
- optimizer_scan_cost
- Moved all engine specific cost to OPTIMIZER_COSTS structure.
- Changed costs to use 'records_out' instead of 'records_read' when
recalculating costs.
- Split optimizer_costs.h to optimizer_costs.h and optimizer_defaults.h.
This allows one to change costs without having to compile a lot of
files.
- Updated costs for filter lookup.
- Use a better cost estimate in best_extension_by_limited_search()
for the sorting cost.
- Fixed previous issues with 'filtered' explain column as we are now
using 'records_out' (min rows seen for table) to calculate filtering.
This greatly simplifies the filtering code in
JOIN_TAB::save_explain_data().
This change caused a lot of queries to be optimized differently than
before, which exposed different issues in the optimizer that needs to
be fixed. These fixes are in the following commits. To not have to
change the same test case over and over again, the changes in the test
cases are done in a single commit after all the critical change sets
are done.
InnoDB changes:
- Updated InnoDB to not divide big range cost with 2.
- Added cost for InnoDB (innobase_update_optimizer_costs()).
- Don't mark clustered primary key with HA_KEYREAD_ONLY. This will
prevent that the optimizer is trying to use index-only scans on
the clustered key.
- Disabled ha_innobase::scan_time() and ha_innobase::read_time() and
ha_innobase::rnd_pos_time() as the default engine cost functions now
works good for InnoDB.
Other things:
- Added --show-query-costs (\Q) option to mysql.cc to show the query
cost after each query (good when working with query costs).
- Extended my_getopt with GET_ADJUSTED_VALUE which allows one to adjust
the value that user is given. This is used to change cost from
microseconds (user input) to milliseconds (what the server is
internally using).
- Added include/my_tracker.h ; Useful include file to quickly test
costs of a function.
- Use handler::set_table() in all places instead of 'table= arg'.
- Added SHOW_OPTIMIZER_COSTS to sys variables. These are input and
shown in microseconds for the user but stored as milliseconds.
This is to make the numbers easier to read for the user (less
pre-zeros). Implemented in 'Sys_var_optimizer_cost' class.
- In test_quick_select() do not use index scans if 'no_keyread' is set
for the table. This is what we do in other places of the server.
- Added THD parameter to Unique::get_use_cost() and
check_index_intersect_extension() and similar functions to be able
to provide costs to called functions.
- Changed 'records' to 'rows' in optimizer_trace.
- Write more information to optimizer_trace.
- Added INDEX_BLOCK_FILL_FACTOR_MUL (4) and INDEX_BLOCK_FILL_FACTOR_DIV (3)
to calculate usage space of keys in b-trees. (Before we used numeric
constants).
- Removed code that assumed that b-trees has similar costs as binary
trees. Replaced with engine calls that returns the cost.
- Added Bitmap::find_first_bit()
- Added timings to join_cache for ANALYZE table (patch by Sergei Petrunia).
- Added records_init and records_after_filter to POSITION to remember
more of what best_access_patch() calculates.
- table_after_join_selectivity() changed to recalculate 'records_out'
based on the new fields from best_access_patch()
Bug fixes:
- Some queries did not update last_query_cost (was 0). Fixed by moving
setting thd->...last_query_cost in JOIN::optimize().
- Write '0' as number of rows for const tables with a matching row.
Some internals:
- Engine cost are stored in OPTIMIZER_COSTS structure. When a
handlerton is created, we also created a new cost variable for the
handlerton. We also create a new variable if the user changes a
optimizer cost for a not yet loaded handlerton either with command
line arguments or with SET
@@global.engine.optimizer_cost_variable=xx.
- There are 3 global OPTIMIZER_COSTS variables:
default_optimizer_costs The default costs + changes from the
command line without an engine specifier.
heap_optimizer_costs Heap table costs, used for temporary tables
tmp_table_optimizer_costs The cost for the default on disk internal
temporary table (MyISAM or Aria)
- The engine cost for a table is stored in table_share. To speed up
accesses the handler has a pointer to this. The cost is copied
to the table on first access. If one wants to change the cost one
must first update the global engine cost and then do a FLUSH TABLES.
This was done to be able to access the costs for an open table
without any locks.
- When a handlerton is created, the cost are updated the following way:
See sql/keycaches.cc for details:
- Use 'default_optimizer_costs' as a base
- Call hton->update_optimizer_costs() to override with the engines
default costs.
- Override the costs that the user has specified for the engine.
- One handler open, copy the engine cost from handlerton to TABLE_SHARE.
- Call handler::update_optimizer_costs() to allow the engine to update
cost for this particular table.
- There are two costs stored in THD. These are copied to the handler
when the table is used in a query:
- optimizer_where_cost
- optimizer_scan_setup_cost
- Simply code in best_access_path() by storing all cost result in a
structure. (Idea/Suggestion by Igor)
2022-08-11 13:05:23 +03:00
|
|
|
#include "optimizer_defaults.h"
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2022-09-30 17:10:37 +03:00
|
|
|
static void get_sweep_read_cost(TABLE *table, ha_rows nrows, bool interrupted,
|
|
|
|
Cost_estimate *cost);
|
|
|
|
|
2022-09-23 14:48:13 +03:00
|
|
|
|
|
|
|
|
2022-12-27 14:49:27 +02:00
|
|
|
/*
|
|
|
|
The following calculation is the same as in multi_range_read_info()
|
|
|
|
|
|
|
|
@param cost Total cost is stored here
|
|
|
|
@param keyno Key number
|
|
|
|
@param n_ranges Number of different ranges
|
|
|
|
@param multi_row_ranges Number of ranges that are not EQ_REF
|
|
|
|
@param flags Flags. Only HA_MRR_INDEX_ONLY is used.
|
|
|
|
@param total_rows Number of rows expected to be read.
|
|
|
|
@param io_blocks Number of blocks we expect to read for
|
|
|
|
a not clustered index.
|
|
|
|
@param unassigned_single_point_ranges
|
|
|
|
Number of blocks we have not yet read for
|
|
|
|
a clustered index.
|
|
|
|
*/
|
2022-09-23 14:48:13 +03:00
|
|
|
|
|
|
|
void handler::calculate_costs(Cost_estimate *cost, uint keyno,
|
2022-12-27 14:49:27 +02:00
|
|
|
uint n_ranges, uint multi_row_ranges,
|
|
|
|
uint flags,
|
2022-09-23 14:48:13 +03:00
|
|
|
ha_rows total_rows,
|
|
|
|
ulonglong io_blocks,
|
|
|
|
ulonglong unassigned_single_point_ranges)
|
|
|
|
{
|
2022-09-30 17:10:37 +03:00
|
|
|
cost->reset(this);
|
2022-09-23 14:48:13 +03:00
|
|
|
|
|
|
|
if (!is_clustering_key(keyno))
|
|
|
|
{
|
2022-12-27 14:49:27 +02:00
|
|
|
cost->index_cost= ha_keyread_time(keyno, n_ranges,
|
|
|
|
total_rows + multi_row_ranges,
|
|
|
|
io_blocks);
|
2022-09-23 14:48:13 +03:00
|
|
|
|
|
|
|
if (!(flags & HA_MRR_INDEX_ONLY))
|
|
|
|
{
|
|
|
|
/* ha_rnd_pos_time includes ROW_COPY_COST */
|
2022-09-30 17:10:37 +03:00
|
|
|
cost->row_cost= ha_rnd_pos_time(total_rows);
|
|
|
|
/* Adjust io cost to data size */
|
|
|
|
cost->row_cost.io= MY_MIN(cost->row_cost.io, row_blocks());
|
2022-09-23 14:48:13 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Index only read */
|
2022-09-30 17:10:37 +03:00
|
|
|
cost->copy_cost= rows2double(total_rows) * KEY_COPY_COST;
|
2022-09-23 14:48:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Clustered index */
|
2022-09-30 17:10:37 +03:00
|
|
|
io_blocks= unassigned_single_point_ranges;
|
2022-12-27 14:49:27 +02:00
|
|
|
cost->index_cost= ha_keyread_time(keyno, n_ranges,
|
|
|
|
total_rows + multi_row_ranges,
|
|
|
|
io_blocks);
|
2022-09-30 17:10:37 +03:00
|
|
|
cost->copy_cost= rows2double(total_rows) * ROW_COPY_COST;
|
2022-09-23 14:48:13 +03:00
|
|
|
}
|
2022-09-30 17:10:37 +03:00
|
|
|
/* Adjust io cost to data size */
|
|
|
|
cost->index_cost.io= MY_MIN(cost->index_cost.io, index_blocks(keyno));
|
|
|
|
|
2023-05-26 17:26:42 +03:00
|
|
|
cost->comp_cost= rows2double(total_rows) * WHERE_COST;
|
|
|
|
cost->setup_cost= MULTI_RANGE_READ_SETUP_COST;
|
2022-09-23 14:48:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
/****************************************************************************
|
|
|
|
* Default MRR implementation (MRR to non-MRR converter)
|
2009-12-15 10:16:46 +03:00
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
/**
|
2009-12-22 15:33:21 +03:00
|
|
|
Get cost and other information about MRR scan over a known list of ranges
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
Calculate estimated cost and other information about an MRR scan for given
|
|
|
|
sequence of ranges.
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
@param keyno Index number
|
|
|
|
@param seq Range sequence to be traversed
|
|
|
|
@param seq_init_param First parameter for seq->init()
|
|
|
|
@param n_ranges_arg Number of ranges in the sequence, or 0 if the caller
|
|
|
|
can't efficiently determine it
|
|
|
|
@param bufsz INOUT IN: Size of the buffer available for use
|
2021-10-06 12:31:19 +03:00
|
|
|
OUT: Size of the buffer that is expected to be
|
|
|
|
actually used, or 0 if buffer is not needed.
|
2009-12-22 15:33:21 +03:00
|
|
|
@param flags INOUT A combination of HA_MRR_* flags
|
|
|
|
@param cost OUT Estimated cost of MRR access
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
@note
|
|
|
|
This method (or an overriding one in a derived class) must check for
|
|
|
|
thd->killed and return HA_POS_ERROR if it is not zero. This is required
|
|
|
|
for a user to be able to interrupt the calculation by killing the
|
|
|
|
connection/query.
|
|
|
|
|
|
|
|
@retval
|
|
|
|
HA_POS_ERROR Error or the engine is unable to perform the requested
|
|
|
|
scan. Values of OUT parameters are undefined.
|
|
|
|
@retval
|
|
|
|
other OK, *cost contains cost of the scan, *bufsz and *flags
|
|
|
|
contain scan parameters.
|
2009-12-15 10:16:46 +03:00
|
|
|
*/
|
|
|
|
|
2022-09-23 14:48:13 +03:00
|
|
|
|
2020-02-28 12:59:30 +02:00
|
|
|
ha_rows
|
2009-12-22 15:33:21 +03:00
|
|
|
handler::multi_range_read_info_const(uint keyno, RANGE_SEQ_IF *seq,
|
|
|
|
void *seq_init_param, uint n_ranges_arg,
|
2020-02-28 12:59:30 +02:00
|
|
|
uint *bufsz, uint *flags,
|
2022-09-23 14:48:13 +03:00
|
|
|
ha_rows top_limit,
|
2020-02-28 12:59:30 +02:00
|
|
|
Cost_estimate *cost)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2009-12-22 15:33:21 +03:00
|
|
|
KEY_MULTI_RANGE range;
|
|
|
|
range_seq_t seq_it;
|
2019-02-03 14:56:12 -08:00
|
|
|
ha_rows total_rows= 0;
|
2009-12-22 15:33:21 +03:00
|
|
|
uint n_ranges=0;
|
2019-02-03 14:56:12 -08:00
|
|
|
ha_rows max_rows= stats.records;
|
2015-04-28 15:20:48 +04:00
|
|
|
THD *thd= table->in_use;
|
2020-02-28 12:59:30 +02:00
|
|
|
ulonglong io_blocks;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Counter of blocks that contain range edges for those ranges
|
|
|
|
for which records_in_range() is called
|
|
|
|
*/
|
|
|
|
ulonglong edge_blocks_cnt= 0;
|
|
|
|
/*
|
|
|
|
Counter of blocks that contain index tuples for those ranges
|
|
|
|
for which records_in_range() is called
|
|
|
|
*/
|
|
|
|
ulonglong range_blocks_cnt= 0;
|
|
|
|
/*
|
|
|
|
The position of the block containing the last record of the previous range
|
|
|
|
for which the info about range position is provided
|
|
|
|
*/
|
|
|
|
ulonglong prev_range_last_block= UNUSED_PAGE_NO;
|
|
|
|
/* The counter of records the staring from prev_range_last_block */
|
|
|
|
ulonglong prev_range_last_block_records= 0;
|
|
|
|
/*
|
|
|
|
The counter of single point ranges.
|
|
|
|
(For single point ranges we do not call records_in_range())
|
|
|
|
*/
|
|
|
|
ulonglong single_point_ranges= 0;
|
|
|
|
/*
|
|
|
|
The counter of of single point ranges that we succeded to assign
|
|
|
|
to some blocks
|
|
|
|
*/
|
|
|
|
ulonglong assigned_single_point_ranges= 0;
|
|
|
|
/*
|
|
|
|
Counter of single point ranges for which records_in_range in not
|
|
|
|
called and that are encountered between two ranges without such property
|
|
|
|
For example, let's have a subsequence of ranges
|
|
|
|
R1,r1,....rk,R2
|
|
|
|
where r1,...,rk are single point ranges for which records_in_range is
|
|
|
|
called while R1 and R2 are not such ranges.
|
|
|
|
Then single_point_ranges_delta will count ranges r1,...,rk.
|
|
|
|
*/
|
|
|
|
ulonglong unassigned_single_point_ranges= 0;
|
|
|
|
|
2020-03-13 15:28:42 +02:00
|
|
|
uint len= table->key_info[keyno].key_length + table->file->ref_length;
|
2020-02-26 14:52:23 +02:00
|
|
|
if (table->file->is_clustering_key(keyno))
|
2020-03-13 15:28:42 +02:00
|
|
|
len= table->s->stored_rec_length;
|
|
|
|
/* Assume block is 75 % full */
|
2020-02-28 12:59:30 +02:00
|
|
|
uint avg_block_records= ((uint) (stats.block_size*3/4))/len + 1;
|
|
|
|
uint limit= thd->variables.eq_range_index_dive_limit;
|
|
|
|
bool use_statistics_for_eq_range= eq_ranges_exceeds_limit(seq,
|
|
|
|
seq_init_param,
|
|
|
|
limit);
|
2020-03-13 15:28:42 +02:00
|
|
|
DBUG_ENTER("multi_range_read_info_const");
|
2018-08-24 20:53:00 -07:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
/* Default MRR implementation doesn't need buffer */
|
|
|
|
*bufsz= 0;
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
seq_it= seq->init(seq_init_param, n_ranges, *flags);
|
|
|
|
while (!seq->next(seq_it, &range))
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2019-02-03 14:56:12 -08:00
|
|
|
ha_rows rows;
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
if (unlikely(thd->killed != 0))
|
2020-03-13 15:28:42 +02:00
|
|
|
DBUG_RETURN(HA_POS_ERROR);
|
2020-02-28 12:59:30 +02:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
n_ranges++;
|
|
|
|
key_range *min_endp, *max_endp;
|
|
|
|
if (range.range_flag & GEOM_FLAG)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2009-12-22 15:33:21 +03:00
|
|
|
/* In this case tmp_min_flag contains the handler-read-function */
|
|
|
|
range.start_key.flag= (ha_rkey_function) (range.range_flag ^ GEOM_FLAG);
|
|
|
|
min_endp= &range.start_key;
|
|
|
|
max_endp= NULL;
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
2009-12-22 15:33:21 +03:00
|
|
|
else
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2009-12-22 15:33:21 +03:00
|
|
|
min_endp= range.start_key.length? &range.start_key : NULL;
|
|
|
|
max_endp= range.end_key.length? &range.end_key : NULL;
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
2018-08-17 14:27:42 -07:00
|
|
|
int keyparts_used= my_count_bits(range.start_key.keypart_map);
|
2020-02-28 12:59:30 +02:00
|
|
|
|
|
|
|
if ((range.range_flag & UNIQUE_RANGE) && !(range.range_flag & NULL_RANGE))
|
2019-02-03 14:56:12 -08:00
|
|
|
{
|
2020-02-28 12:59:30 +02:00
|
|
|
rows= 1;
|
|
|
|
/*
|
|
|
|
In this case we do not call records_in_range() and as a result
|
|
|
|
do not get any info on the edge blocks for this range. However if it
|
|
|
|
happens that the range for which we have such info uses the same block
|
|
|
|
for its first record as the last range for which such info is
|
|
|
|
provided uses for its last record then this range can be assigned
|
|
|
|
later to one of the blocks used by other ranges.
|
|
|
|
|
|
|
|
Note that we don't have to increment edge_blocks_cnt or
|
|
|
|
range_blocks_cnt here.
|
|
|
|
*/
|
|
|
|
single_point_ranges++;
|
|
|
|
}
|
|
|
|
else if (use_statistics_for_eq_range &&
|
|
|
|
!(range.range_flag & NULL_RANGE) &&
|
|
|
|
(range.range_flag & EQ_RANGE) &&
|
|
|
|
table->key_info[keyno].actual_rec_per_key(keyparts_used - 1) > 0.5)
|
|
|
|
{
|
|
|
|
rows= ((ha_rows) table->key_info[keyno].
|
|
|
|
actual_rec_per_key(keyparts_used-1));
|
|
|
|
range_blocks_cnt+= ((MY_MAX(rows, 1) - 1) / avg_block_records + 1);
|
2019-02-03 14:56:12 -08:00
|
|
|
}
|
2009-12-22 15:33:21 +03:00
|
|
|
else
|
|
|
|
{
|
2020-02-27 19:12:27 +02:00
|
|
|
page_range pages= unused_page_range;
|
2020-02-28 12:59:30 +02:00
|
|
|
if ((rows= this->records_in_range(keyno, min_endp, max_endp, &pages)) ==
|
|
|
|
HA_POS_ERROR)
|
2009-12-22 15:33:21 +03:00
|
|
|
{
|
|
|
|
/* Can't scan one range => can't do MRR scan at all */
|
|
|
|
total_rows= HA_POS_ERROR;
|
2022-06-06 15:22:24 +03:00
|
|
|
if (thd->is_error())
|
|
|
|
DBUG_RETURN(HA_POS_ERROR);
|
2009-12-22 15:33:21 +03:00
|
|
|
break;
|
|
|
|
}
|
2020-02-28 12:59:30 +02:00
|
|
|
if (pages.first_page == UNUSED_PAGE_NO)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
The engine does not provide info on the range position.
|
|
|
|
Place the range in a new block. Note that in this case
|
|
|
|
any new range will be placed in a new block.
|
|
|
|
*/
|
|
|
|
ulonglong additional_blocks= ((MY_MAX(rows,1) - 1) / avg_block_records +
|
|
|
|
1);
|
|
|
|
edge_blocks_cnt+= additional_blocks == 1 ? 1 : 2;
|
|
|
|
range_blocks_cnt+= additional_blocks;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The info on the range position is provided */
|
|
|
|
if (pages.first_page == prev_range_last_block)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
The new range starts in the same block that the last range
|
|
|
|
for which the position of the range was provided.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
First add records of single point ranges that can be placed
|
|
|
|
between these two ranges.
|
|
|
|
*/
|
|
|
|
prev_range_last_block_records+= (single_point_ranges -
|
|
|
|
assigned_single_point_ranges);
|
|
|
|
assigned_single_point_ranges= single_point_ranges;
|
|
|
|
if (pages.first_page == pages.last_page)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
All records of the current range are in the same block
|
|
|
|
Note that the prev_range_last_block_records can be much larger
|
|
|
|
than max_records_in_block as the rows can be compressed!
|
|
|
|
*/
|
|
|
|
prev_range_last_block_records+= rows;
|
|
|
|
DBUG_ASSERT(prev_range_last_block_records <
|
|
|
|
stats.block_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
The current range spans more than one block
|
|
|
|
|
|
|
|
Place part of the range records in 'prev_range_last_block'
|
|
|
|
and the remaining records in additional blocks.
|
|
|
|
|
|
|
|
We don't know where the first key was positioned in the
|
|
|
|
block, so we assume the range started in the middle of the
|
|
|
|
block.
|
|
|
|
|
|
|
|
Note that prev_range_last_block_records > avg_block_records
|
|
|
|
can be true in case of compressed rows.
|
|
|
|
*/
|
|
|
|
ha_rows rem_rows= rows;
|
|
|
|
|
|
|
|
if (avg_block_records > prev_range_last_block_records)
|
|
|
|
{
|
|
|
|
ha_rows space_left_in_prev_block=
|
|
|
|
(avg_block_records - prev_range_last_block_records)/2;
|
|
|
|
rem_rows= 0;
|
|
|
|
if (rows > space_left_in_prev_block)
|
|
|
|
rem_rows= rows - space_left_in_prev_block;
|
|
|
|
}
|
|
|
|
/* Calculate how many additional blocks we need for rem_rows */
|
|
|
|
ulonglong additional_blocks= ((MY_MAX(rem_rows, 1) - 1) /
|
|
|
|
avg_block_records + 1);
|
|
|
|
edge_blocks_cnt++;
|
|
|
|
range_blocks_cnt+= additional_blocks;
|
|
|
|
prev_range_last_block= pages.last_page;
|
|
|
|
/* There is at least one row on last page */
|
|
|
|
prev_range_last_block_records= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
The new range does not start in the same block that the last range
|
|
|
|
for which the position of the range was provided.
|
|
|
|
Note that rows may be 0!
|
|
|
|
*/
|
|
|
|
ulonglong additional_blocks= ((MY_MAX(rows, 1) - 1) /
|
|
|
|
avg_block_records + 1);
|
|
|
|
edge_blocks_cnt+= additional_blocks == 1 ? 1 : 2;
|
|
|
|
range_blocks_cnt+= additional_blocks;
|
|
|
|
unassigned_single_point_ranges+= (single_point_ranges -
|
|
|
|
assigned_single_point_ranges);
|
|
|
|
assigned_single_point_ranges= single_point_ranges;
|
|
|
|
prev_range_last_block= pages.last_page;
|
|
|
|
/* There is at least one row on last page */
|
|
|
|
prev_range_last_block_records= 1;
|
|
|
|
}
|
|
|
|
}
|
2009-12-22 15:33:21 +03:00
|
|
|
}
|
2020-02-28 12:59:30 +02:00
|
|
|
total_rows+= rows;
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
2020-02-28 12:59:30 +02:00
|
|
|
/*
|
|
|
|
Count the number of io_blocks that where not yet read and thus not cached.
|
|
|
|
The number of equal read blocks that where not read are:
|
|
|
|
|
|
|
|
(single_point_ranges - assigned_single_point_ranges).
|
|
|
|
|
|
|
|
We don't add these to io_blocks as we don't want to penalize equal
|
2021-10-06 12:31:19 +03:00
|
|
|
reads (if we did, a range that would read 5 rows would be
|
2020-02-28 12:59:30 +02:00
|
|
|
regarded as better than one equal read).
|
|
|
|
|
|
|
|
Better to assume we have done a records_in_range() for the equal
|
|
|
|
range and it's also cached.
|
2021-10-06 12:31:19 +03:00
|
|
|
|
|
|
|
One effect of this is that io_blocks for simple ranges are often 0,
|
|
|
|
as the blocks where already read by records_in_range and we assume
|
|
|
|
that we don't have to read it again.
|
2020-02-28 12:59:30 +02:00
|
|
|
*/
|
|
|
|
io_blocks= (range_blocks_cnt - edge_blocks_cnt);
|
|
|
|
unassigned_single_point_ranges+= (single_point_ranges -
|
|
|
|
assigned_single_point_ranges);
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
if (total_rows != HA_POS_ERROR)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2019-02-03 14:56:12 -08:00
|
|
|
set_if_smaller(total_rows, max_rows);
|
2009-12-22 15:33:21 +03:00
|
|
|
*flags |= HA_MRR_USE_DEFAULT_IMPL;
|
2022-12-27 14:49:27 +02:00
|
|
|
calculate_costs(cost, keyno, n_ranges,
|
|
|
|
n_ranges - (uint) single_point_ranges,
|
|
|
|
*flags, total_rows,
|
2022-09-23 14:48:13 +03:00
|
|
|
io_blocks, unassigned_single_point_ranges);
|
|
|
|
if (top_limit < total_rows)
|
2019-02-03 14:56:12 -08:00
|
|
|
{
|
2022-09-23 14:48:13 +03:00
|
|
|
/*
|
|
|
|
Calculate what the cost would be if we only have to read 'top_limit'
|
2022-09-30 17:10:37 +03:00
|
|
|
rows. This is the lowest possible cost when using the range
|
2022-09-23 14:48:13 +03:00
|
|
|
when we find the 'accepted rows' at once.
|
|
|
|
*/
|
|
|
|
Cost_estimate limit_cost;
|
2022-12-27 14:49:27 +02:00
|
|
|
calculate_costs(&limit_cost, keyno, n_ranges,
|
|
|
|
n_ranges - (uint)single_point_ranges,
|
|
|
|
*flags, top_limit, io_blocks,
|
|
|
|
unassigned_single_point_ranges);
|
2022-09-23 14:48:13 +03:00
|
|
|
cost->limit_cost= limit_cost.total_cost();
|
2019-02-03 14:56:12 -08:00
|
|
|
}
|
2022-09-30 17:10:37 +03:00
|
|
|
DBUG_PRINT("statistics",
|
|
|
|
("key: %s rows: %llu total_cost: %.3f io_blocks: %llu "
|
|
|
|
"cpu_cost: %.3f",
|
|
|
|
table->s->keynames.type_names[keyno],
|
|
|
|
(ulonglong) total_rows, cost->total_cost(),
|
|
|
|
(ulonglong) (cost->row_cost.io + cost->index_cost.io),
|
|
|
|
(double) (cost->row_cost.cpu + cost->index_cost.cpu)));
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
2020-03-13 15:28:42 +02:00
|
|
|
DBUG_RETURN(total_rows);
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
/**
|
|
|
|
Get cost and other information about MRR scan over some sequence of ranges
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
Calculate estimated cost and other information about an MRR scan for some
|
|
|
|
sequence of ranges.
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
The ranges themselves will be known only at execution phase. When this
|
|
|
|
function is called we only know number of ranges and a (rough) E(#records)
|
|
|
|
within those ranges.
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
Currently this function is only called for "n-keypart singlepoint" ranges,
|
|
|
|
i.e. each range is "keypart1=someconst1 AND ... AND keypartN=someconstN"
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
The flags parameter is a combination of those flags: HA_MRR_SORTED,
|
|
|
|
HA_MRR_INDEX_ONLY, HA_MRR_NO_ASSOCIATION, HA_MRR_LIMITS.
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
@param keyno Index number
|
|
|
|
@param n_ranges Estimated number of ranges (i.e. intervals) in the
|
|
|
|
range sequence.
|
Changing all cost calculation to be given in milliseconds
This makes it easier to compare different costs and also allows
the optimizer to optimizer different storage engines more reliably.
- Added tests/check_costs.pl, a tool to verify optimizer cost calculations.
- Most engine costs has been found with this program. All steps to
calculate the new costs are documented in Docs/optimizer_costs.txt
- User optimizer_cost variables are given in microseconds (as individual
costs can be very small). Internally they are stored in ms.
- Changed DISK_READ_COST (was DISK_SEEK_BASE_COST) from a hard disk cost
(9 ms) to common SSD cost (400MB/sec).
- Removed cost calculations for hard disks (rotation etc).
- Changed the following handler functions to return IO_AND_CPU_COST.
This makes it easy to apply different cost modifiers in ha_..time()
functions for io and cpu costs.
- scan_time()
- rnd_pos_time() & rnd_pos_call_time()
- keyread_time()
- Enhanched keyread_time() to calculate the full cost of reading of a set
of keys with a given number of ranges and optional number of blocks that
need to be accessed.
- Removed read_time() as keyread_time() + rnd_pos_time() can do the same
thing and more.
- Tuned cost for: heap, myisam, Aria, InnoDB, archive and MyRocks.
Used heap table costs for json_table. The rest are using default engine
costs.
- Added the following new optimizer variables:
- optimizer_disk_read_ratio
- optimizer_disk_read_cost
- optimizer_key_lookup_cost
- optimizer_row_lookup_cost
- optimizer_row_next_find_cost
- optimizer_scan_cost
- Moved all engine specific cost to OPTIMIZER_COSTS structure.
- Changed costs to use 'records_out' instead of 'records_read' when
recalculating costs.
- Split optimizer_costs.h to optimizer_costs.h and optimizer_defaults.h.
This allows one to change costs without having to compile a lot of
files.
- Updated costs for filter lookup.
- Use a better cost estimate in best_extension_by_limited_search()
for the sorting cost.
- Fixed previous issues with 'filtered' explain column as we are now
using 'records_out' (min rows seen for table) to calculate filtering.
This greatly simplifies the filtering code in
JOIN_TAB::save_explain_data().
This change caused a lot of queries to be optimized differently than
before, which exposed different issues in the optimizer that needs to
be fixed. These fixes are in the following commits. To not have to
change the same test case over and over again, the changes in the test
cases are done in a single commit after all the critical change sets
are done.
InnoDB changes:
- Updated InnoDB to not divide big range cost with 2.
- Added cost for InnoDB (innobase_update_optimizer_costs()).
- Don't mark clustered primary key with HA_KEYREAD_ONLY. This will
prevent that the optimizer is trying to use index-only scans on
the clustered key.
- Disabled ha_innobase::scan_time() and ha_innobase::read_time() and
ha_innobase::rnd_pos_time() as the default engine cost functions now
works good for InnoDB.
Other things:
- Added --show-query-costs (\Q) option to mysql.cc to show the query
cost after each query (good when working with query costs).
- Extended my_getopt with GET_ADJUSTED_VALUE which allows one to adjust
the value that user is given. This is used to change cost from
microseconds (user input) to milliseconds (what the server is
internally using).
- Added include/my_tracker.h ; Useful include file to quickly test
costs of a function.
- Use handler::set_table() in all places instead of 'table= arg'.
- Added SHOW_OPTIMIZER_COSTS to sys variables. These are input and
shown in microseconds for the user but stored as milliseconds.
This is to make the numbers easier to read for the user (less
pre-zeros). Implemented in 'Sys_var_optimizer_cost' class.
- In test_quick_select() do not use index scans if 'no_keyread' is set
for the table. This is what we do in other places of the server.
- Added THD parameter to Unique::get_use_cost() and
check_index_intersect_extension() and similar functions to be able
to provide costs to called functions.
- Changed 'records' to 'rows' in optimizer_trace.
- Write more information to optimizer_trace.
- Added INDEX_BLOCK_FILL_FACTOR_MUL (4) and INDEX_BLOCK_FILL_FACTOR_DIV (3)
to calculate usage space of keys in b-trees. (Before we used numeric
constants).
- Removed code that assumed that b-trees has similar costs as binary
trees. Replaced with engine calls that returns the cost.
- Added Bitmap::find_first_bit()
- Added timings to join_cache for ANALYZE table (patch by Sergei Petrunia).
- Added records_init and records_after_filter to POSITION to remember
more of what best_access_patch() calculates.
- table_after_join_selectivity() changed to recalculate 'records_out'
based on the new fields from best_access_patch()
Bug fixes:
- Some queries did not update last_query_cost (was 0). Fixed by moving
setting thd->...last_query_cost in JOIN::optimize().
- Write '0' as number of rows for const tables with a matching row.
Some internals:
- Engine cost are stored in OPTIMIZER_COSTS structure. When a
handlerton is created, we also created a new cost variable for the
handlerton. We also create a new variable if the user changes a
optimizer cost for a not yet loaded handlerton either with command
line arguments or with SET
@@global.engine.optimizer_cost_variable=xx.
- There are 3 global OPTIMIZER_COSTS variables:
default_optimizer_costs The default costs + changes from the
command line without an engine specifier.
heap_optimizer_costs Heap table costs, used for temporary tables
tmp_table_optimizer_costs The cost for the default on disk internal
temporary table (MyISAM or Aria)
- The engine cost for a table is stored in table_share. To speed up
accesses the handler has a pointer to this. The cost is copied
to the table on first access. If one wants to change the cost one
must first update the global engine cost and then do a FLUSH TABLES.
This was done to be able to access the costs for an open table
without any locks.
- When a handlerton is created, the cost are updated the following way:
See sql/keycaches.cc for details:
- Use 'default_optimizer_costs' as a base
- Call hton->update_optimizer_costs() to override with the engines
default costs.
- Override the costs that the user has specified for the engine.
- One handler open, copy the engine cost from handlerton to TABLE_SHARE.
- Call handler::update_optimizer_costs() to allow the engine to update
cost for this particular table.
- There are two costs stored in THD. These are copied to the handler
when the table is used in a query:
- optimizer_where_cost
- optimizer_scan_setup_cost
- Simply code in best_access_path() by storing all cost result in a
structure. (Idea/Suggestion by Igor)
2022-08-11 13:05:23 +03:00
|
|
|
@param total_rows Estimated total number of records contained within all
|
2009-12-22 15:33:21 +03:00
|
|
|
of the ranges
|
|
|
|
@param bufsz INOUT IN: Size of the buffer available for use
|
|
|
|
OUT: Size of the buffer that will be actually used, or
|
|
|
|
0 if buffer is not needed.
|
|
|
|
@param flags INOUT A combination of HA_MRR_* flags
|
|
|
|
@param cost OUT Estimated cost of MRR access
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
@retval
|
|
|
|
0 OK, *cost contains cost of the scan, *bufsz and *flags contain scan
|
|
|
|
parameters.
|
|
|
|
@retval
|
|
|
|
other Error or can't perform the requested scan
|
2009-12-15 10:16:46 +03:00
|
|
|
*/
|
|
|
|
|
2022-09-30 17:10:37 +03:00
|
|
|
ha_rows handler::multi_range_read_info(uint keyno, uint n_ranges,
|
|
|
|
uint total_rows,
|
2020-02-28 12:59:30 +02:00
|
|
|
uint key_parts, uint *bufsz,
|
2012-08-01 17:27:34 +03:00
|
|
|
uint *flags, Cost_estimate *cost)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2010-06-22 21:24:22 +04:00
|
|
|
/*
|
|
|
|
Currently we expect this function to be called only in preparation of scan
|
|
|
|
with HA_MRR_SINGLE_POINT property.
|
|
|
|
*/
|
|
|
|
DBUG_ASSERT(*flags | HA_MRR_SINGLE_POINT);
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
*bufsz= 0; /* Default implementation doesn't need a buffer */
|
|
|
|
*flags |= HA_MRR_USE_DEFAULT_IMPL;
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2022-09-30 17:10:37 +03:00
|
|
|
cost->reset(this);
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
/* Produce the same cost as non-MRR code does */
|
2020-03-24 22:22:57 +01:00
|
|
|
if (!is_clustering_key(keyno))
|
2019-02-03 14:56:12 -08:00
|
|
|
{
|
2022-09-30 17:10:37 +03:00
|
|
|
cost->index_cost= ha_keyread_time(keyno, n_ranges, total_rows, 0);
|
Changing all cost calculation to be given in milliseconds
This makes it easier to compare different costs and also allows
the optimizer to optimizer different storage engines more reliably.
- Added tests/check_costs.pl, a tool to verify optimizer cost calculations.
- Most engine costs has been found with this program. All steps to
calculate the new costs are documented in Docs/optimizer_costs.txt
- User optimizer_cost variables are given in microseconds (as individual
costs can be very small). Internally they are stored in ms.
- Changed DISK_READ_COST (was DISK_SEEK_BASE_COST) from a hard disk cost
(9 ms) to common SSD cost (400MB/sec).
- Removed cost calculations for hard disks (rotation etc).
- Changed the following handler functions to return IO_AND_CPU_COST.
This makes it easy to apply different cost modifiers in ha_..time()
functions for io and cpu costs.
- scan_time()
- rnd_pos_time() & rnd_pos_call_time()
- keyread_time()
- Enhanched keyread_time() to calculate the full cost of reading of a set
of keys with a given number of ranges and optional number of blocks that
need to be accessed.
- Removed read_time() as keyread_time() + rnd_pos_time() can do the same
thing and more.
- Tuned cost for: heap, myisam, Aria, InnoDB, archive and MyRocks.
Used heap table costs for json_table. The rest are using default engine
costs.
- Added the following new optimizer variables:
- optimizer_disk_read_ratio
- optimizer_disk_read_cost
- optimizer_key_lookup_cost
- optimizer_row_lookup_cost
- optimizer_row_next_find_cost
- optimizer_scan_cost
- Moved all engine specific cost to OPTIMIZER_COSTS structure.
- Changed costs to use 'records_out' instead of 'records_read' when
recalculating costs.
- Split optimizer_costs.h to optimizer_costs.h and optimizer_defaults.h.
This allows one to change costs without having to compile a lot of
files.
- Updated costs for filter lookup.
- Use a better cost estimate in best_extension_by_limited_search()
for the sorting cost.
- Fixed previous issues with 'filtered' explain column as we are now
using 'records_out' (min rows seen for table) to calculate filtering.
This greatly simplifies the filtering code in
JOIN_TAB::save_explain_data().
This change caused a lot of queries to be optimized differently than
before, which exposed different issues in the optimizer that needs to
be fixed. These fixes are in the following commits. To not have to
change the same test case over and over again, the changes in the test
cases are done in a single commit after all the critical change sets
are done.
InnoDB changes:
- Updated InnoDB to not divide big range cost with 2.
- Added cost for InnoDB (innobase_update_optimizer_costs()).
- Don't mark clustered primary key with HA_KEYREAD_ONLY. This will
prevent that the optimizer is trying to use index-only scans on
the clustered key.
- Disabled ha_innobase::scan_time() and ha_innobase::read_time() and
ha_innobase::rnd_pos_time() as the default engine cost functions now
works good for InnoDB.
Other things:
- Added --show-query-costs (\Q) option to mysql.cc to show the query
cost after each query (good when working with query costs).
- Extended my_getopt with GET_ADJUSTED_VALUE which allows one to adjust
the value that user is given. This is used to change cost from
microseconds (user input) to milliseconds (what the server is
internally using).
- Added include/my_tracker.h ; Useful include file to quickly test
costs of a function.
- Use handler::set_table() in all places instead of 'table= arg'.
- Added SHOW_OPTIMIZER_COSTS to sys variables. These are input and
shown in microseconds for the user but stored as milliseconds.
This is to make the numbers easier to read for the user (less
pre-zeros). Implemented in 'Sys_var_optimizer_cost' class.
- In test_quick_select() do not use index scans if 'no_keyread' is set
for the table. This is what we do in other places of the server.
- Added THD parameter to Unique::get_use_cost() and
check_index_intersect_extension() and similar functions to be able
to provide costs to called functions.
- Changed 'records' to 'rows' in optimizer_trace.
- Write more information to optimizer_trace.
- Added INDEX_BLOCK_FILL_FACTOR_MUL (4) and INDEX_BLOCK_FILL_FACTOR_DIV (3)
to calculate usage space of keys in b-trees. (Before we used numeric
constants).
- Removed code that assumed that b-trees has similar costs as binary
trees. Replaced with engine calls that returns the cost.
- Added Bitmap::find_first_bit()
- Added timings to join_cache for ANALYZE table (patch by Sergei Petrunia).
- Added records_init and records_after_filter to POSITION to remember
more of what best_access_patch() calculates.
- table_after_join_selectivity() changed to recalculate 'records_out'
based on the new fields from best_access_patch()
Bug fixes:
- Some queries did not update last_query_cost (was 0). Fixed by moving
setting thd->...last_query_cost in JOIN::optimize().
- Write '0' as number of rows for const tables with a matching row.
Some internals:
- Engine cost are stored in OPTIMIZER_COSTS structure. When a
handlerton is created, we also created a new cost variable for the
handlerton. We also create a new variable if the user changes a
optimizer cost for a not yet loaded handlerton either with command
line arguments or with SET
@@global.engine.optimizer_cost_variable=xx.
- There are 3 global OPTIMIZER_COSTS variables:
default_optimizer_costs The default costs + changes from the
command line without an engine specifier.
heap_optimizer_costs Heap table costs, used for temporary tables
tmp_table_optimizer_costs The cost for the default on disk internal
temporary table (MyISAM or Aria)
- The engine cost for a table is stored in table_share. To speed up
accesses the handler has a pointer to this. The cost is copied
to the table on first access. If one wants to change the cost one
must first update the global engine cost and then do a FLUSH TABLES.
This was done to be able to access the costs for an open table
without any locks.
- When a handlerton is created, the cost are updated the following way:
See sql/keycaches.cc for details:
- Use 'default_optimizer_costs' as a base
- Call hton->update_optimizer_costs() to override with the engines
default costs.
- Override the costs that the user has specified for the engine.
- One handler open, copy the engine cost from handlerton to TABLE_SHARE.
- Call handler::update_optimizer_costs() to allow the engine to update
cost for this particular table.
- There are two costs stored in THD. These are copied to the handler
when the table is used in a query:
- optimizer_where_cost
- optimizer_scan_setup_cost
- Simply code in best_access_path() by storing all cost result in a
structure. (Idea/Suggestion by Igor)
2022-08-11 13:05:23 +03:00
|
|
|
|
2019-02-03 14:56:12 -08:00
|
|
|
if (!(*flags & HA_MRR_INDEX_ONLY))
|
|
|
|
{
|
Changing all cost calculation to be given in milliseconds
This makes it easier to compare different costs and also allows
the optimizer to optimizer different storage engines more reliably.
- Added tests/check_costs.pl, a tool to verify optimizer cost calculations.
- Most engine costs has been found with this program. All steps to
calculate the new costs are documented in Docs/optimizer_costs.txt
- User optimizer_cost variables are given in microseconds (as individual
costs can be very small). Internally they are stored in ms.
- Changed DISK_READ_COST (was DISK_SEEK_BASE_COST) from a hard disk cost
(9 ms) to common SSD cost (400MB/sec).
- Removed cost calculations for hard disks (rotation etc).
- Changed the following handler functions to return IO_AND_CPU_COST.
This makes it easy to apply different cost modifiers in ha_..time()
functions for io and cpu costs.
- scan_time()
- rnd_pos_time() & rnd_pos_call_time()
- keyread_time()
- Enhanched keyread_time() to calculate the full cost of reading of a set
of keys with a given number of ranges and optional number of blocks that
need to be accessed.
- Removed read_time() as keyread_time() + rnd_pos_time() can do the same
thing and more.
- Tuned cost for: heap, myisam, Aria, InnoDB, archive and MyRocks.
Used heap table costs for json_table. The rest are using default engine
costs.
- Added the following new optimizer variables:
- optimizer_disk_read_ratio
- optimizer_disk_read_cost
- optimizer_key_lookup_cost
- optimizer_row_lookup_cost
- optimizer_row_next_find_cost
- optimizer_scan_cost
- Moved all engine specific cost to OPTIMIZER_COSTS structure.
- Changed costs to use 'records_out' instead of 'records_read' when
recalculating costs.
- Split optimizer_costs.h to optimizer_costs.h and optimizer_defaults.h.
This allows one to change costs without having to compile a lot of
files.
- Updated costs for filter lookup.
- Use a better cost estimate in best_extension_by_limited_search()
for the sorting cost.
- Fixed previous issues with 'filtered' explain column as we are now
using 'records_out' (min rows seen for table) to calculate filtering.
This greatly simplifies the filtering code in
JOIN_TAB::save_explain_data().
This change caused a lot of queries to be optimized differently than
before, which exposed different issues in the optimizer that needs to
be fixed. These fixes are in the following commits. To not have to
change the same test case over and over again, the changes in the test
cases are done in a single commit after all the critical change sets
are done.
InnoDB changes:
- Updated InnoDB to not divide big range cost with 2.
- Added cost for InnoDB (innobase_update_optimizer_costs()).
- Don't mark clustered primary key with HA_KEYREAD_ONLY. This will
prevent that the optimizer is trying to use index-only scans on
the clustered key.
- Disabled ha_innobase::scan_time() and ha_innobase::read_time() and
ha_innobase::rnd_pos_time() as the default engine cost functions now
works good for InnoDB.
Other things:
- Added --show-query-costs (\Q) option to mysql.cc to show the query
cost after each query (good when working with query costs).
- Extended my_getopt with GET_ADJUSTED_VALUE which allows one to adjust
the value that user is given. This is used to change cost from
microseconds (user input) to milliseconds (what the server is
internally using).
- Added include/my_tracker.h ; Useful include file to quickly test
costs of a function.
- Use handler::set_table() in all places instead of 'table= arg'.
- Added SHOW_OPTIMIZER_COSTS to sys variables. These are input and
shown in microseconds for the user but stored as milliseconds.
This is to make the numbers easier to read for the user (less
pre-zeros). Implemented in 'Sys_var_optimizer_cost' class.
- In test_quick_select() do not use index scans if 'no_keyread' is set
for the table. This is what we do in other places of the server.
- Added THD parameter to Unique::get_use_cost() and
check_index_intersect_extension() and similar functions to be able
to provide costs to called functions.
- Changed 'records' to 'rows' in optimizer_trace.
- Write more information to optimizer_trace.
- Added INDEX_BLOCK_FILL_FACTOR_MUL (4) and INDEX_BLOCK_FILL_FACTOR_DIV (3)
to calculate usage space of keys in b-trees. (Before we used numeric
constants).
- Removed code that assumed that b-trees has similar costs as binary
trees. Replaced with engine calls that returns the cost.
- Added Bitmap::find_first_bit()
- Added timings to join_cache for ANALYZE table (patch by Sergei Petrunia).
- Added records_init and records_after_filter to POSITION to remember
more of what best_access_patch() calculates.
- table_after_join_selectivity() changed to recalculate 'records_out'
based on the new fields from best_access_patch()
Bug fixes:
- Some queries did not update last_query_cost (was 0). Fixed by moving
setting thd->...last_query_cost in JOIN::optimize().
- Write '0' as number of rows for const tables with a matching row.
Some internals:
- Engine cost are stored in OPTIMIZER_COSTS structure. When a
handlerton is created, we also created a new cost variable for the
handlerton. We also create a new variable if the user changes a
optimizer cost for a not yet loaded handlerton either with command
line arguments or with SET
@@global.engine.optimizer_cost_variable=xx.
- There are 3 global OPTIMIZER_COSTS variables:
default_optimizer_costs The default costs + changes from the
command line without an engine specifier.
heap_optimizer_costs Heap table costs, used for temporary tables
tmp_table_optimizer_costs The cost for the default on disk internal
temporary table (MyISAM or Aria)
- The engine cost for a table is stored in table_share. To speed up
accesses the handler has a pointer to this. The cost is copied
to the table on first access. If one wants to change the cost one
must first update the global engine cost and then do a FLUSH TABLES.
This was done to be able to access the costs for an open table
without any locks.
- When a handlerton is created, the cost are updated the following way:
See sql/keycaches.cc for details:
- Use 'default_optimizer_costs' as a base
- Call hton->update_optimizer_costs() to override with the engines
default costs.
- Override the costs that the user has specified for the engine.
- One handler open, copy the engine cost from handlerton to TABLE_SHARE.
- Call handler::update_optimizer_costs() to allow the engine to update
cost for this particular table.
- There are two costs stored in THD. These are copied to the handler
when the table is used in a query:
- optimizer_where_cost
- optimizer_scan_setup_cost
- Simply code in best_access_path() by storing all cost result in a
structure. (Idea/Suggestion by Igor)
2022-08-11 13:05:23 +03:00
|
|
|
/* ha_rnd_pos_time includes ROW_COPY_COST */
|
2022-09-30 17:10:37 +03:00
|
|
|
cost->row_cost= ha_rnd_pos_time(total_rows);
|
Update row and key fetch cost models to take into account data copy costs
Before this patch, when calculating the cost of fetching and using a
row/key from the engine, we took into account the cost of finding a
row or key from the engine, but did not consistently take into account
index only accessed, clustered key or covered keys for all access
paths.
The cost of the WHERE clause (TIME_FOR_COMPARE) was not consistently
considered in best_access_path(). TIME_FOR_COMPARE was used in
calculation in other places, like greedy_search(), but was in some
cases (like scans) done an a different number of rows than was
accessed.
The cost calculation of row and index scans didn't take into account
the number of rows that where accessed, only the number of accepted
rows.
When using a filter, the cost of index_only_reads and cost of
accessing and disregarding 'filtered rows' where not taken into
account, which made filters cost less than there actually where.
To remedy the above, the following key & row fetch related costs
has been added:
- The cost of fetching and using a row is now split into different costs:
- key + Row fetch cost (as before) but multiplied with the variable
'optimizer_cache_cost' (default to 0.5). This allows the user to
tell the optimizer the likehood of finding the key and row in the
engine cache.
- ROW_COPY_COST, The cost copying a row from the engine to the
sql layer or creating a row from the join_cache to the record
buffer. Mostly affects table scan costs.
- ROW_LOOKUP_COST, the cost of fetching a row by rowid.
- KEY_COPY_COST the cost of finding the next key and copying it from
the engine to the SQL layer. This is used when we calculate the cost
index only reads. It makes index scans more expensive than before if
they cover a lot of rows. (main.index_merge_myisam)
- KEY_LOOKUP_COST, the cost of finding the first key in a range.
This replaces the old define IDX_LOOKUP_COST, but with a higher cost.
- KEY_NEXT_FIND_COST, the cost of finding the next key (and rowid).
when doing a index scan and comparing the rowid to the filter.
Before this cost was assumed to be 0.
All of the above constants/variables are now tuned to be somewhat in
proportion of executing complexity to each other. There is tuning
need for these in the future, but that can wait until the above are
made user variables as that will make tuning much easier.
To make the usage of the above easy, there are new (not virtual)
cost calclation functions in handler:
- ha_read_time(), like read_time(), but take optimizer_cache_cost into
account.
- ha_read_and_copy_time(), like ha_read_time() but take into account
ROW_COPY_TIME
- ha_read_and_compare_time(), like ha_read_and_copy_time() but take
TIME_FOR_COMPARE into account.
- ha_rnd_pos_time(). Read row with row id, taking ROW_COPY_COST
into account. This is used with filesort where we don't need
to execute the WHERE clause again.
- ha_keyread_time(), like keyread_time() but take
optimizer_cache_cost into account.
- ha_keyread_and_copy_time(), like ha_keyread_time(), but add
KEY_COPY_COST.
- ha_key_scan_time(), like key_scan_time() but take
optimizer_cache_cost nto account.
- ha_key_scan_and_compare_time(), like ha_key_scan_time(), but add
KEY_COPY_COST & TIME_FOR_COMPARE.
I also added some setup costs for doing different types of scans and
creating temporary tables (on disk and in memory). This encourages
the optimizer to not use these for simple 'a few row' lookups if
there are adequate key lookup strategies.
- TABLE_SCAN_SETUP_COST, cost of starting a table scan.
- INDEX_SCAN_SETUP_COST, cost of starting an index scan.
- HEAP_TEMPTABLE_CREATE_COST, cost of creating in memory
temporary table.
- DISK_TEMPTABLE_CREATE_COST, cost of creating an on disk temporary
table.
When calculating cost of fetching ranges, we had a cost of
IDX_LOOKUP_COST (0.125) for doing a key div for a new range. This is
now replaced with 'io_cost * KEY_LOOKUP_COST (1.0) *
optimizer_cache_cost', which matches the cost we use for 'ref' and
other key lookups. The effect is that the cost is now a bit higher
when we have many ranges for a key.
Allmost all calculation with TIME_FOR_COMPARE is now done in
best_access_path(). 'JOIN::read_time' now includes the full
cost for finding the rows in the table.
In the result files, many of the changes are now again close to what
they where before the "Update cost for hash and cached joins" commit,
as that commit didn't fix the filter cost (too complex to do
everything in one commit).
The above changes showed a lot of a lot of inconsistencies in
optimizer cost calculation. The main objective with the other changes
was to do calculation as similar (and accurate) as possible and to make
different plans more comparable.
Detailed list of changes:
- Calculate index_only_cost consistently and correctly for all scan
and ref accesses. The row fetch_cost and index_only_cost now
takes into account clustered keys, covered keys and index
only accesses.
- cost_for_index_read now returns both full cost and index_only_cost
- Fixed cost calculation of get_sweep_read_cost() to match other
similar costs. This is bases on the assumption that data is more
often stored on SSD than a hard disk.
- Replaced constant 2.0 with new define TABLE_SCAN_SETUP_COST.
- Some scan cost estimates did not take into account
TIME_FOR_COMPARE. Now all scan costs takes this into
account. (main.show_explain)
- Added session variable optimizer_cache_hit_ratio (default 50%). By
adjusting this on can reduce or increase the cost of index or direct
record lookups. The effect of the default is that key lookups is now
a bit cheaper than before. See usage of 'optimizer_cache_cost' in
handler.h.
- JOIN_TAB::scan_time() did not take into account index only scans,
which produced a wrong cost when index scan was used. Changed
JOIN_TAB:::scan_time() to take into consideration clustered and
covered keys. The values are now cached and we only have to call
this function once. Other calls are changed to use the cached
values. Function renamed to JOIN_TAB::estimate_scan_time().
- Fixed that most index cost calculations are done the same way and
more close to 'range' calculations. The cost is now lower than
before for small data sets and higher for large data sets as we take
into account how many keys are read (main.opt_trace_selectivity,
main.limit_rows_examined).
- Ensured that index_scan_cost() ==
range(scan_of_all_rows_in_table_using_one_range) +
MULTI_RANGE_READ_INFO_CONST. One effect of this is that if there
is choice of doing a full index scan and a range-index scan over
almost the whole table then index scan will be preferred (no
range-read setup cost). (innodb.innodb, main.show_explain,
main.range)
- Fixed the EQ_REF and REF takes into account clustered and covered
keys. This changes some plans to use covered or clustered indexes
as these are much cheaper. (main.subselect_mat_cost,
main.state_tables_innodb, main.limit_rows_examined)
- Rowid filter setup cost and filter compare cost now takes into
account fetching and checking the rowid (KEY_NEXT_FIND_COST).
(main.partition_pruning heap.heap_btree main.log_state)
- Added KEY_NEXT_FIND_COST to
Range_rowid_filter_cost_info::lookup_cost to account of the time
to find and check the next key value against the container
- Introduced ha_keyread_time(rows) that takes into account finding
the next row and copying the key value to 'record'
(KEY_COPY_COST).
- Introduced ha_key_scan_time() for calculating an index scan over
all rows.
- Added IDX_LOOKUP_COST to keyread_time() as a startup cost.
- Added index_only_fetch_cost() as a convenience function to
OPT_RANGE.
- keyread_time() cost is slightly reduced to prefer shorter keys.
(main.index_merge_myisam)
- All of the above caused some index_merge combinations to be
rejected because of cost (main.index_intersect). In some cases
'ref' where replaced with index_merge because of the low
cost calculation of get_sweep_read_cost().
- Some index usage moved from PRIMARY to a covering index.
(main.subselect_innodb)
- Changed cost calculation of filter to take KEY_LOOKUP_COST and
TIME_FOR_COMPARE into account. See sql_select.cc::apply_filter().
filter parameters and costs are now written to optimizer_trace.
- Don't use matchings_records_in_range() to try to estimate the number
of filtered rows for ranges. The reason is that we want to ensure
that 'range' is calculated similar to 'ref'. There is also more work
needed to calculate the selectivity when using ranges and ranges and
filtering. This causes filtering column in EXPLAIN EXTENDED to be
100.00 for some cases where range cannot use filtering.
(main.rowid_filter)
- Introduced ha_scan_time() that takes into account the CPU cost of
finding the next row and copying the row from the engine to
'record'. This causes costs of table scan to slightly increase and
some test to changed their plan from ALL to RANGE or ALL to ref.
(innodb.innodb_mysql, main.select_pkeycache)
In a few cases where scan time of very small tables have lower cost
than a ref or range, things changed from ref/range to ALL.
(main.myisam, main.func_group, main.limit_rows_examined,
main.subselect2)
- Introduced ha_scan_and_compare_time() which is like ha_scan_time()
but also adds the cost of the where clause (TIME_FOR_COMPARE).
- Added small cost for creating temporary table for
materialization. This causes some very small tables to use scan
instead of materialization.
- Added checking of the WHERE clause (TIME_FOR_COMPARE) of the
accepted rows to ROR costs in get_best_ror_intersect()
- Removed '- 0.001' from 'join->best_read' and optimize_straight_join()
to ensure that the 'Last_query_cost' status variable contains the
same value as the one that was calculated by the optimizer.
- Take avg_io_cost() into account in handler::keyread_time() and
handler::read_time(). This should have no effect as it's 1.0 by
default, except for heap that overrides these functions.
- Some 'ref_or_null' accesses changed to 'range' because of cost
adjustments (main.order_by)
- Added scan type "scan_with_join_cache" for optimizer_trace. This is
just to show in the trace what kind of scan was used.
- When using 'scan_with_join_cache' take into account number of
preceding tables (as have to restore all fields for all previous
table combination when checking the where clause)
The new cost added is:
(row_combinations * ROW_COPY_COST * number_of_cached_tables).
This increases the cost of join buffering in proportion of the
number of tables in the join buffer. One effect is that full scans
are now done earlier as the cost is then smaller.
(main.join_outer_innodb, main.greedy_optimizer)
- Removed the usage of 'worst_seeks' in cost_for_index_read as it
caused wrong plans to be created; It prefered JT_EQ_REF even if it
would be much more expensive than a full table scan. A related
issue was that worst_seeks only applied to full lookup, not to
clustered or index only lookups, which is not consistent. This
caused some plans to use index scan instead of eq_ref (main.union)
- Changed federated block size from 4096 to 1500, which is the
typical size of an IO packet.
- Added costs for reading rows to Federated. Needed as there is no
caching of rows in the federated engine.
- Added ha_innobase::rnd_pos_time() cost function.
- A lot of extra things added to optimizer trace
- More costs, especially for materialization and index_merge.
- Make lables more uniform
- Fixed a lot of minor bugs
- Added 'trace_started()' around a lot of trace blocks.
- When calculating ORDER BY with LIMIT cost for using an index
the cost did not take into account the number of row retrivals
that has to be done or the cost of comparing the rows with the
WHERE clause. The cost calculated would be just a fraction of
the real cost. Now we calculate the cost as we do for ranges
and 'ref'.
- 'Using index for group-by' is used a bit more than before as
now take into account the WHERE clause cost when comparing
with 'ref' and prefer the method with fewer row combinations.
(main.group_min_max).
Bugs fixed:
- Fixed that we don't calculate TIME_FOR_COMPARE twice for some plans,
like in optimize_straight_join() and greedy_search()
- Fixed bug in save_explain_data where we could test for the wrong
index when displaying 'Using index'. This caused some old plans to
show 'Using index'. (main.subselect_innodb, main.subselect2)
- Fixed bug in get_best_ror_intersect() where 'min_cost' was not
updated, and the cost we compared with was not the one that was
used.
- Fixed very wrong cost calculation for priority queues in
check_if_pq_applicable(). (main.order_by now correctly uses priority
queue)
- When calculating cost of EQ_REF or REF, we added the cost of
comparing the WHERE clause with the found rows, not all row
combinations. This made ref and eq_ref to be regarded way to cheap
compared to other access methods.
- FORCE INDEX cost calculation didn't take into account clustered or
covered indexes.
- JT_EQ_REF cost was estimated as avg_io_cost(), which is half the
cost of a JT_REF key. This may be true for InnoDB primary key, but
not for other unique keys or other engines. Now we use handler
function to calculate the cost, which allows us to handle
consistently clustered, covered keys and not covered keys.
- ha_start_keyread() didn't call extra_opt() if keyread was already
enabled but still changed the 'keyread' variable (which is wrong).
Fixed by not doing anything if keyread is already enabled.
- multi_range_read_info_cost() didn't take into account io_cost when
calculating the cost of ranges.
- fix_semijoin_strategies_for_picked_join_order() used the wrong
record_count when calling best_access_path() for SJ_OPT_FIRST_MATCH
and SJ_OPT_LOOSE_SCAN.
- Hash joins didn't provide correct best_cost to the upper level, which
means that the cost for hash_joins more expensive than calculated
in best_access_path (a difference of 10x * TIME_OF_COMPARE).
This is fixed in the new code thanks to that we now include
TIME_OF_COMPARE cost in 'read_time'.
Other things:
- Added some 'if (thd->trace_started())' to speed up code
- Removed not used function Cost_estimate::is_zero()
- Simplified testing of HA_POS_ERROR in get_best_ror_intersect().
(No cost changes)
- Moved ha_start_keyread() from join_read_const_table() to join_read_const()
to enable keyread for all types of JT_CONST tables.
- Made a few very short functions inline in handler.h
Notes:
- In main.rowid_filter the join order of order and lineitem is swapped.
This is because the cost of doing a range fetch of lineitem(98 rows) is
almost as big as the whole join of order,lineitem. The filtering will
also ensure that we only have to do very small key fetches of the rows
in lineitem.
- main.index_merge_myisam had a few changes where we are now using
less keys for index_merge. This is because index scans are now more
expensive than before.
- handler->optimizer_cache_cost is updated in ha_external_lock().
This ensures that it is up to date per statements.
Not an optimal solution (for locked tables), but should be ok for now.
- 'DELETE FROM t1 WHERE t1.a > 0 ORDER BY t1.a' does not take cost of
filesort into consideration when table scan is chosen.
(main.myisam_explain_non_select_all)
- perfschema.table_aggregate_global_* has changed because an update
on a table with 1 row will now use table scan instead of key lookup.
TODO in upcomming commits:
- Fix selectivity calculation for ranges with and without filtering and
when there is a ref access but scan is chosen.
For this we have to store the lowest known value for
'accepted_records' in the OPT_RANGE structure.
- Change that records_read does not include filtered rows.
- test_if_cheaper_ordering() needs to be updated to properly calculate
costs. This will fix tests like main.order_by_innodb,
main.single_delete_update
- Extend get_range_limit_read_cost() to take into considering
cost_for_index_read() if there where no quick keys. This will reduce
the computed cost for ORDER BY with LIMIT in some cases.
(main.innodb_ext_key)
- Fix that we take into account selectivity when counting the number
of rows we have to read when considering using a index table scan to
resolve ORDER BY.
- Add new calculation for rnd_pos_time() where we take into account the
benefit of reading multiple rows from the same page.
2021-11-01 12:34:24 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
Changing all cost calculation to be given in milliseconds
This makes it easier to compare different costs and also allows
the optimizer to optimizer different storage engines more reliably.
- Added tests/check_costs.pl, a tool to verify optimizer cost calculations.
- Most engine costs has been found with this program. All steps to
calculate the new costs are documented in Docs/optimizer_costs.txt
- User optimizer_cost variables are given in microseconds (as individual
costs can be very small). Internally they are stored in ms.
- Changed DISK_READ_COST (was DISK_SEEK_BASE_COST) from a hard disk cost
(9 ms) to common SSD cost (400MB/sec).
- Removed cost calculations for hard disks (rotation etc).
- Changed the following handler functions to return IO_AND_CPU_COST.
This makes it easy to apply different cost modifiers in ha_..time()
functions for io and cpu costs.
- scan_time()
- rnd_pos_time() & rnd_pos_call_time()
- keyread_time()
- Enhanched keyread_time() to calculate the full cost of reading of a set
of keys with a given number of ranges and optional number of blocks that
need to be accessed.
- Removed read_time() as keyread_time() + rnd_pos_time() can do the same
thing and more.
- Tuned cost for: heap, myisam, Aria, InnoDB, archive and MyRocks.
Used heap table costs for json_table. The rest are using default engine
costs.
- Added the following new optimizer variables:
- optimizer_disk_read_ratio
- optimizer_disk_read_cost
- optimizer_key_lookup_cost
- optimizer_row_lookup_cost
- optimizer_row_next_find_cost
- optimizer_scan_cost
- Moved all engine specific cost to OPTIMIZER_COSTS structure.
- Changed costs to use 'records_out' instead of 'records_read' when
recalculating costs.
- Split optimizer_costs.h to optimizer_costs.h and optimizer_defaults.h.
This allows one to change costs without having to compile a lot of
files.
- Updated costs for filter lookup.
- Use a better cost estimate in best_extension_by_limited_search()
for the sorting cost.
- Fixed previous issues with 'filtered' explain column as we are now
using 'records_out' (min rows seen for table) to calculate filtering.
This greatly simplifies the filtering code in
JOIN_TAB::save_explain_data().
This change caused a lot of queries to be optimized differently than
before, which exposed different issues in the optimizer that needs to
be fixed. These fixes are in the following commits. To not have to
change the same test case over and over again, the changes in the test
cases are done in a single commit after all the critical change sets
are done.
InnoDB changes:
- Updated InnoDB to not divide big range cost with 2.
- Added cost for InnoDB (innobase_update_optimizer_costs()).
- Don't mark clustered primary key with HA_KEYREAD_ONLY. This will
prevent that the optimizer is trying to use index-only scans on
the clustered key.
- Disabled ha_innobase::scan_time() and ha_innobase::read_time() and
ha_innobase::rnd_pos_time() as the default engine cost functions now
works good for InnoDB.
Other things:
- Added --show-query-costs (\Q) option to mysql.cc to show the query
cost after each query (good when working with query costs).
- Extended my_getopt with GET_ADJUSTED_VALUE which allows one to adjust
the value that user is given. This is used to change cost from
microseconds (user input) to milliseconds (what the server is
internally using).
- Added include/my_tracker.h ; Useful include file to quickly test
costs of a function.
- Use handler::set_table() in all places instead of 'table= arg'.
- Added SHOW_OPTIMIZER_COSTS to sys variables. These are input and
shown in microseconds for the user but stored as milliseconds.
This is to make the numbers easier to read for the user (less
pre-zeros). Implemented in 'Sys_var_optimizer_cost' class.
- In test_quick_select() do not use index scans if 'no_keyread' is set
for the table. This is what we do in other places of the server.
- Added THD parameter to Unique::get_use_cost() and
check_index_intersect_extension() and similar functions to be able
to provide costs to called functions.
- Changed 'records' to 'rows' in optimizer_trace.
- Write more information to optimizer_trace.
- Added INDEX_BLOCK_FILL_FACTOR_MUL (4) and INDEX_BLOCK_FILL_FACTOR_DIV (3)
to calculate usage space of keys in b-trees. (Before we used numeric
constants).
- Removed code that assumed that b-trees has similar costs as binary
trees. Replaced with engine calls that returns the cost.
- Added Bitmap::find_first_bit()
- Added timings to join_cache for ANALYZE table (patch by Sergei Petrunia).
- Added records_init and records_after_filter to POSITION to remember
more of what best_access_patch() calculates.
- table_after_join_selectivity() changed to recalculate 'records_out'
based on the new fields from best_access_patch()
Bug fixes:
- Some queries did not update last_query_cost (was 0). Fixed by moving
setting thd->...last_query_cost in JOIN::optimize().
- Write '0' as number of rows for const tables with a matching row.
Some internals:
- Engine cost are stored in OPTIMIZER_COSTS structure. When a
handlerton is created, we also created a new cost variable for the
handlerton. We also create a new variable if the user changes a
optimizer cost for a not yet loaded handlerton either with command
line arguments or with SET
@@global.engine.optimizer_cost_variable=xx.
- There are 3 global OPTIMIZER_COSTS variables:
default_optimizer_costs The default costs + changes from the
command line without an engine specifier.
heap_optimizer_costs Heap table costs, used for temporary tables
tmp_table_optimizer_costs The cost for the default on disk internal
temporary table (MyISAM or Aria)
- The engine cost for a table is stored in table_share. To speed up
accesses the handler has a pointer to this. The cost is copied
to the table on first access. If one wants to change the cost one
must first update the global engine cost and then do a FLUSH TABLES.
This was done to be able to access the costs for an open table
without any locks.
- When a handlerton is created, the cost are updated the following way:
See sql/keycaches.cc for details:
- Use 'default_optimizer_costs' as a base
- Call hton->update_optimizer_costs() to override with the engines
default costs.
- Override the costs that the user has specified for the engine.
- One handler open, copy the engine cost from handlerton to TABLE_SHARE.
- Call handler::update_optimizer_costs() to allow the engine to update
cost for this particular table.
- There are two costs stored in THD. These are copied to the handler
when the table is used in a query:
- optimizer_where_cost
- optimizer_scan_setup_cost
- Simply code in best_access_path() by storing all cost result in a
structure. (Idea/Suggestion by Igor)
2022-08-11 13:05:23 +03:00
|
|
|
/* Index only read */
|
|
|
|
cost->copy_cost= rows2double(total_rows) * KEY_COPY_COST;
|
2019-02-03 14:56:12 -08:00
|
|
|
}
|
|
|
|
}
|
2009-12-22 15:33:21 +03:00
|
|
|
else
|
2019-02-03 14:56:12 -08:00
|
|
|
{
|
Update row and key fetch cost models to take into account data copy costs
Before this patch, when calculating the cost of fetching and using a
row/key from the engine, we took into account the cost of finding a
row or key from the engine, but did not consistently take into account
index only accessed, clustered key or covered keys for all access
paths.
The cost of the WHERE clause (TIME_FOR_COMPARE) was not consistently
considered in best_access_path(). TIME_FOR_COMPARE was used in
calculation in other places, like greedy_search(), but was in some
cases (like scans) done an a different number of rows than was
accessed.
The cost calculation of row and index scans didn't take into account
the number of rows that where accessed, only the number of accepted
rows.
When using a filter, the cost of index_only_reads and cost of
accessing and disregarding 'filtered rows' where not taken into
account, which made filters cost less than there actually where.
To remedy the above, the following key & row fetch related costs
has been added:
- The cost of fetching and using a row is now split into different costs:
- key + Row fetch cost (as before) but multiplied with the variable
'optimizer_cache_cost' (default to 0.5). This allows the user to
tell the optimizer the likehood of finding the key and row in the
engine cache.
- ROW_COPY_COST, The cost copying a row from the engine to the
sql layer or creating a row from the join_cache to the record
buffer. Mostly affects table scan costs.
- ROW_LOOKUP_COST, the cost of fetching a row by rowid.
- KEY_COPY_COST the cost of finding the next key and copying it from
the engine to the SQL layer. This is used when we calculate the cost
index only reads. It makes index scans more expensive than before if
they cover a lot of rows. (main.index_merge_myisam)
- KEY_LOOKUP_COST, the cost of finding the first key in a range.
This replaces the old define IDX_LOOKUP_COST, but with a higher cost.
- KEY_NEXT_FIND_COST, the cost of finding the next key (and rowid).
when doing a index scan and comparing the rowid to the filter.
Before this cost was assumed to be 0.
All of the above constants/variables are now tuned to be somewhat in
proportion of executing complexity to each other. There is tuning
need for these in the future, but that can wait until the above are
made user variables as that will make tuning much easier.
To make the usage of the above easy, there are new (not virtual)
cost calclation functions in handler:
- ha_read_time(), like read_time(), but take optimizer_cache_cost into
account.
- ha_read_and_copy_time(), like ha_read_time() but take into account
ROW_COPY_TIME
- ha_read_and_compare_time(), like ha_read_and_copy_time() but take
TIME_FOR_COMPARE into account.
- ha_rnd_pos_time(). Read row with row id, taking ROW_COPY_COST
into account. This is used with filesort where we don't need
to execute the WHERE clause again.
- ha_keyread_time(), like keyread_time() but take
optimizer_cache_cost into account.
- ha_keyread_and_copy_time(), like ha_keyread_time(), but add
KEY_COPY_COST.
- ha_key_scan_time(), like key_scan_time() but take
optimizer_cache_cost nto account.
- ha_key_scan_and_compare_time(), like ha_key_scan_time(), but add
KEY_COPY_COST & TIME_FOR_COMPARE.
I also added some setup costs for doing different types of scans and
creating temporary tables (on disk and in memory). This encourages
the optimizer to not use these for simple 'a few row' lookups if
there are adequate key lookup strategies.
- TABLE_SCAN_SETUP_COST, cost of starting a table scan.
- INDEX_SCAN_SETUP_COST, cost of starting an index scan.
- HEAP_TEMPTABLE_CREATE_COST, cost of creating in memory
temporary table.
- DISK_TEMPTABLE_CREATE_COST, cost of creating an on disk temporary
table.
When calculating cost of fetching ranges, we had a cost of
IDX_LOOKUP_COST (0.125) for doing a key div for a new range. This is
now replaced with 'io_cost * KEY_LOOKUP_COST (1.0) *
optimizer_cache_cost', which matches the cost we use for 'ref' and
other key lookups. The effect is that the cost is now a bit higher
when we have many ranges for a key.
Allmost all calculation with TIME_FOR_COMPARE is now done in
best_access_path(). 'JOIN::read_time' now includes the full
cost for finding the rows in the table.
In the result files, many of the changes are now again close to what
they where before the "Update cost for hash and cached joins" commit,
as that commit didn't fix the filter cost (too complex to do
everything in one commit).
The above changes showed a lot of a lot of inconsistencies in
optimizer cost calculation. The main objective with the other changes
was to do calculation as similar (and accurate) as possible and to make
different plans more comparable.
Detailed list of changes:
- Calculate index_only_cost consistently and correctly for all scan
and ref accesses. The row fetch_cost and index_only_cost now
takes into account clustered keys, covered keys and index
only accesses.
- cost_for_index_read now returns both full cost and index_only_cost
- Fixed cost calculation of get_sweep_read_cost() to match other
similar costs. This is bases on the assumption that data is more
often stored on SSD than a hard disk.
- Replaced constant 2.0 with new define TABLE_SCAN_SETUP_COST.
- Some scan cost estimates did not take into account
TIME_FOR_COMPARE. Now all scan costs takes this into
account. (main.show_explain)
- Added session variable optimizer_cache_hit_ratio (default 50%). By
adjusting this on can reduce or increase the cost of index or direct
record lookups. The effect of the default is that key lookups is now
a bit cheaper than before. See usage of 'optimizer_cache_cost' in
handler.h.
- JOIN_TAB::scan_time() did not take into account index only scans,
which produced a wrong cost when index scan was used. Changed
JOIN_TAB:::scan_time() to take into consideration clustered and
covered keys. The values are now cached and we only have to call
this function once. Other calls are changed to use the cached
values. Function renamed to JOIN_TAB::estimate_scan_time().
- Fixed that most index cost calculations are done the same way and
more close to 'range' calculations. The cost is now lower than
before for small data sets and higher for large data sets as we take
into account how many keys are read (main.opt_trace_selectivity,
main.limit_rows_examined).
- Ensured that index_scan_cost() ==
range(scan_of_all_rows_in_table_using_one_range) +
MULTI_RANGE_READ_INFO_CONST. One effect of this is that if there
is choice of doing a full index scan and a range-index scan over
almost the whole table then index scan will be preferred (no
range-read setup cost). (innodb.innodb, main.show_explain,
main.range)
- Fixed the EQ_REF and REF takes into account clustered and covered
keys. This changes some plans to use covered or clustered indexes
as these are much cheaper. (main.subselect_mat_cost,
main.state_tables_innodb, main.limit_rows_examined)
- Rowid filter setup cost and filter compare cost now takes into
account fetching and checking the rowid (KEY_NEXT_FIND_COST).
(main.partition_pruning heap.heap_btree main.log_state)
- Added KEY_NEXT_FIND_COST to
Range_rowid_filter_cost_info::lookup_cost to account of the time
to find and check the next key value against the container
- Introduced ha_keyread_time(rows) that takes into account finding
the next row and copying the key value to 'record'
(KEY_COPY_COST).
- Introduced ha_key_scan_time() for calculating an index scan over
all rows.
- Added IDX_LOOKUP_COST to keyread_time() as a startup cost.
- Added index_only_fetch_cost() as a convenience function to
OPT_RANGE.
- keyread_time() cost is slightly reduced to prefer shorter keys.
(main.index_merge_myisam)
- All of the above caused some index_merge combinations to be
rejected because of cost (main.index_intersect). In some cases
'ref' where replaced with index_merge because of the low
cost calculation of get_sweep_read_cost().
- Some index usage moved from PRIMARY to a covering index.
(main.subselect_innodb)
- Changed cost calculation of filter to take KEY_LOOKUP_COST and
TIME_FOR_COMPARE into account. See sql_select.cc::apply_filter().
filter parameters and costs are now written to optimizer_trace.
- Don't use matchings_records_in_range() to try to estimate the number
of filtered rows for ranges. The reason is that we want to ensure
that 'range' is calculated similar to 'ref'. There is also more work
needed to calculate the selectivity when using ranges and ranges and
filtering. This causes filtering column in EXPLAIN EXTENDED to be
100.00 for some cases where range cannot use filtering.
(main.rowid_filter)
- Introduced ha_scan_time() that takes into account the CPU cost of
finding the next row and copying the row from the engine to
'record'. This causes costs of table scan to slightly increase and
some test to changed their plan from ALL to RANGE or ALL to ref.
(innodb.innodb_mysql, main.select_pkeycache)
In a few cases where scan time of very small tables have lower cost
than a ref or range, things changed from ref/range to ALL.
(main.myisam, main.func_group, main.limit_rows_examined,
main.subselect2)
- Introduced ha_scan_and_compare_time() which is like ha_scan_time()
but also adds the cost of the where clause (TIME_FOR_COMPARE).
- Added small cost for creating temporary table for
materialization. This causes some very small tables to use scan
instead of materialization.
- Added checking of the WHERE clause (TIME_FOR_COMPARE) of the
accepted rows to ROR costs in get_best_ror_intersect()
- Removed '- 0.001' from 'join->best_read' and optimize_straight_join()
to ensure that the 'Last_query_cost' status variable contains the
same value as the one that was calculated by the optimizer.
- Take avg_io_cost() into account in handler::keyread_time() and
handler::read_time(). This should have no effect as it's 1.0 by
default, except for heap that overrides these functions.
- Some 'ref_or_null' accesses changed to 'range' because of cost
adjustments (main.order_by)
- Added scan type "scan_with_join_cache" for optimizer_trace. This is
just to show in the trace what kind of scan was used.
- When using 'scan_with_join_cache' take into account number of
preceding tables (as have to restore all fields for all previous
table combination when checking the where clause)
The new cost added is:
(row_combinations * ROW_COPY_COST * number_of_cached_tables).
This increases the cost of join buffering in proportion of the
number of tables in the join buffer. One effect is that full scans
are now done earlier as the cost is then smaller.
(main.join_outer_innodb, main.greedy_optimizer)
- Removed the usage of 'worst_seeks' in cost_for_index_read as it
caused wrong plans to be created; It prefered JT_EQ_REF even if it
would be much more expensive than a full table scan. A related
issue was that worst_seeks only applied to full lookup, not to
clustered or index only lookups, which is not consistent. This
caused some plans to use index scan instead of eq_ref (main.union)
- Changed federated block size from 4096 to 1500, which is the
typical size of an IO packet.
- Added costs for reading rows to Federated. Needed as there is no
caching of rows in the federated engine.
- Added ha_innobase::rnd_pos_time() cost function.
- A lot of extra things added to optimizer trace
- More costs, especially for materialization and index_merge.
- Make lables more uniform
- Fixed a lot of minor bugs
- Added 'trace_started()' around a lot of trace blocks.
- When calculating ORDER BY with LIMIT cost for using an index
the cost did not take into account the number of row retrivals
that has to be done or the cost of comparing the rows with the
WHERE clause. The cost calculated would be just a fraction of
the real cost. Now we calculate the cost as we do for ranges
and 'ref'.
- 'Using index for group-by' is used a bit more than before as
now take into account the WHERE clause cost when comparing
with 'ref' and prefer the method with fewer row combinations.
(main.group_min_max).
Bugs fixed:
- Fixed that we don't calculate TIME_FOR_COMPARE twice for some plans,
like in optimize_straight_join() and greedy_search()
- Fixed bug in save_explain_data where we could test for the wrong
index when displaying 'Using index'. This caused some old plans to
show 'Using index'. (main.subselect_innodb, main.subselect2)
- Fixed bug in get_best_ror_intersect() where 'min_cost' was not
updated, and the cost we compared with was not the one that was
used.
- Fixed very wrong cost calculation for priority queues in
check_if_pq_applicable(). (main.order_by now correctly uses priority
queue)
- When calculating cost of EQ_REF or REF, we added the cost of
comparing the WHERE clause with the found rows, not all row
combinations. This made ref and eq_ref to be regarded way to cheap
compared to other access methods.
- FORCE INDEX cost calculation didn't take into account clustered or
covered indexes.
- JT_EQ_REF cost was estimated as avg_io_cost(), which is half the
cost of a JT_REF key. This may be true for InnoDB primary key, but
not for other unique keys or other engines. Now we use handler
function to calculate the cost, which allows us to handle
consistently clustered, covered keys and not covered keys.
- ha_start_keyread() didn't call extra_opt() if keyread was already
enabled but still changed the 'keyread' variable (which is wrong).
Fixed by not doing anything if keyread is already enabled.
- multi_range_read_info_cost() didn't take into account io_cost when
calculating the cost of ranges.
- fix_semijoin_strategies_for_picked_join_order() used the wrong
record_count when calling best_access_path() for SJ_OPT_FIRST_MATCH
and SJ_OPT_LOOSE_SCAN.
- Hash joins didn't provide correct best_cost to the upper level, which
means that the cost for hash_joins more expensive than calculated
in best_access_path (a difference of 10x * TIME_OF_COMPARE).
This is fixed in the new code thanks to that we now include
TIME_OF_COMPARE cost in 'read_time'.
Other things:
- Added some 'if (thd->trace_started())' to speed up code
- Removed not used function Cost_estimate::is_zero()
- Simplified testing of HA_POS_ERROR in get_best_ror_intersect().
(No cost changes)
- Moved ha_start_keyread() from join_read_const_table() to join_read_const()
to enable keyread for all types of JT_CONST tables.
- Made a few very short functions inline in handler.h
Notes:
- In main.rowid_filter the join order of order and lineitem is swapped.
This is because the cost of doing a range fetch of lineitem(98 rows) is
almost as big as the whole join of order,lineitem. The filtering will
also ensure that we only have to do very small key fetches of the rows
in lineitem.
- main.index_merge_myisam had a few changes where we are now using
less keys for index_merge. This is because index scans are now more
expensive than before.
- handler->optimizer_cache_cost is updated in ha_external_lock().
This ensures that it is up to date per statements.
Not an optimal solution (for locked tables), but should be ok for now.
- 'DELETE FROM t1 WHERE t1.a > 0 ORDER BY t1.a' does not take cost of
filesort into consideration when table scan is chosen.
(main.myisam_explain_non_select_all)
- perfschema.table_aggregate_global_* has changed because an update
on a table with 1 row will now use table scan instead of key lookup.
TODO in upcomming commits:
- Fix selectivity calculation for ranges with and without filtering and
when there is a ref access but scan is chosen.
For this we have to store the lowest known value for
'accepted_records' in the OPT_RANGE structure.
- Change that records_read does not include filtered rows.
- test_if_cheaper_ordering() needs to be updated to properly calculate
costs. This will fix tests like main.order_by_innodb,
main.single_delete_update
- Extend get_range_limit_read_cost() to take into considering
cost_for_index_read() if there where no quick keys. This will reduce
the computed cost for ORDER BY with LIMIT in some cases.
(main.innodb_ext_key)
- Fix that we take into account selectivity when counting the number
of rows we have to read when considering using a index table scan to
resolve ORDER BY.
- Add new calculation for rnd_pos_time() where we take into account the
benefit of reading multiple rows from the same page.
2021-11-01 12:34:24 +02:00
|
|
|
/* Clustering key */
|
2022-09-30 17:10:37 +03:00
|
|
|
cost->index_cost= ha_keyread_clustered_time(keyno, n_ranges, total_rows,
|
|
|
|
0);
|
Changing all cost calculation to be given in milliseconds
This makes it easier to compare different costs and also allows
the optimizer to optimizer different storage engines more reliably.
- Added tests/check_costs.pl, a tool to verify optimizer cost calculations.
- Most engine costs has been found with this program. All steps to
calculate the new costs are documented in Docs/optimizer_costs.txt
- User optimizer_cost variables are given in microseconds (as individual
costs can be very small). Internally they are stored in ms.
- Changed DISK_READ_COST (was DISK_SEEK_BASE_COST) from a hard disk cost
(9 ms) to common SSD cost (400MB/sec).
- Removed cost calculations for hard disks (rotation etc).
- Changed the following handler functions to return IO_AND_CPU_COST.
This makes it easy to apply different cost modifiers in ha_..time()
functions for io and cpu costs.
- scan_time()
- rnd_pos_time() & rnd_pos_call_time()
- keyread_time()
- Enhanched keyread_time() to calculate the full cost of reading of a set
of keys with a given number of ranges and optional number of blocks that
need to be accessed.
- Removed read_time() as keyread_time() + rnd_pos_time() can do the same
thing and more.
- Tuned cost for: heap, myisam, Aria, InnoDB, archive and MyRocks.
Used heap table costs for json_table. The rest are using default engine
costs.
- Added the following new optimizer variables:
- optimizer_disk_read_ratio
- optimizer_disk_read_cost
- optimizer_key_lookup_cost
- optimizer_row_lookup_cost
- optimizer_row_next_find_cost
- optimizer_scan_cost
- Moved all engine specific cost to OPTIMIZER_COSTS structure.
- Changed costs to use 'records_out' instead of 'records_read' when
recalculating costs.
- Split optimizer_costs.h to optimizer_costs.h and optimizer_defaults.h.
This allows one to change costs without having to compile a lot of
files.
- Updated costs for filter lookup.
- Use a better cost estimate in best_extension_by_limited_search()
for the sorting cost.
- Fixed previous issues with 'filtered' explain column as we are now
using 'records_out' (min rows seen for table) to calculate filtering.
This greatly simplifies the filtering code in
JOIN_TAB::save_explain_data().
This change caused a lot of queries to be optimized differently than
before, which exposed different issues in the optimizer that needs to
be fixed. These fixes are in the following commits. To not have to
change the same test case over and over again, the changes in the test
cases are done in a single commit after all the critical change sets
are done.
InnoDB changes:
- Updated InnoDB to not divide big range cost with 2.
- Added cost for InnoDB (innobase_update_optimizer_costs()).
- Don't mark clustered primary key with HA_KEYREAD_ONLY. This will
prevent that the optimizer is trying to use index-only scans on
the clustered key.
- Disabled ha_innobase::scan_time() and ha_innobase::read_time() and
ha_innobase::rnd_pos_time() as the default engine cost functions now
works good for InnoDB.
Other things:
- Added --show-query-costs (\Q) option to mysql.cc to show the query
cost after each query (good when working with query costs).
- Extended my_getopt with GET_ADJUSTED_VALUE which allows one to adjust
the value that user is given. This is used to change cost from
microseconds (user input) to milliseconds (what the server is
internally using).
- Added include/my_tracker.h ; Useful include file to quickly test
costs of a function.
- Use handler::set_table() in all places instead of 'table= arg'.
- Added SHOW_OPTIMIZER_COSTS to sys variables. These are input and
shown in microseconds for the user but stored as milliseconds.
This is to make the numbers easier to read for the user (less
pre-zeros). Implemented in 'Sys_var_optimizer_cost' class.
- In test_quick_select() do not use index scans if 'no_keyread' is set
for the table. This is what we do in other places of the server.
- Added THD parameter to Unique::get_use_cost() and
check_index_intersect_extension() and similar functions to be able
to provide costs to called functions.
- Changed 'records' to 'rows' in optimizer_trace.
- Write more information to optimizer_trace.
- Added INDEX_BLOCK_FILL_FACTOR_MUL (4) and INDEX_BLOCK_FILL_FACTOR_DIV (3)
to calculate usage space of keys in b-trees. (Before we used numeric
constants).
- Removed code that assumed that b-trees has similar costs as binary
trees. Replaced with engine calls that returns the cost.
- Added Bitmap::find_first_bit()
- Added timings to join_cache for ANALYZE table (patch by Sergei Petrunia).
- Added records_init and records_after_filter to POSITION to remember
more of what best_access_patch() calculates.
- table_after_join_selectivity() changed to recalculate 'records_out'
based on the new fields from best_access_patch()
Bug fixes:
- Some queries did not update last_query_cost (was 0). Fixed by moving
setting thd->...last_query_cost in JOIN::optimize().
- Write '0' as number of rows for const tables with a matching row.
Some internals:
- Engine cost are stored in OPTIMIZER_COSTS structure. When a
handlerton is created, we also created a new cost variable for the
handlerton. We also create a new variable if the user changes a
optimizer cost for a not yet loaded handlerton either with command
line arguments or with SET
@@global.engine.optimizer_cost_variable=xx.
- There are 3 global OPTIMIZER_COSTS variables:
default_optimizer_costs The default costs + changes from the
command line without an engine specifier.
heap_optimizer_costs Heap table costs, used for temporary tables
tmp_table_optimizer_costs The cost for the default on disk internal
temporary table (MyISAM or Aria)
- The engine cost for a table is stored in table_share. To speed up
accesses the handler has a pointer to this. The cost is copied
to the table on first access. If one wants to change the cost one
must first update the global engine cost and then do a FLUSH TABLES.
This was done to be able to access the costs for an open table
without any locks.
- When a handlerton is created, the cost are updated the following way:
See sql/keycaches.cc for details:
- Use 'default_optimizer_costs' as a base
- Call hton->update_optimizer_costs() to override with the engines
default costs.
- Override the costs that the user has specified for the engine.
- One handler open, copy the engine cost from handlerton to TABLE_SHARE.
- Call handler::update_optimizer_costs() to allow the engine to update
cost for this particular table.
- There are two costs stored in THD. These are copied to the handler
when the table is used in a query:
- optimizer_where_cost
- optimizer_scan_setup_cost
- Simply code in best_access_path() by storing all cost result in a
structure. (Idea/Suggestion by Igor)
2022-08-11 13:05:23 +03:00
|
|
|
cost->copy_cost= rows2double(total_rows) * ROW_COPY_COST;
|
2019-02-03 14:56:12 -08:00
|
|
|
}
|
Changing all cost calculation to be given in milliseconds
This makes it easier to compare different costs and also allows
the optimizer to optimizer different storage engines more reliably.
- Added tests/check_costs.pl, a tool to verify optimizer cost calculations.
- Most engine costs has been found with this program. All steps to
calculate the new costs are documented in Docs/optimizer_costs.txt
- User optimizer_cost variables are given in microseconds (as individual
costs can be very small). Internally they are stored in ms.
- Changed DISK_READ_COST (was DISK_SEEK_BASE_COST) from a hard disk cost
(9 ms) to common SSD cost (400MB/sec).
- Removed cost calculations for hard disks (rotation etc).
- Changed the following handler functions to return IO_AND_CPU_COST.
This makes it easy to apply different cost modifiers in ha_..time()
functions for io and cpu costs.
- scan_time()
- rnd_pos_time() & rnd_pos_call_time()
- keyread_time()
- Enhanched keyread_time() to calculate the full cost of reading of a set
of keys with a given number of ranges and optional number of blocks that
need to be accessed.
- Removed read_time() as keyread_time() + rnd_pos_time() can do the same
thing and more.
- Tuned cost for: heap, myisam, Aria, InnoDB, archive and MyRocks.
Used heap table costs for json_table. The rest are using default engine
costs.
- Added the following new optimizer variables:
- optimizer_disk_read_ratio
- optimizer_disk_read_cost
- optimizer_key_lookup_cost
- optimizer_row_lookup_cost
- optimizer_row_next_find_cost
- optimizer_scan_cost
- Moved all engine specific cost to OPTIMIZER_COSTS structure.
- Changed costs to use 'records_out' instead of 'records_read' when
recalculating costs.
- Split optimizer_costs.h to optimizer_costs.h and optimizer_defaults.h.
This allows one to change costs without having to compile a lot of
files.
- Updated costs for filter lookup.
- Use a better cost estimate in best_extension_by_limited_search()
for the sorting cost.
- Fixed previous issues with 'filtered' explain column as we are now
using 'records_out' (min rows seen for table) to calculate filtering.
This greatly simplifies the filtering code in
JOIN_TAB::save_explain_data().
This change caused a lot of queries to be optimized differently than
before, which exposed different issues in the optimizer that needs to
be fixed. These fixes are in the following commits. To not have to
change the same test case over and over again, the changes in the test
cases are done in a single commit after all the critical change sets
are done.
InnoDB changes:
- Updated InnoDB to not divide big range cost with 2.
- Added cost for InnoDB (innobase_update_optimizer_costs()).
- Don't mark clustered primary key with HA_KEYREAD_ONLY. This will
prevent that the optimizer is trying to use index-only scans on
the clustered key.
- Disabled ha_innobase::scan_time() and ha_innobase::read_time() and
ha_innobase::rnd_pos_time() as the default engine cost functions now
works good for InnoDB.
Other things:
- Added --show-query-costs (\Q) option to mysql.cc to show the query
cost after each query (good when working with query costs).
- Extended my_getopt with GET_ADJUSTED_VALUE which allows one to adjust
the value that user is given. This is used to change cost from
microseconds (user input) to milliseconds (what the server is
internally using).
- Added include/my_tracker.h ; Useful include file to quickly test
costs of a function.
- Use handler::set_table() in all places instead of 'table= arg'.
- Added SHOW_OPTIMIZER_COSTS to sys variables. These are input and
shown in microseconds for the user but stored as milliseconds.
This is to make the numbers easier to read for the user (less
pre-zeros). Implemented in 'Sys_var_optimizer_cost' class.
- In test_quick_select() do not use index scans if 'no_keyread' is set
for the table. This is what we do in other places of the server.
- Added THD parameter to Unique::get_use_cost() and
check_index_intersect_extension() and similar functions to be able
to provide costs to called functions.
- Changed 'records' to 'rows' in optimizer_trace.
- Write more information to optimizer_trace.
- Added INDEX_BLOCK_FILL_FACTOR_MUL (4) and INDEX_BLOCK_FILL_FACTOR_DIV (3)
to calculate usage space of keys in b-trees. (Before we used numeric
constants).
- Removed code that assumed that b-trees has similar costs as binary
trees. Replaced with engine calls that returns the cost.
- Added Bitmap::find_first_bit()
- Added timings to join_cache for ANALYZE table (patch by Sergei Petrunia).
- Added records_init and records_after_filter to POSITION to remember
more of what best_access_patch() calculates.
- table_after_join_selectivity() changed to recalculate 'records_out'
based on the new fields from best_access_patch()
Bug fixes:
- Some queries did not update last_query_cost (was 0). Fixed by moving
setting thd->...last_query_cost in JOIN::optimize().
- Write '0' as number of rows for const tables with a matching row.
Some internals:
- Engine cost are stored in OPTIMIZER_COSTS structure. When a
handlerton is created, we also created a new cost variable for the
handlerton. We also create a new variable if the user changes a
optimizer cost for a not yet loaded handlerton either with command
line arguments or with SET
@@global.engine.optimizer_cost_variable=xx.
- There are 3 global OPTIMIZER_COSTS variables:
default_optimizer_costs The default costs + changes from the
command line without an engine specifier.
heap_optimizer_costs Heap table costs, used for temporary tables
tmp_table_optimizer_costs The cost for the default on disk internal
temporary table (MyISAM or Aria)
- The engine cost for a table is stored in table_share. To speed up
accesses the handler has a pointer to this. The cost is copied
to the table on first access. If one wants to change the cost one
must first update the global engine cost and then do a FLUSH TABLES.
This was done to be able to access the costs for an open table
without any locks.
- When a handlerton is created, the cost are updated the following way:
See sql/keycaches.cc for details:
- Use 'default_optimizer_costs' as a base
- Call hton->update_optimizer_costs() to override with the engines
default costs.
- Override the costs that the user has specified for the engine.
- One handler open, copy the engine cost from handlerton to TABLE_SHARE.
- Call handler::update_optimizer_costs() to allow the engine to update
cost for this particular table.
- There are two costs stored in THD. These are copied to the handler
when the table is used in a query:
- optimizer_where_cost
- optimizer_scan_setup_cost
- Simply code in best_access_path() by storing all cost result in a
structure. (Idea/Suggestion by Igor)
2022-08-11 13:05:23 +03:00
|
|
|
cost->comp_cost= rows2double(total_rows) * WHERE_COST;
|
2009-12-22 15:33:21 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
Initialize the MRR scan
|
|
|
|
|
|
|
|
Initialize the MRR scan. This function may do heavyweight scan
|
|
|
|
initialization like row prefetching/sorting/etc (NOTE: but better not do
|
|
|
|
it here as we may not need it, e.g. if we never satisfy WHERE clause on
|
|
|
|
previous tables. For many implementations it would be natural to do such
|
|
|
|
initializations in the first multi_read_range_next() call)
|
|
|
|
|
|
|
|
mode is a combination of the following flags: HA_MRR_SORTED,
|
|
|
|
HA_MRR_INDEX_ONLY, HA_MRR_NO_ASSOCIATION
|
|
|
|
|
|
|
|
@param seq Range sequence to be traversed
|
|
|
|
@param seq_init_param First parameter for seq->init()
|
|
|
|
@param n_ranges Number of ranges in the sequence
|
|
|
|
@param mode Flags, see the description section for the details
|
|
|
|
@param buf INOUT: memory buffer to be used
|
|
|
|
|
|
|
|
@note
|
|
|
|
One must have called index_init() before calling this function. Several
|
|
|
|
multi_range_read_init() calls may be made in course of one query.
|
|
|
|
|
|
|
|
Buffer memory management is done according to the following scenario:
|
|
|
|
The caller allocates the buffer and provides it to the callee by filling
|
|
|
|
the members of HANDLER_BUFFER structure.
|
|
|
|
The callee consumes all or some fraction of the provided buffer space, and
|
|
|
|
sets the HANDLER_BUFFER members accordingly.
|
|
|
|
The callee may use the buffer memory until the next multi_range_read_init()
|
|
|
|
call is made, all records have been read, or until index_end() call is
|
|
|
|
made, whichever comes first.
|
|
|
|
|
|
|
|
@retval 0 OK
|
|
|
|
@retval 1 Error
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
handler::multi_range_read_init(RANGE_SEQ_IF *seq_funcs, void *seq_init_param,
|
|
|
|
uint n_ranges, uint mode, HANDLER_BUFFER *buf)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("handler::multi_range_read_init");
|
|
|
|
mrr_iter= seq_funcs->init(seq_init_param, n_ranges, mode);
|
|
|
|
mrr_funcs= *seq_funcs;
|
2014-02-19 14:05:15 +04:00
|
|
|
mrr_is_output_sorted= MY_TEST(mode & HA_MRR_SORTED);
|
2009-12-22 15:33:21 +03:00
|
|
|
mrr_have_range= FALSE;
|
2009-12-15 10:16:46 +03:00
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-22 15:33:21 +03:00
|
|
|
Get next record in MRR scan
|
|
|
|
|
|
|
|
Default MRR implementation: read the next record
|
|
|
|
|
|
|
|
@param range_info OUT Undefined if HA_MRR_NO_ASSOCIATION flag is in effect
|
|
|
|
Otherwise, the opaque value associated with the range
|
|
|
|
that contains the returned record.
|
|
|
|
|
|
|
|
@retval 0 OK
|
|
|
|
@retval other Error code
|
2009-12-15 10:16:46 +03:00
|
|
|
*/
|
|
|
|
|
2011-03-04 12:06:03 +03:00
|
|
|
int handler::multi_range_read_next(range_id_t *range_info)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2010-10-06 13:27:12 -07:00
|
|
|
int result= HA_ERR_END_OF_FILE;
|
2010-12-17 13:06:21 +03:00
|
|
|
bool range_res;
|
2009-12-22 15:33:21 +03:00
|
|
|
DBUG_ENTER("handler::multi_range_read_next");
|
|
|
|
|
|
|
|
if (!mrr_have_range)
|
|
|
|
{
|
|
|
|
mrr_have_range= TRUE;
|
|
|
|
goto start;
|
|
|
|
}
|
2009-12-15 10:16:46 +03:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2009-12-22 15:33:21 +03:00
|
|
|
/* Save a call if there can be only one row in range. */
|
|
|
|
if (mrr_cur_range.range_flag != (UNIQUE_RANGE | EQ_RANGE))
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2009-12-22 15:33:21 +03:00
|
|
|
result= read_range_next();
|
|
|
|
/* On success or non-EOF errors jump to the end. */
|
|
|
|
if (result != HA_ERR_END_OF_FILE)
|
|
|
|
break;
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
2009-12-22 15:33:21 +03:00
|
|
|
else
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2016-08-21 20:18:39 +03:00
|
|
|
if (ha_was_semi_consistent_read())
|
2010-12-17 14:58:08 +03:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
The following assignment is redundant, but for extra safety and to
|
|
|
|
remove the compiler warning:
|
|
|
|
*/
|
|
|
|
range_res= FALSE;
|
2009-12-22 15:33:21 +03:00
|
|
|
goto scan_it_again;
|
2010-12-17 14:58:08 +03:00
|
|
|
}
|
2009-12-22 15:33:21 +03:00
|
|
|
/*
|
|
|
|
We need to set this for the last range only, but checking this
|
|
|
|
condition is more expensive than just setting the result code.
|
|
|
|
*/
|
|
|
|
result= HA_ERR_END_OF_FILE;
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
start:
|
|
|
|
/* Try the next range(s) until one matches a record. */
|
|
|
|
while (!(range_res= mrr_funcs.next(mrr_iter, &mrr_cur_range)))
|
|
|
|
{
|
|
|
|
scan_it_again:
|
|
|
|
result= read_range_first(mrr_cur_range.start_key.keypart_map ?
|
|
|
|
&mrr_cur_range.start_key : 0,
|
|
|
|
mrr_cur_range.end_key.keypart_map ?
|
|
|
|
&mrr_cur_range.end_key : 0,
|
2014-02-19 14:05:15 +04:00
|
|
|
MY_TEST(mrr_cur_range.range_flag & EQ_RANGE),
|
2009-12-22 15:33:21 +03:00
|
|
|
mrr_is_output_sorted);
|
|
|
|
if (result != HA_ERR_END_OF_FILE)
|
|
|
|
break;
|
|
|
|
}
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
2009-12-22 15:33:21 +03:00
|
|
|
while ((result == HA_ERR_END_OF_FILE) && !range_res);
|
|
|
|
|
|
|
|
*range_info= mrr_cur_range.ptr;
|
|
|
|
DBUG_PRINT("exit",("handler::multi_range_read_next result %d", result));
|
|
|
|
DBUG_RETURN(result);
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
/****************************************************************************
|
2010-11-03 00:09:28 +03:00
|
|
|
* Mrr_*_reader classes (building blocks for DS-MRR)
|
|
|
|
***************************************************************************/
|
2010-09-10 20:48:11 +04:00
|
|
|
|
2010-11-03 00:09:28 +03:00
|
|
|
int Mrr_simple_index_reader::init(handler *h_arg, RANGE_SEQ_IF *seq_funcs,
|
|
|
|
void *seq_init_param, uint n_ranges,
|
2010-12-02 14:10:52 +03:00
|
|
|
uint mode, Key_parameters *key_par_arg,
|
|
|
|
Lifo_buffer *key_buffer_arg,
|
|
|
|
Buffer_manager *buf_manager_arg)
|
2010-11-03 00:09:28 +03:00
|
|
|
{
|
|
|
|
HANDLER_BUFFER no_buffer = {NULL, NULL, NULL};
|
2010-12-02 16:21:52 +03:00
|
|
|
file= h_arg;
|
|
|
|
return file->handler::multi_range_read_init(seq_funcs, seq_init_param,
|
|
|
|
n_ranges, mode, &no_buffer);
|
2010-11-03 00:09:28 +03:00
|
|
|
}
|
2010-10-26 15:35:13 +04:00
|
|
|
|
2010-11-08 15:15:50 +03:00
|
|
|
|
2011-03-04 12:06:03 +03:00
|
|
|
int Mrr_simple_index_reader::get_next(range_id_t *range_info)
|
2010-10-26 15:35:13 +04:00
|
|
|
{
|
2010-11-08 15:15:50 +03:00
|
|
|
int res;
|
2010-12-02 16:21:52 +03:00
|
|
|
while (!(res= file->handler::multi_range_read_next(range_info)))
|
2010-10-26 15:35:13 +04:00
|
|
|
{
|
2010-12-02 16:21:52 +03:00
|
|
|
KEY_MULTI_RANGE *curr_range= &file->handler::mrr_cur_range;
|
|
|
|
if (!file->mrr_funcs.skip_index_tuple ||
|
|
|
|
!file->mrr_funcs.skip_index_tuple(file->mrr_iter, curr_range->ptr))
|
2010-10-26 15:35:13 +04:00
|
|
|
break;
|
|
|
|
}
|
2011-02-18 21:45:32 +02:00
|
|
|
if (res && res != HA_ERR_END_OF_FILE && res != HA_ERR_KEY_NOT_FOUND)
|
|
|
|
file->print_error(res, MYF(0)); // Fatal error
|
2010-10-26 15:35:13 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2010-11-03 00:09:28 +03:00
|
|
|
@brief Get next index record
|
|
|
|
|
|
|
|
@param range_info OUT identifier of range that the returned record belongs to
|
2010-10-26 15:35:13 +04:00
|
|
|
|
|
|
|
@note
|
|
|
|
We actually iterate over nested sequences:
|
2010-11-03 00:09:28 +03:00
|
|
|
- an ordered sequence of groups of identical keys
|
|
|
|
- each key group has key value, which has multiple matching records
|
|
|
|
- thus, each record matches all members of the key group
|
2010-10-26 15:35:13 +04:00
|
|
|
|
|
|
|
@retval 0 OK, next record was successfully read
|
|
|
|
@retval HA_ERR_END_OF_FILE End of records
|
2011-02-18 21:45:32 +02:00
|
|
|
@retval Other Some other error; Error is printed
|
2010-10-26 15:35:13 +04:00
|
|
|
*/
|
|
|
|
|
2011-03-04 12:06:03 +03:00
|
|
|
int Mrr_ordered_index_reader::get_next(range_id_t *range_info)
|
2010-10-26 15:35:13 +04:00
|
|
|
{
|
2010-11-08 15:15:50 +03:00
|
|
|
int res;
|
2010-10-26 15:35:13 +04:00
|
|
|
DBUG_ENTER("Mrr_ordered_index_reader::get_next");
|
2010-11-01 13:52:10 +03:00
|
|
|
|
2010-11-22 17:08:22 +03:00
|
|
|
for(;;)
|
2010-10-26 15:35:13 +04:00
|
|
|
{
|
2010-12-02 14:10:52 +03:00
|
|
|
if (!scanning_key_val_iter)
|
2010-10-26 15:35:13 +04:00
|
|
|
{
|
2010-11-08 15:15:50 +03:00
|
|
|
while ((res= kv_it.init(this)))
|
2010-10-26 15:35:13 +04:00
|
|
|
{
|
2010-11-22 19:34:03 +03:00
|
|
|
if ((res != HA_ERR_KEY_NOT_FOUND && res != HA_ERR_END_OF_FILE))
|
|
|
|
DBUG_RETURN(res); /* Some fatal error */
|
2011-02-18 21:45:32 +02:00
|
|
|
|
2010-12-02 14:10:52 +03:00
|
|
|
if (key_buffer->is_empty())
|
2010-10-26 15:35:13 +04:00
|
|
|
{
|
2010-11-22 19:34:03 +03:00
|
|
|
DBUG_RETURN(HA_ERR_END_OF_FILE);
|
2010-10-26 15:35:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
scanning_key_val_iter= TRUE;
|
2010-12-02 14:10:52 +03:00
|
|
|
}
|
|
|
|
|
2010-12-16 21:18:35 +03:00
|
|
|
if ((res= kv_it.get_next(range_info)))
|
2010-12-02 14:10:52 +03:00
|
|
|
{
|
|
|
|
scanning_key_val_iter= FALSE;
|
|
|
|
if ((res != HA_ERR_KEY_NOT_FOUND && res != HA_ERR_END_OF_FILE))
|
|
|
|
DBUG_RETURN(res);
|
|
|
|
kv_it.move_to_next_key_value();
|
2010-11-23 18:08:11 +03:00
|
|
|
continue;
|
2010-10-26 15:35:13 +04:00
|
|
|
}
|
2010-12-16 21:18:35 +03:00
|
|
|
if (!skip_index_tuple(*range_info) &&
|
|
|
|
!skip_record(*range_info, NULL))
|
2010-10-26 15:35:13 +04:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Go get another (record, range_id) combination */
|
|
|
|
} /* while */
|
|
|
|
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
2010-12-19 13:56:12 +03:00
|
|
|
|
|
|
|
/*
|
2010-12-20 14:40:12 +03:00
|
|
|
Supply index reader with the O(1)space it needs for scan interrupt/restore
|
|
|
|
operation
|
2010-12-19 13:56:12 +03:00
|
|
|
*/
|
|
|
|
|
2010-12-20 14:40:12 +03:00
|
|
|
bool Mrr_ordered_index_reader::set_interruption_temp_buffer(uint rowid_length,
|
|
|
|
uint key_len,
|
2011-03-04 00:54:10 +03:00
|
|
|
uint saved_pk_len,
|
2010-12-20 14:40:12 +03:00
|
|
|
uchar **space_start,
|
|
|
|
uchar *space_end)
|
2010-12-09 00:47:33 +03:00
|
|
|
{
|
2011-03-04 00:54:10 +03:00
|
|
|
if (space_end - *space_start <= (ptrdiff_t)(rowid_length + key_len + saved_pk_len))
|
2010-12-19 13:56:12 +03:00
|
|
|
return TRUE;
|
2010-12-20 14:40:12 +03:00
|
|
|
support_scan_interruptions= TRUE;
|
|
|
|
|
2010-12-19 13:56:12 +03:00
|
|
|
saved_rowid= *space_start;
|
|
|
|
*space_start += rowid_length;
|
2011-03-04 00:54:10 +03:00
|
|
|
|
|
|
|
if (saved_pk_len)
|
|
|
|
{
|
|
|
|
saved_primary_key= *space_start;
|
|
|
|
*space_start += saved_pk_len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
saved_primary_key= NULL;
|
|
|
|
|
2010-12-19 13:56:12 +03:00
|
|
|
saved_key_tuple= *space_start;
|
|
|
|
*space_start += key_len;
|
|
|
|
|
2010-12-09 00:47:33 +03:00
|
|
|
have_saved_rowid= FALSE;
|
2014-10-16 17:57:13 +04:00
|
|
|
read_was_interrupted= FALSE;
|
2010-12-19 13:56:12 +03:00
|
|
|
return FALSE;
|
2010-12-09 00:47:33 +03:00
|
|
|
}
|
|
|
|
|
2010-12-20 14:40:12 +03:00
|
|
|
void Mrr_ordered_index_reader::set_no_interruption_temp_buffer()
|
|
|
|
{
|
|
|
|
support_scan_interruptions= FALSE;
|
2011-03-04 00:54:10 +03:00
|
|
|
saved_key_tuple= saved_rowid= saved_primary_key= NULL; /* safety */
|
2010-12-20 14:40:12 +03:00
|
|
|
have_saved_rowid= FALSE;
|
2014-10-16 17:57:13 +04:00
|
|
|
read_was_interrupted= FALSE;
|
2010-12-20 14:40:12 +03:00
|
|
|
}
|
|
|
|
|
2010-12-09 00:47:33 +03:00
|
|
|
void Mrr_ordered_index_reader::interrupt_read()
|
|
|
|
{
|
2010-12-20 14:40:12 +03:00
|
|
|
DBUG_ASSERT(support_scan_interruptions);
|
2011-03-04 00:54:10 +03:00
|
|
|
TABLE *table= file->get_table();
|
2011-09-13 23:45:02 +04:00
|
|
|
KEY *used_index= &table->key_info[file->active_index];
|
2010-12-19 13:56:12 +03:00
|
|
|
/* Save the current key value */
|
2011-03-04 00:54:10 +03:00
|
|
|
key_copy(saved_key_tuple, table->record[0],
|
2011-09-13 23:45:02 +04:00
|
|
|
used_index, used_index->key_length);
|
2011-03-04 00:54:10 +03:00
|
|
|
|
|
|
|
if (saved_primary_key)
|
|
|
|
{
|
|
|
|
key_copy(saved_primary_key, table->record[0],
|
|
|
|
&table->key_info[table->s->primary_key],
|
|
|
|
table->key_info[table->s->primary_key].key_length);
|
|
|
|
}
|
2014-10-16 17:57:13 +04:00
|
|
|
read_was_interrupted= TRUE;
|
2010-12-19 13:56:12 +03:00
|
|
|
|
2010-12-09 00:47:33 +03:00
|
|
|
/* Save the last rowid */
|
|
|
|
memcpy(saved_rowid, file->ref, file->ref_length);
|
|
|
|
have_saved_rowid= TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mrr_ordered_index_reader::position()
|
|
|
|
{
|
|
|
|
if (have_saved_rowid)
|
|
|
|
memcpy(file->ref, saved_rowid, file->ref_length);
|
|
|
|
else
|
|
|
|
Mrr_index_reader::position();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mrr_ordered_index_reader::resume_read()
|
|
|
|
{
|
2011-03-04 00:54:10 +03:00
|
|
|
TABLE *table= file->get_table();
|
2014-10-29 01:37:58 +03:00
|
|
|
|
|
|
|
if (!read_was_interrupted)
|
|
|
|
return;
|
|
|
|
|
2011-09-13 23:45:02 +04:00
|
|
|
KEY *used_index= &table->key_info[file->active_index];
|
2011-03-04 00:54:10 +03:00
|
|
|
key_restore(table->record[0], saved_key_tuple,
|
2011-09-13 23:45:02 +04:00
|
|
|
used_index, used_index->key_length);
|
2011-03-04 00:54:10 +03:00
|
|
|
if (saved_primary_key)
|
|
|
|
{
|
|
|
|
key_restore(table->record[0], saved_primary_key,
|
|
|
|
&table->key_info[table->s->primary_key],
|
|
|
|
table->key_info[table->s->primary_key].key_length);
|
|
|
|
}
|
2010-12-09 00:47:33 +03:00
|
|
|
}
|
|
|
|
|
2010-10-26 15:35:13 +04:00
|
|
|
|
|
|
|
/**
|
2010-11-03 00:09:28 +03:00
|
|
|
Fill the buffer with (lookup_tuple, range_id) pairs and sort
|
2012-01-23 23:35:52 +04:00
|
|
|
|
|
|
|
@return
|
|
|
|
0 OK, the buffer is non-empty and sorted
|
|
|
|
HA_ERR_END_OF_FILE Source exhausted, the buffer is empty.
|
2010-10-26 15:35:13 +04:00
|
|
|
*/
|
|
|
|
|
2010-11-25 14:35:21 +03:00
|
|
|
int Mrr_ordered_index_reader::refill_buffer(bool initial)
|
2010-10-26 15:35:13 +04:00
|
|
|
{
|
|
|
|
KEY_MULTI_RANGE cur_range;
|
|
|
|
DBUG_ENTER("Mrr_ordered_index_reader::refill_buffer");
|
|
|
|
|
2010-12-02 14:10:52 +03:00
|
|
|
DBUG_ASSERT(key_buffer->is_empty());
|
2010-11-25 14:35:21 +03:00
|
|
|
|
|
|
|
if (source_exhausted)
|
|
|
|
DBUG_RETURN(HA_ERR_END_OF_FILE);
|
|
|
|
|
2010-12-13 20:01:32 +03:00
|
|
|
buf_manager->reset_buffer_sizes(buf_manager->arg);
|
|
|
|
key_buffer->reset();
|
2010-12-16 23:37:26 +03:00
|
|
|
key_buffer->setup_writing(keypar.key_size_in_keybuf,
|
2011-03-04 12:06:03 +03:00
|
|
|
is_mrr_assoc? sizeof(range_id_t) : 0);
|
2010-10-26 15:35:13 +04:00
|
|
|
|
2010-12-02 14:10:52 +03:00
|
|
|
while (key_buffer->can_write() &&
|
2010-12-13 20:01:32 +03:00
|
|
|
!(source_exhausted= mrr_funcs.next(mrr_iter, &cur_range)))
|
2010-10-26 15:35:13 +04:00
|
|
|
{
|
|
|
|
DBUG_ASSERT(cur_range.range_flag & EQ_RANGE);
|
|
|
|
|
|
|
|
/* Put key, or {key, range_id} pair into the buffer */
|
2010-12-16 23:37:26 +03:00
|
|
|
key_buffer->write_ptr1= keypar.use_key_pointers ?
|
|
|
|
(uchar*)&cur_range.start_key.key :
|
|
|
|
(uchar*)cur_range.start_key.key;
|
|
|
|
key_buffer->write_ptr2= (uchar*)&cur_range.ptr;
|
2010-10-26 15:35:13 +04:00
|
|
|
key_buffer->write();
|
|
|
|
}
|
2010-11-22 17:08:22 +03:00
|
|
|
|
|
|
|
/* Force get_next() to start with kv_it.init() call: */
|
2010-11-02 19:32:26 +03:00
|
|
|
scanning_key_val_iter= FALSE;
|
|
|
|
|
2010-12-02 14:10:52 +03:00
|
|
|
if (source_exhausted && key_buffer->is_empty())
|
2010-11-02 19:32:26 +03:00
|
|
|
DBUG_RETURN(HA_ERR_END_OF_FILE);
|
2010-10-26 15:35:13 +04:00
|
|
|
|
2012-01-23 23:35:52 +04:00
|
|
|
if (!initial)
|
|
|
|
{
|
|
|
|
/* This is a non-initial buffer fill and we've got a non-empty buffer */
|
|
|
|
THD *thd= current_thd;
|
2012-02-22 16:48:29 +04:00
|
|
|
status_var_increment(thd->status_var.ha_mrr_key_refills_count);
|
2012-01-23 23:35:52 +04:00
|
|
|
}
|
|
|
|
|
2010-10-26 15:35:13 +04:00
|
|
|
key_buffer->sort((key_buffer->type() == Lifo_buffer::FORWARD)?
|
2010-11-22 19:34:03 +03:00
|
|
|
(qsort2_cmp)Mrr_ordered_index_reader::compare_keys_reverse :
|
|
|
|
(qsort2_cmp)Mrr_ordered_index_reader::compare_keys,
|
2010-11-22 17:08:22 +03:00
|
|
|
this);
|
2010-11-02 19:32:26 +03:00
|
|
|
DBUG_RETURN(0);
|
2010-10-26 15:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Mrr_ordered_index_reader::init(handler *h_arg, RANGE_SEQ_IF *seq_funcs,
|
|
|
|
void *seq_init_param, uint n_ranges,
|
2010-12-02 14:10:52 +03:00
|
|
|
uint mode, Key_parameters *key_par_arg,
|
|
|
|
Lifo_buffer *key_buffer_arg,
|
|
|
|
Buffer_manager *buf_manager_arg)
|
2010-10-26 15:35:13 +04:00
|
|
|
{
|
2010-12-02 16:21:52 +03:00
|
|
|
file= h_arg;
|
2010-12-02 14:10:52 +03:00
|
|
|
key_buffer= key_buffer_arg;
|
|
|
|
buf_manager= buf_manager_arg;
|
|
|
|
keypar= *key_par_arg;
|
|
|
|
|
2010-12-02 16:21:52 +03:00
|
|
|
KEY *key_info= &file->get_table()->key_info[file->active_index];
|
2014-02-19 14:05:15 +04:00
|
|
|
keypar.index_ranges_unique= MY_TEST(key_info->flags & HA_NOSAME &&
|
|
|
|
key_info->user_defined_key_parts ==
|
|
|
|
my_count_bits(keypar.key_tuple_map));
|
2010-12-02 14:10:52 +03:00
|
|
|
|
2010-10-26 15:35:13 +04:00
|
|
|
mrr_iter= seq_funcs->init(seq_init_param, n_ranges, mode);
|
2014-02-19 14:05:15 +04:00
|
|
|
is_mrr_assoc= !MY_TEST(mode & HA_MRR_NO_ASSOCIATION);
|
2010-10-26 15:35:13 +04:00
|
|
|
mrr_funcs= *seq_funcs;
|
2010-11-25 14:35:21 +03:00
|
|
|
source_exhausted= FALSE;
|
2014-10-16 17:57:13 +04:00
|
|
|
read_was_interrupted= false;
|
2010-12-09 00:47:33 +03:00
|
|
|
have_saved_rowid= FALSE;
|
2010-10-26 15:35:13 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-02 16:21:52 +03:00
|
|
|
static int rowid_cmp_reverse(void *file, uchar *a, uchar *b)
|
2010-10-26 15:35:13 +04:00
|
|
|
{
|
2010-12-02 16:21:52 +03:00
|
|
|
return - ((handler*)file)->cmp_ref(a, b);
|
2010-10-26 15:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Mrr_ordered_rndpos_reader::init(handler *h_arg,
|
|
|
|
Mrr_index_reader *index_reader_arg,
|
|
|
|
uint mode,
|
2020-02-18 22:49:42 -08:00
|
|
|
Lifo_buffer *buf,
|
|
|
|
Rowid_filter *filter)
|
2010-10-26 15:35:13 +04:00
|
|
|
{
|
2010-12-02 16:21:52 +03:00
|
|
|
file= h_arg;
|
2010-10-26 15:35:13 +04:00
|
|
|
index_reader= index_reader_arg;
|
|
|
|
rowid_buffer= buf;
|
2014-02-19 14:05:15 +04:00
|
|
|
is_mrr_assoc= !MY_TEST(mode & HA_MRR_NO_ASSOCIATION);
|
2010-11-01 13:52:10 +03:00
|
|
|
index_reader_exhausted= FALSE;
|
2010-11-25 14:35:21 +03:00
|
|
|
index_reader_needs_refill= TRUE;
|
2020-02-18 22:49:42 -08:00
|
|
|
rowid_filter= filter;
|
2020-01-18 13:26:03 -08:00
|
|
|
|
2010-10-31 22:00:15 +03:00
|
|
|
return 0;
|
2010-10-26 15:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
DS-MRR: Fill and sort the rowid buffer
|
|
|
|
|
|
|
|
Scan the MRR ranges and collect ROWIDs (or {ROWID, range_id} pairs) into
|
|
|
|
buffer. When the buffer is full or scan is completed, sort the buffer by
|
|
|
|
rowid and return.
|
|
|
|
|
|
|
|
When this function returns, either rowid buffer is not empty, or the source
|
|
|
|
of lookup keys (i.e. ranges) is exhaused.
|
|
|
|
|
|
|
|
@retval 0 OK, the next portion of rowids is in the buffer,
|
|
|
|
properly ordered
|
|
|
|
@retval other Error
|
|
|
|
*/
|
|
|
|
|
2010-11-25 14:35:21 +03:00
|
|
|
int Mrr_ordered_rndpos_reader::refill_buffer(bool initial)
|
2010-11-01 13:52:10 +03:00
|
|
|
{
|
|
|
|
int res;
|
2012-01-23 23:35:52 +04:00
|
|
|
bool first_call= initial;
|
2010-11-01 13:52:10 +03:00
|
|
|
DBUG_ENTER("Mrr_ordered_rndpos_reader::refill_buffer");
|
|
|
|
|
|
|
|
if (index_reader_exhausted)
|
|
|
|
DBUG_RETURN(HA_ERR_END_OF_FILE);
|
|
|
|
|
2010-11-25 14:35:21 +03:00
|
|
|
while (initial || index_reader_needs_refill ||
|
|
|
|
(res= refill_from_index_reader()) == HA_ERR_END_OF_FILE)
|
2010-11-01 13:52:10 +03:00
|
|
|
{
|
2010-11-25 14:35:21 +03:00
|
|
|
if ((res= index_reader->refill_buffer(initial)))
|
2010-11-01 13:52:10 +03:00
|
|
|
{
|
|
|
|
if (res == HA_ERR_END_OF_FILE)
|
|
|
|
index_reader_exhausted= TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2010-11-25 14:35:21 +03:00
|
|
|
initial= FALSE;
|
|
|
|
index_reader_needs_refill= FALSE;
|
2010-11-01 13:52:10 +03:00
|
|
|
}
|
2012-01-23 23:35:52 +04:00
|
|
|
|
|
|
|
if (!first_call && !index_reader_exhausted)
|
|
|
|
{
|
|
|
|
/* Ok, this was a successful buffer refill operation */
|
|
|
|
THD *thd= current_thd;
|
2012-02-22 16:48:29 +04:00
|
|
|
status_var_increment(thd->status_var.ha_mrr_rowid_refills_count);
|
2012-01-23 23:35:52 +04:00
|
|
|
}
|
|
|
|
|
2010-11-01 13:52:10 +03:00
|
|
|
DBUG_RETURN(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-03 00:09:28 +03:00
|
|
|
void Mrr_index_reader::position()
|
|
|
|
{
|
2010-12-02 16:21:52 +03:00
|
|
|
file->position(file->get_table()->record[0]);
|
2010-11-03 00:09:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
@brief Try to refill the rowid buffer without calling
|
|
|
|
index_reader->refill_buffer().
|
|
|
|
*/
|
|
|
|
|
2010-11-25 14:35:21 +03:00
|
|
|
int Mrr_ordered_rndpos_reader::refill_from_index_reader()
|
2010-10-26 15:35:13 +04:00
|
|
|
{
|
2011-03-04 12:06:03 +03:00
|
|
|
range_id_t range_info;
|
2010-10-26 15:35:13 +04:00
|
|
|
int res;
|
2010-11-25 14:35:21 +03:00
|
|
|
DBUG_ENTER("Mrr_ordered_rndpos_reader::refill_from_index_reader");
|
2010-11-01 13:52:10 +03:00
|
|
|
|
2010-10-26 15:35:13 +04:00
|
|
|
DBUG_ASSERT(rowid_buffer->is_empty());
|
|
|
|
index_rowid= index_reader->get_rowid_ptr();
|
|
|
|
rowid_buffer->reset();
|
2010-12-16 23:37:26 +03:00
|
|
|
rowid_buffer->setup_writing(file->ref_length,
|
2011-03-04 12:06:03 +03:00
|
|
|
is_mrr_assoc? sizeof(range_id_t) : 0);
|
2010-10-26 15:35:13 +04:00
|
|
|
|
|
|
|
last_identical_rowid= NULL;
|
|
|
|
|
2010-12-09 00:47:33 +03:00
|
|
|
index_reader->resume_read();
|
2010-10-26 15:35:13 +04:00
|
|
|
while (rowid_buffer->can_write())
|
|
|
|
{
|
|
|
|
res= index_reader->get_next(&range_info);
|
|
|
|
|
|
|
|
if (res)
|
2010-11-22 17:08:22 +03:00
|
|
|
{
|
|
|
|
if (res != HA_ERR_END_OF_FILE)
|
|
|
|
DBUG_RETURN(res);
|
2010-11-25 14:35:21 +03:00
|
|
|
index_reader_needs_refill=TRUE;
|
2010-10-26 15:35:13 +04:00
|
|
|
break;
|
2010-11-22 17:08:22 +03:00
|
|
|
}
|
2010-10-26 15:35:13 +04:00
|
|
|
|
2010-11-03 00:09:28 +03:00
|
|
|
index_reader->position();
|
2010-10-26 15:35:13 +04:00
|
|
|
|
2020-01-18 13:26:03 -08:00
|
|
|
/*
|
2020-02-18 22:49:42 -08:00
|
|
|
If the built rowid filter cannot be used at the engine level, use it here.
|
2020-01-18 13:26:03 -08:00
|
|
|
*/
|
|
|
|
if (rowid_filter && !file->pushed_rowid_filter &&
|
|
|
|
!rowid_filter->check((char *)index_rowid))
|
|
|
|
continue;
|
|
|
|
|
2010-11-22 17:08:22 +03:00
|
|
|
/* Put rowid, or {rowid, range_id} pair into the buffer */
|
2010-12-16 23:37:26 +03:00
|
|
|
rowid_buffer->write_ptr1= index_rowid;
|
|
|
|
rowid_buffer->write_ptr2= (uchar*)&range_info;
|
2010-10-26 15:35:13 +04:00
|
|
|
rowid_buffer->write();
|
|
|
|
}
|
2014-11-13 13:56:35 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
When index_reader_needs_refill=TRUE, this means we've got all of index
|
|
|
|
tuples for lookups keys that index_reader had. We are not in the middle
|
|
|
|
of an index read, so there is no need to call interrupt_read.
|
|
|
|
|
|
|
|
Actually, we must not call interrupt_read(), because it could be that we
|
|
|
|
haven't read a single row (because all index lookups returned
|
|
|
|
HA_ERR_KEY_NOT_FOUND). In this case, interrupt_read() will cause [harmless]
|
|
|
|
valgrind warnings when trying to save garbage from table->record[0].
|
|
|
|
*/
|
|
|
|
if (!index_reader_needs_refill)
|
|
|
|
index_reader->interrupt_read();
|
2010-10-26 15:35:13 +04:00
|
|
|
/* Sort the buffer contents by rowid */
|
2010-12-02 16:21:52 +03:00
|
|
|
rowid_buffer->sort((qsort2_cmp)rowid_cmp_reverse, (void*)file);
|
2010-10-26 15:35:13 +04:00
|
|
|
|
2010-12-16 21:18:35 +03:00
|
|
|
rowid_buffer->setup_reading(file->ref_length,
|
2011-03-04 12:06:03 +03:00
|
|
|
is_mrr_assoc ? sizeof(range_id_t) : 0);
|
2010-11-01 13:52:10 +03:00
|
|
|
DBUG_RETURN(rowid_buffer->is_empty()? HA_ERR_END_OF_FILE : 0);
|
2010-10-26 15:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-03 00:09:28 +03:00
|
|
|
/*
|
2010-11-08 15:15:50 +03:00
|
|
|
Get the next {record, range_id} using ordered array of rowid+range_id pairs
|
2010-10-26 15:35:13 +04:00
|
|
|
|
2010-11-03 00:09:28 +03:00
|
|
|
@note
|
|
|
|
Since we have sorted rowids, we try not to make multiple rnd_pos() calls
|
|
|
|
with the same rowid value.
|
2010-10-26 15:35:13 +04:00
|
|
|
*/
|
|
|
|
|
2011-03-04 12:06:03 +03:00
|
|
|
int Mrr_ordered_rndpos_reader::get_next(range_id_t *range_info)
|
2010-10-26 15:35:13 +04:00
|
|
|
{
|
|
|
|
int res;
|
2010-11-23 18:08:11 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
First, check if rowid buffer has elements with the same rowid value as
|
|
|
|
the previous.
|
|
|
|
*/
|
2010-10-26 15:35:13 +04:00
|
|
|
while (last_identical_rowid)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Current record (the one we've returned in previous call) was obtained
|
|
|
|
from a rowid that matched multiple range_ids. Return this record again,
|
|
|
|
with next matching range_id.
|
|
|
|
*/
|
2010-11-23 18:08:11 +03:00
|
|
|
(void)rowid_buffer->read();
|
2010-10-26 15:35:13 +04:00
|
|
|
|
2010-12-16 21:18:35 +03:00
|
|
|
if (rowid_buffer->read_ptr1 == last_identical_rowid)
|
2010-10-26 15:35:13 +04:00
|
|
|
last_identical_rowid= NULL; /* reached the last of identical rowids */
|
|
|
|
|
2010-11-23 18:08:11 +03:00
|
|
|
if (!is_mrr_assoc)
|
|
|
|
return 0;
|
|
|
|
|
2011-03-04 12:06:03 +03:00
|
|
|
memcpy(range_info, rowid_buffer->read_ptr2, sizeof(range_id_t));
|
|
|
|
if (!index_reader->skip_record(*range_info, rowid_buffer->read_ptr1))
|
2010-10-26 15:35:13 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2010-11-23 18:08:11 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
Ok, last_identical_rowid==NULL, it's time to read next different rowid
|
|
|
|
value and get record for it.
|
|
|
|
*/
|
2010-11-22 17:08:22 +03:00
|
|
|
for(;;)
|
2010-10-26 15:35:13 +04:00
|
|
|
{
|
|
|
|
/* Return eof if there are no rowids in the buffer after re-fill attempt */
|
|
|
|
if (rowid_buffer->read())
|
|
|
|
return HA_ERR_END_OF_FILE;
|
|
|
|
|
|
|
|
if (is_mrr_assoc)
|
|
|
|
{
|
2011-03-04 12:06:03 +03:00
|
|
|
memcpy(range_info, rowid_buffer->read_ptr2, sizeof(range_id_t));
|
2010-12-16 21:18:35 +03:00
|
|
|
if (index_reader->skip_record(*range_info, rowid_buffer->read_ptr1))
|
2010-11-23 18:08:11 +03:00
|
|
|
continue;
|
|
|
|
}
|
2010-10-26 15:35:13 +04:00
|
|
|
|
2010-12-16 21:18:35 +03:00
|
|
|
res= file->ha_rnd_pos(file->get_table()->record[0],
|
|
|
|
rowid_buffer->read_ptr1);
|
2010-10-26 15:35:13 +04:00
|
|
|
|
2010-11-23 18:08:11 +03:00
|
|
|
if (res)
|
|
|
|
return res; /* Some fatal error */
|
|
|
|
|
|
|
|
break; /* Got another record */
|
2010-10-26 15:35:13 +04:00
|
|
|
}
|
|
|
|
|
2010-11-23 18:08:11 +03:00
|
|
|
/*
|
|
|
|
Check if subsequent buffer elements have the same rowid value as this
|
|
|
|
one. If yes, remember this fact so that we don't make any more rnd_pos()
|
|
|
|
calls with this value.
|
2010-12-16 21:18:35 +03:00
|
|
|
|
2010-11-23 18:08:11 +03:00
|
|
|
Note: this implies that SQL layer doesn't touch table->record[0]
|
|
|
|
between calls.
|
|
|
|
*/
|
|
|
|
Lifo_buffer_iterator it;
|
|
|
|
it.init(rowid_buffer);
|
2010-12-16 21:18:35 +03:00
|
|
|
while (!it.read())
|
2010-11-23 18:08:11 +03:00
|
|
|
{
|
2010-12-16 21:18:35 +03:00
|
|
|
if (file->cmp_ref(it.read_ptr1, rowid_buffer->read_ptr1))
|
2010-11-23 18:08:11 +03:00
|
|
|
break;
|
2010-12-16 21:18:35 +03:00
|
|
|
last_identical_rowid= it.read_ptr1;
|
2010-11-23 18:08:11 +03:00
|
|
|
}
|
|
|
|
return 0;
|
2010-10-26 15:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
/****************************************************************************
|
2010-11-03 00:09:28 +03:00
|
|
|
* Top-level DS-MRR implementation functions (the ones called by storage engine)
|
2009-12-22 15:33:21 +03:00
|
|
|
***************************************************************************/
|
2009-12-15 10:16:46 +03:00
|
|
|
|
|
|
|
/**
|
2009-12-22 15:33:21 +03:00
|
|
|
DS-MRR: Initialize and start MRR scan
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
Initialize and start the MRR scan. Depending on the mode parameter, this
|
|
|
|
may use default or DS-MRR implementation.
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2010-12-02 16:21:52 +03:00
|
|
|
@param h_arg Table handler to be used
|
2009-12-22 15:33:21 +03:00
|
|
|
@param key Index to be used
|
|
|
|
@param seq_funcs Interval sequence enumeration functions
|
|
|
|
@param seq_init_param Interval sequence enumeration parameter
|
|
|
|
@param n_ranges Number of ranges in the sequence.
|
|
|
|
@param mode HA_MRR_* modes to use
|
|
|
|
@param buf INOUT Buffer to use
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
@retval 0 Ok, Scan started.
|
|
|
|
@retval other Error
|
2009-12-15 10:16:46 +03:00
|
|
|
*/
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
int DsMrr_impl::dsmrr_init(handler *h_arg, RANGE_SEQ_IF *seq_funcs,
|
|
|
|
void *seq_init_param, uint n_ranges, uint mode,
|
|
|
|
HANDLER_BUFFER *buf)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2020-02-18 22:49:42 -08:00
|
|
|
TABLE *table= h_arg->get_table();
|
|
|
|
THD *thd= table->in_use;
|
2010-10-26 15:35:13 +04:00
|
|
|
int res;
|
2010-12-02 14:10:52 +03:00
|
|
|
Key_parameters keypar;
|
2015-02-10 14:05:49 +04:00
|
|
|
uint UNINIT_VAR(key_buff_elem_size); /* set/used when do_sort_keys==TRUE */
|
2010-12-02 14:10:52 +03:00
|
|
|
handler *h_idx;
|
|
|
|
Mrr_ordered_rndpos_reader *disk_strategy= NULL;
|
|
|
|
bool do_sort_keys= FALSE;
|
2009-12-22 15:33:21 +03:00
|
|
|
DBUG_ENTER("DsMrr_impl::dsmrr_init");
|
2009-12-15 10:16:46 +03:00
|
|
|
/*
|
2009-12-22 15:33:21 +03:00
|
|
|
index_merge may invoke a scan on an object for which dsmrr_info[_const]
|
|
|
|
has not been called, so set the owner handler here as well.
|
2009-12-15 10:16:46 +03:00
|
|
|
*/
|
2010-12-02 16:21:52 +03:00
|
|
|
primary_file= h_arg;
|
2014-02-19 14:05:15 +04:00
|
|
|
is_mrr_assoc= !MY_TEST(mode & HA_MRR_NO_ASSOCIATION);
|
2010-10-26 15:35:13 +04:00
|
|
|
|
2010-11-26 15:45:46 +03:00
|
|
|
strategy_exhausted= FALSE;
|
2010-12-02 14:10:52 +03:00
|
|
|
|
|
|
|
/* By default, have do-nothing buffer manager */
|
|
|
|
buf_manager.arg= this;
|
|
|
|
buf_manager.reset_buffer_sizes= do_nothing;
|
|
|
|
buf_manager.redistribute_buffer_space= do_nothing;
|
2010-11-26 15:45:46 +03:00
|
|
|
|
2010-11-23 18:08:11 +03:00
|
|
|
if (mode & (HA_MRR_USE_DEFAULT_IMPL | HA_MRR_SORTED))
|
2010-12-02 14:10:52 +03:00
|
|
|
goto use_default_impl;
|
2010-08-08 11:13:54 +04:00
|
|
|
|
2010-07-17 18:03:50 +04:00
|
|
|
/*
|
2010-09-28 12:19:50 +04:00
|
|
|
Determine whether we'll need to do key sorting and/or rnd_pos() scan
|
2010-07-18 01:05:44 +04:00
|
|
|
*/
|
2010-10-26 15:35:13 +04:00
|
|
|
index_strategy= NULL;
|
|
|
|
if ((mode & HA_MRR_SINGLE_POINT) &&
|
|
|
|
optimizer_flag(thd, OPTIMIZER_SWITCH_MRR_SORT_KEYS))
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2010-12-02 14:10:52 +03:00
|
|
|
do_sort_keys= TRUE;
|
2010-11-23 18:08:11 +03:00
|
|
|
index_strategy= &reader_factory.ordered_index_reader;
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
2010-10-26 15:35:13 +04:00
|
|
|
else
|
2010-11-02 23:25:35 +03:00
|
|
|
index_strategy= &reader_factory.simple_index_reader;
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2010-10-26 15:35:13 +04:00
|
|
|
strategy= index_strategy;
|
|
|
|
/*
|
|
|
|
We don't need a rowid-to-rndpos step if
|
|
|
|
- We're doing a scan on clustered primary key
|
|
|
|
- [In the future] We're doing an index_only read
|
|
|
|
*/
|
2010-12-02 16:21:52 +03:00
|
|
|
DBUG_ASSERT(primary_file->inited == handler::INDEX ||
|
|
|
|
(primary_file->inited == handler::RND &&
|
|
|
|
secondary_file &&
|
|
|
|
secondary_file->inited == handler::INDEX));
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2010-12-02 16:21:52 +03:00
|
|
|
h_idx= (primary_file->inited == handler::INDEX)? primary_file: secondary_file;
|
2010-10-26 15:35:13 +04:00
|
|
|
keyno= h_idx->active_index;
|
|
|
|
|
2020-02-26 14:52:23 +02:00
|
|
|
if (! h_idx->is_clustering_key(keyno))
|
2010-07-17 18:03:50 +04:00
|
|
|
{
|
2010-11-02 23:25:35 +03:00
|
|
|
strategy= disk_strategy= &reader_factory.ordered_rndpos_reader;
|
2020-02-18 22:49:42 -08:00
|
|
|
if (h_arg->pushed_rowid_filter)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Currently usage of a rowid filter within InnoDB engine is not supported
|
|
|
|
if the table is accessed by the primary key.
|
|
|
|
With optimizer switches ''mrr' and 'mrr_sort_keys' are both enabled
|
|
|
|
any access by a secondary index is converted to the rndpos access. In
|
|
|
|
InnoDB the rndpos access is always uses the primary key.
|
|
|
|
Do not use pushed rowid filter if the table is accessed actually by the
|
|
|
|
primary key. Use the rowid filter outside the engine code (see
|
|
|
|
Mrr_ordered_rndpos_reader::refill_from_index_reader).
|
|
|
|
*/
|
2020-02-20 13:35:19 +03:00
|
|
|
rowid_filter= h_arg->pushed_rowid_filter;
|
2020-02-18 22:49:42 -08:00
|
|
|
h_arg->cancel_pushed_rowid_filter();
|
|
|
|
}
|
2010-07-17 18:03:50 +04:00
|
|
|
}
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2010-09-10 20:48:11 +04:00
|
|
|
full_buf= buf->buffer;
|
|
|
|
full_buf_end= buf->buffer_end;
|
2010-12-02 14:10:52 +03:00
|
|
|
|
|
|
|
if (do_sort_keys)
|
|
|
|
{
|
|
|
|
/* Pre-calculate some parameters of key sorting */
|
2014-02-19 14:05:15 +04:00
|
|
|
keypar.use_key_pointers= MY_TEST(mode & HA_MRR_MATERIALIZED_KEYS);
|
2010-12-02 14:10:52 +03:00
|
|
|
seq_funcs->get_key_info(seq_init_param, &keypar.key_tuple_length,
|
|
|
|
&keypar.key_tuple_map);
|
|
|
|
keypar.key_size_in_keybuf= keypar.use_key_pointers?
|
|
|
|
sizeof(char*) : keypar.key_tuple_length;
|
|
|
|
key_buff_elem_size= keypar.key_size_in_keybuf + (int)is_mrr_assoc * sizeof(void*);
|
2010-12-09 00:47:33 +03:00
|
|
|
|
|
|
|
/* Ordered index reader needs some space to store an index tuple */
|
|
|
|
if (strategy != index_strategy)
|
|
|
|
{
|
2011-03-04 00:54:10 +03:00
|
|
|
uint saved_pk_length=0;
|
2020-02-26 14:52:23 +02:00
|
|
|
uint pk= h_idx->get_table()->s->primary_key;
|
|
|
|
if (h_idx->pk_is_clustering_key(pk))
|
2011-03-04 00:54:10 +03:00
|
|
|
{
|
2020-02-26 14:52:23 +02:00
|
|
|
saved_pk_length= h_idx->get_table()->key_info[pk].key_length;
|
2011-03-04 00:54:10 +03:00
|
|
|
}
|
2011-09-13 23:45:02 +04:00
|
|
|
|
|
|
|
KEY *used_index= &h_idx->get_table()->key_info[h_idx->active_index];
|
2010-12-20 14:40:12 +03:00
|
|
|
if (reader_factory.ordered_index_reader.
|
|
|
|
set_interruption_temp_buffer(primary_file->ref_length,
|
2011-09-13 23:45:02 +04:00
|
|
|
used_index->key_length,
|
2011-03-04 00:54:10 +03:00
|
|
|
saved_pk_length,
|
2010-12-20 14:40:12 +03:00
|
|
|
&full_buf, full_buf_end))
|
2010-12-09 00:47:33 +03:00
|
|
|
goto use_default_impl;
|
|
|
|
}
|
2010-12-20 14:40:12 +03:00
|
|
|
else
|
|
|
|
reader_factory.ordered_index_reader.set_no_interruption_temp_buffer();
|
2010-12-02 14:10:52 +03:00
|
|
|
}
|
|
|
|
|
2010-10-26 15:35:13 +04:00
|
|
|
if (strategy == index_strategy)
|
2010-06-19 15:40:19 +04:00
|
|
|
{
|
2010-12-02 14:10:52 +03:00
|
|
|
/*
|
|
|
|
Index strategy alone handles the record retrieval. Give all buffer space
|
|
|
|
to it. Key buffer should have forward orientation so we can return the
|
|
|
|
end of it.
|
|
|
|
*/
|
|
|
|
key_buffer= &forward_key_buf;
|
|
|
|
key_buffer->set_buffer_space(full_buf, full_buf_end);
|
|
|
|
|
|
|
|
/* Safety: specify that rowid buffer has zero size: */
|
|
|
|
rowid_buffer.set_buffer_space(full_buf_end, full_buf_end);
|
|
|
|
|
|
|
|
if (do_sort_keys && !key_buffer->have_space_for(key_buff_elem_size))
|
|
|
|
goto use_default_impl;
|
|
|
|
|
2010-12-02 16:21:52 +03:00
|
|
|
if ((res= index_strategy->init(primary_file, seq_funcs, seq_init_param, n_ranges,
|
2010-12-02 14:10:52 +03:00
|
|
|
mode, &keypar, key_buffer, &buf_manager)))
|
2010-10-26 15:35:13 +04:00
|
|
|
goto error;
|
2010-06-22 21:24:22 +04:00
|
|
|
}
|
2010-10-26 15:35:13 +04:00
|
|
|
else
|
2010-08-08 11:13:54 +04:00
|
|
|
{
|
2010-12-02 14:10:52 +03:00
|
|
|
/* We'll have both index and rndpos strategies working together */
|
|
|
|
if (do_sort_keys)
|
|
|
|
{
|
|
|
|
/* Both strategies will need buffer space, share the buffer */
|
|
|
|
if (setup_buffer_sharing(keypar.key_size_in_keybuf, keypar.key_tuple_map))
|
|
|
|
goto use_default_impl;
|
2010-10-26 15:35:13 +04:00
|
|
|
|
2010-12-02 14:10:52 +03:00
|
|
|
buf_manager.reset_buffer_sizes= reset_buffer_sizes;
|
|
|
|
buf_manager.redistribute_buffer_space= redistribute_buffer_space;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* index strategy doesn't need buffer, give all space to rowids*/
|
|
|
|
rowid_buffer.set_buffer_space(full_buf, full_buf_end);
|
2010-12-02 16:21:52 +03:00
|
|
|
if (!rowid_buffer.have_space_for(primary_file->ref_length +
|
2011-03-04 12:06:03 +03:00
|
|
|
(int)is_mrr_assoc * sizeof(range_id_t)))
|
2010-12-02 14:10:52 +03:00
|
|
|
goto use_default_impl;
|
|
|
|
}
|
2010-10-26 15:35:13 +04:00
|
|
|
|
2020-02-20 13:35:19 +03:00
|
|
|
// setup_two_handlers() will call dsmrr_close() will clears the filter.
|
|
|
|
// Save its value and restore afterwards.
|
|
|
|
Rowid_filter *tmp = rowid_filter;
|
2010-10-26 15:35:13 +04:00
|
|
|
if ((res= setup_two_handlers()))
|
2010-11-23 18:08:11 +03:00
|
|
|
goto error;
|
2020-02-20 13:35:19 +03:00
|
|
|
rowid_filter= tmp;
|
2010-10-26 15:35:13 +04:00
|
|
|
|
2010-12-02 16:21:52 +03:00
|
|
|
if ((res= index_strategy->init(secondary_file, seq_funcs, seq_init_param,
|
|
|
|
n_ranges, mode, &keypar, key_buffer,
|
|
|
|
&buf_manager)) ||
|
|
|
|
(res= disk_strategy->init(primary_file, index_strategy, mode,
|
2020-02-20 13:35:19 +03:00
|
|
|
&rowid_buffer, rowid_filter)))
|
2010-10-26 15:35:13 +04:00
|
|
|
{
|
|
|
|
goto error;
|
|
|
|
}
|
2010-08-08 11:13:54 +04:00
|
|
|
}
|
2012-02-22 16:48:29 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
At this point, we're sure that we're running a native MRR scan (i.e. we
|
|
|
|
didnt fall back to default implementation for some reason).
|
|
|
|
*/
|
|
|
|
status_var_increment(thd->status_var.ha_mrr_init_count);
|
2010-11-26 00:30:39 +03:00
|
|
|
|
2010-11-25 14:35:21 +03:00
|
|
|
res= strategy->refill_buffer(TRUE);
|
2010-11-26 00:30:39 +03:00
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
if (res != HA_ERR_END_OF_FILE)
|
|
|
|
goto error;
|
|
|
|
strategy_exhausted= TRUE;
|
|
|
|
}
|
2010-10-26 15:35:13 +04:00
|
|
|
|
2010-06-19 15:40:19 +04:00
|
|
|
/*
|
2010-10-26 15:35:13 +04:00
|
|
|
If we have scanned through all intervals in *seq, then adjust *buf to
|
|
|
|
indicate that the remaining buffer space will not be used.
|
2009-12-22 15:33:21 +03:00
|
|
|
*/
|
2010-10-26 15:35:13 +04:00
|
|
|
// if (dsmrr_eof)
|
|
|
|
// buf->end_of_used_area= rowid_buffer.end_of_space();
|
|
|
|
|
|
|
|
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
error:
|
|
|
|
close_second_handler();
|
2010-11-23 18:08:11 +03:00
|
|
|
/* Safety, not really needed but: */
|
2010-10-26 15:35:13 +04:00
|
|
|
strategy= NULL;
|
Fix for lp:711565 "Index Condition Pushdown can make a thread hold MyISAM locks as well as be unKILLable for long time"
- In Maria/MyISAM: Release/re-acquire locks to give queries that wait on them a chance to make progress
- In Maria/MyISAM: Change from numeric constants to ICP_RES values.
- In Maria: Do check index condition in maria_rprev() (was lost in the merge/backport?)
- In Maria/MyISAM/XtraDB: Check if the query was killed, and return immediately if it was.
Added new storage engine error: HA_ERR_ABORTED_BY_USER, for handler to signal that it detected a kill of the query and aborted
Authors: Sergey Petrunia & Monty
include/my_base.h:
Added HA_ERR_ABORTED_BY_USER, for handler to signal that it detected a kill of the query and aborted
include/my_handler.h:
Added comment
mysql-test/r/myisam_icp.result:
Updated test
mysql-test/t/myisam_icp.test:
Drop used tables at start of test
Added test case that can help with manual testing of killing index condition pushdown query.
mysys/my_handler_errors.h:
Text for new storage engine error
sql/handler.cc:
If engine got HA_ERR_ABORTED_BY_USER, send kill message.
sql/multi_range_read.cc:
Return error code
storage/maria/ha_maria.cc:
Added ma_killed_in_mariadb() to detect kill.
Ensure that file->external_ref points to TABLE object.
storage/maria/ma_extra.c:
Dummy test-if-killed for standalone
storage/maria/ma_key.c:
If ma_check_index_cond() fails, set my_errno and info->cur_row.lastpos
storage/maria/ma_rkey.c:
Release/re-acquire locks to give queries that wait on them a chance to make progress
Check if the query was killed, and return immediately if it was
storage/maria/ma_rnext.c:
Check if the query was killed, and return immediately if it was
Added missing fast_ma_writeinfo(info)
storage/maria/ma_rnext_same.c:
Check if the query was killed, and return immediately if it was
Added missing fast_ma_writeinfo(info)
storage/maria/ma_rprev.c:
Check if the query was killed, and return immediately if it was
Added missing fast_ma_writeinfo(info) and ma_check_index_cond()
storage/maria/ma_search.c:
Give error message if we find a wrong key
storage/maria/ma_static.c:
Added pointer to test-if-killed function
storage/maria/maria_def.h:
New prototypes
storage/myisam/ha_myisam.cc:
Added mi_killed_in_mariadb()
Ensure that file->external_ref points to TABLE object.
storage/myisam/mi_extra.c:
Dummy test-if-killed for standalone
storage/myisam/mi_key.c:
If ma_check_index_cond() fails, set my_errno and info->lastpos
storage/myisam/mi_rkey.c:
Ensure that info->lastpos= HA_OFFSET_ERROR in case of error
Release/re-acquire locks to give queries that wait on them a chance to make progress
Check if the query was killed, and return immediately if it was
Reorder code to do less things in case of error.
Added missing fast_mi_writeinfo()
storage/myisam/mi_rnext.c:
Check if the query was killed, and return immediately if it was
Simplify old ICP code
Added missing fast_ma_writeinfo(info)
storage/myisam/mi_rnext_same.c:
Check if the query was killed, and return immediately if it was
Added missing fast_mi_writeinfo(info)
storage/myisam/mi_rprev.c:
Check if the query was killed, and return immediately if it was
Simplify error handling of ICP
Added missing fast_mi_writeinfo(info)
storage/myisam/mi_search.c:
Give error message if we find a wrong key
storage/myisam/mi_static.c:
Added pointer to test-if-killed function
storage/myisam/myisamdef.h:
New prototypes
storage/xtradb/handler/ha_innodb.cc:
Added DB_SEARCH_ABORTED_BY_USER and ha_innobase::is_thd_killed()
Check if the query was killed, and return immediately if it was
storage/xtradb/handler/ha_innodb.h:
Added prototype
storage/xtradb/include/db0err.h:
Added DB_SEARCH_ABORTED_BY_USER
storage/xtradb/include/row0mysql.h:
Added possible ICP errors
storage/xtradb/row/row0sel.c:
Use ICP errors instead of constants.
Detect if killed and return B_SEARCH_ABORTED_BY_USER
2011-02-18 17:43:59 +02:00
|
|
|
DBUG_RETURN(res);
|
2010-12-02 14:10:52 +03:00
|
|
|
|
|
|
|
use_default_impl:
|
2011-03-02 23:08:13 +03:00
|
|
|
if (primary_file->inited != handler::INDEX)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2011-03-02 23:08:13 +03:00
|
|
|
/* We can get here when
|
|
|
|
- we've previously successfully done a DS-MRR scan (and so have
|
|
|
|
secondary_file!= NULL, secondary_file->inited= INDEX,
|
|
|
|
primary_file->inited=RND)
|
|
|
|
- for this invocation, we haven't got enough buffer space, and so we
|
|
|
|
have to use the default MRR implementation.
|
|
|
|
|
|
|
|
note: primary_file->ha_index_end() will call dsmrr_close() which will
|
|
|
|
close/destroy the secondary_file, this is intentional.
|
|
|
|
(Yes this is slow, but one can't expect performance with join buffer
|
|
|
|
so small that it can accomodate one rowid and one index tuple)
|
|
|
|
*/
|
|
|
|
if ((res= primary_file->ha_rnd_end()) ||
|
2014-02-19 14:05:15 +04:00
|
|
|
(res= primary_file->ha_index_init(keyno, MY_TEST(mode & HA_MRR_SORTED))))
|
2011-03-02 23:08:13 +03:00
|
|
|
{
|
|
|
|
DBUG_RETURN(res);
|
|
|
|
}
|
|
|
|
}
|
2010-12-02 14:10:52 +03:00
|
|
|
/* Call correct init function and assign to top level object */
|
|
|
|
Mrr_simple_index_reader *s= &reader_factory.simple_index_reader;
|
2010-12-02 16:21:52 +03:00
|
|
|
res= s->init(primary_file, seq_funcs, seq_init_param, n_ranges, mode, NULL,
|
|
|
|
NULL, NULL);
|
2010-12-02 14:10:52 +03:00
|
|
|
strategy= s;
|
|
|
|
DBUG_RETURN(res);
|
2010-10-26 15:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Whatever the current state is, make it so that we have two handler objects:
|
2010-12-02 16:21:52 +03:00
|
|
|
- primary_file - initialized for rnd_pos() scan
|
|
|
|
- secondary_file - initialized for scanning the index specified in
|
2010-10-26 15:35:13 +04:00
|
|
|
this->keyno
|
|
|
|
RETURN
|
|
|
|
0 OK
|
|
|
|
HA_XXX Error code
|
|
|
|
*/
|
|
|
|
|
|
|
|
int DsMrr_impl::setup_two_handlers()
|
|
|
|
{
|
|
|
|
int res;
|
2010-12-02 16:21:52 +03:00
|
|
|
THD *thd= primary_file->get_table()->in_use;
|
2010-10-26 15:35:13 +04:00
|
|
|
DBUG_ENTER("DsMrr_impl::setup_two_handlers");
|
2010-12-02 16:21:52 +03:00
|
|
|
if (!secondary_file)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2010-10-26 15:35:13 +04:00
|
|
|
handler *new_h2;
|
|
|
|
Item *pushed_cond= NULL;
|
2010-12-02 16:21:52 +03:00
|
|
|
DBUG_ASSERT(primary_file->inited == handler::INDEX);
|
2010-07-17 18:03:50 +04:00
|
|
|
/* Create a separate handler object to do rnd_pos() calls. */
|
2009-12-22 15:33:21 +03:00
|
|
|
/*
|
|
|
|
::clone() takes up a lot of stack, especially on 64 bit platforms.
|
|
|
|
The constant 5 is an empiric result.
|
|
|
|
*/
|
|
|
|
if (check_stack_overrun(thd, 5*STACK_MIN_SIZE, (uchar*) &new_h2))
|
|
|
|
DBUG_RETURN(1);
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2010-07-17 18:03:50 +04:00
|
|
|
/* Create a separate handler object to do rnd_pos() calls. */
|
2011-05-10 18:17:43 +03:00
|
|
|
if (!(new_h2= primary_file->clone(primary_file->get_table()->s->
|
|
|
|
normalized_path.str,
|
|
|
|
thd->mem_root)) ||
|
2009-12-22 15:33:21 +03:00
|
|
|
new_h2->ha_external_lock(thd, F_RDLCK))
|
|
|
|
{
|
|
|
|
delete new_h2;
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2010-12-02 16:21:52 +03:00
|
|
|
if (keyno == primary_file->pushed_idx_cond_keyno)
|
|
|
|
pushed_cond= primary_file->pushed_idx_cond;
|
2010-10-26 15:35:13 +04:00
|
|
|
|
2010-10-28 20:48:16 +04:00
|
|
|
Mrr_reader *save_strategy= strategy;
|
2010-10-26 15:35:13 +04:00
|
|
|
strategy= NULL;
|
2009-12-22 15:33:21 +03:00
|
|
|
/*
|
|
|
|
Caution: this call will invoke this->dsmrr_close(). Do not put the
|
2010-12-02 16:21:52 +03:00
|
|
|
created secondary table handler new_h2 into this->secondary_file or it
|
|
|
|
will delete it. Also, save the picked strategy
|
2009-12-22 15:33:21 +03:00
|
|
|
*/
|
2010-12-02 16:21:52 +03:00
|
|
|
res= primary_file->ha_index_end();
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2010-10-26 15:35:13 +04:00
|
|
|
strategy= save_strategy;
|
2010-12-02 16:21:52 +03:00
|
|
|
secondary_file= new_h2;
|
2010-10-26 15:35:13 +04:00
|
|
|
|
2010-12-02 16:21:52 +03:00
|
|
|
if (res || (res= (primary_file->ha_rnd_init(FALSE))))
|
2009-12-22 15:33:21 +03:00
|
|
|
goto error;
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
table->prepare_for_position();
|
2010-12-02 16:21:52 +03:00
|
|
|
secondary_file->extra(HA_EXTRA_KEYREAD);
|
|
|
|
secondary_file->mrr_iter= primary_file->mrr_iter;
|
2010-08-08 11:13:54 +04:00
|
|
|
|
2010-12-02 16:21:52 +03:00
|
|
|
if ((res= secondary_file->ha_index_init(keyno, FALSE)))
|
2009-12-22 15:33:21 +03:00
|
|
|
goto error;
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
if (pushed_cond)
|
2010-12-02 16:21:52 +03:00
|
|
|
secondary_file->idx_cond_push(keyno, pushed_cond);
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-02 16:21:52 +03:00
|
|
|
DBUG_ASSERT(secondary_file && secondary_file->inited==handler::INDEX);
|
2009-12-22 15:33:21 +03:00
|
|
|
/*
|
|
|
|
We get here when the access alternates betwen MRR scan(s) and non-MRR
|
|
|
|
scans.
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2020-02-28 12:59:30 +02:00
|
|
|
Calling primary_file->index_end() will invoke dsmrr_close() for this
|
|
|
|
object, which will delete secondary_file. We need to keep it, so put it
|
|
|
|
away and don't let it be deleted:
|
2009-12-22 15:33:21 +03:00
|
|
|
*/
|
2010-12-02 16:21:52 +03:00
|
|
|
if (primary_file->inited == handler::INDEX)
|
2010-10-26 15:35:13 +04:00
|
|
|
{
|
2010-12-02 16:21:52 +03:00
|
|
|
handler *save_h2= secondary_file;
|
2010-10-28 20:48:16 +04:00
|
|
|
Mrr_reader *save_strategy= strategy;
|
2010-12-02 16:21:52 +03:00
|
|
|
secondary_file= NULL;
|
2010-10-26 15:35:13 +04:00
|
|
|
strategy= NULL;
|
2010-12-02 16:21:52 +03:00
|
|
|
res= primary_file->ha_index_end();
|
|
|
|
secondary_file= save_h2;
|
2010-10-26 15:35:13 +04:00
|
|
|
strategy= save_strategy;
|
|
|
|
if (res)
|
|
|
|
goto error;
|
|
|
|
}
|
2010-12-17 13:06:21 +03:00
|
|
|
if ((primary_file->inited != handler::RND) &&
|
|
|
|
(res= primary_file->ha_rnd_init(FALSE)))
|
2009-12-22 15:33:21 +03:00
|
|
|
goto error;
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
2009-12-22 15:33:21 +03:00
|
|
|
DBUG_RETURN(0);
|
|
|
|
|
|
|
|
error:
|
2010-10-26 15:35:13 +04:00
|
|
|
DBUG_RETURN(res);
|
2009-12-22 15:33:21 +03:00
|
|
|
}
|
2009-12-15 10:16:46 +03:00
|
|
|
|
|
|
|
|
2010-10-26 15:35:13 +04:00
|
|
|
void DsMrr_impl::close_second_handler()
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2010-12-02 16:21:52 +03:00
|
|
|
if (secondary_file)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2013-06-05 23:53:35 +03:00
|
|
|
secondary_file->extra(HA_EXTRA_NO_KEYREAD);
|
2010-12-02 16:21:52 +03:00
|
|
|
secondary_file->ha_index_or_rnd_end();
|
2020-04-19 19:47:31 +02:00
|
|
|
secondary_file->ha_external_unlock(current_thd);
|
2011-06-27 19:07:24 +03:00
|
|
|
secondary_file->ha_close();
|
2010-12-02 16:21:52 +03:00
|
|
|
delete secondary_file;
|
|
|
|
secondary_file= NULL;
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
2009-12-22 15:33:21 +03:00
|
|
|
}
|
2009-12-15 10:16:46 +03:00
|
|
|
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
void DsMrr_impl::dsmrr_close()
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2009-12-22 15:33:21 +03:00
|
|
|
DBUG_ENTER("DsMrr_impl::dsmrr_close");
|
2020-02-20 13:35:19 +03:00
|
|
|
rowid_filter= NULL;
|
2010-10-26 15:35:13 +04:00
|
|
|
close_second_handler();
|
|
|
|
strategy= NULL;
|
|
|
|
DBUG_VOID_RETURN;
|
2009-12-22 15:33:21 +03:00
|
|
|
}
|
2009-12-15 10:16:46 +03:00
|
|
|
|
|
|
|
|
2010-06-22 21:24:22 +04:00
|
|
|
/*
|
2010-11-22 19:34:03 +03:00
|
|
|
my_qsort2-compatible static member function to compare key tuples
|
2009-12-22 15:33:21 +03:00
|
|
|
*/
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2010-12-10 20:18:12 +03:00
|
|
|
int Mrr_ordered_index_reader::compare_keys(void* arg, uchar* key1_arg,
|
|
|
|
uchar* key2_arg)
|
2009-12-22 15:33:21 +03:00
|
|
|
{
|
2010-11-22 19:34:03 +03:00
|
|
|
Mrr_ordered_index_reader *reader= (Mrr_ordered_index_reader*)arg;
|
2010-12-02 16:21:52 +03:00
|
|
|
TABLE *table= reader->file->get_table();
|
|
|
|
KEY_PART_INFO *part= table->key_info[reader->file->active_index].key_part;
|
2010-12-10 20:18:12 +03:00
|
|
|
uchar *key1, *key2;
|
|
|
|
|
2010-11-22 19:34:03 +03:00
|
|
|
if (reader->keypar.use_key_pointers)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2010-07-18 01:05:44 +04:00
|
|
|
/* the buffer stores pointers to keys, get to the keys */
|
2010-12-10 20:18:12 +03:00
|
|
|
memcpy(&key1, key1_arg, sizeof(char*));
|
|
|
|
memcpy(&key2, key2_arg, sizeof(char*));
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
2010-12-10 20:18:12 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
key1= key1_arg;
|
|
|
|
key2= key2_arg;
|
2010-07-18 01:05:44 +04:00
|
|
|
}
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2010-11-22 19:34:03 +03:00
|
|
|
return key_tuple_cmp(part, key1, key2, reader->keypar.key_tuple_length);
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-22 19:34:03 +03:00
|
|
|
int Mrr_ordered_index_reader::compare_keys_reverse(void* arg, uchar* key1,
|
|
|
|
uchar* key2)
|
2009-12-22 15:33:21 +03:00
|
|
|
{
|
2010-11-22 19:34:03 +03:00
|
|
|
return -compare_keys(arg, key1, key2);
|
2009-12-22 15:33:21 +03:00
|
|
|
}
|
2009-12-15 10:16:46 +03:00
|
|
|
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
/**
|
2010-12-02 14:10:52 +03:00
|
|
|
Set the buffer space to be shared between rowid and key buffer
|
|
|
|
|
|
|
|
@return FALSE ok
|
|
|
|
@return TRUE There is so little buffer space that we won't be able to use
|
|
|
|
the strategy.
|
|
|
|
This happens when we don't have enough space for one rowid
|
|
|
|
element and one key element so this is mainly targeted at
|
|
|
|
testing.
|
2010-08-08 11:13:54 +04:00
|
|
|
*/
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2010-12-02 14:10:52 +03:00
|
|
|
bool DsMrr_impl::setup_buffer_sharing(uint key_size_in_keybuf,
|
|
|
|
key_part_map key_tuple_map)
|
2010-08-08 11:13:54 +04:00
|
|
|
{
|
2010-12-09 00:47:33 +03:00
|
|
|
long key_buff_elem_size= key_size_in_keybuf +
|
2011-03-04 12:06:03 +03:00
|
|
|
(int)is_mrr_assoc * sizeof(range_id_t);
|
2009-12-22 15:33:21 +03:00
|
|
|
|
2010-12-02 16:21:52 +03:00
|
|
|
KEY *key_info= &primary_file->get_table()->key_info[keyno];
|
2010-08-11 14:54:34 +04:00
|
|
|
/*
|
|
|
|
Ok if we got here we need to allocate one part of the buffer
|
|
|
|
for keys and another part for rowids.
|
|
|
|
*/
|
2010-12-02 16:21:52 +03:00
|
|
|
ulonglong rowid_buf_elem_size= primary_file->ref_length +
|
2011-03-04 12:06:03 +03:00
|
|
|
(int)is_mrr_assoc * sizeof(range_id_t);
|
2009-12-22 15:33:21 +03:00
|
|
|
|
2010-08-08 11:13:54 +04:00
|
|
|
/*
|
|
|
|
Use rec_per_key statistics as a basis to find out how many rowids
|
|
|
|
we'll get for each key value.
|
|
|
|
TODO: what should be the default value to use when there is no
|
|
|
|
statistics?
|
|
|
|
*/
|
|
|
|
uint parts= my_count_bits(key_tuple_map);
|
2013-05-05 21:39:31 +03:00
|
|
|
ha_rows rpc;
|
2010-11-29 17:27:23 +03:00
|
|
|
ulonglong rowids_size= rowid_buf_elem_size;
|
2013-05-05 21:39:31 +03:00
|
|
|
if ((rpc= (ha_rows) key_info->actual_rec_per_key(parts - 1)))
|
2010-11-28 23:55:08 +03:00
|
|
|
rowids_size= rowid_buf_elem_size * rpc;
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2010-08-08 11:13:54 +04:00
|
|
|
double fraction_for_rowids=
|
2010-11-28 23:55:08 +03:00
|
|
|
(ulonglong2double(rowids_size) /
|
|
|
|
(ulonglong2double(rowids_size) + key_buff_elem_size));
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2010-12-19 13:56:12 +03:00
|
|
|
ptrdiff_t bytes_for_rowids=
|
2010-12-22 02:26:35 +03:00
|
|
|
(ptrdiff_t)floor(0.5 + fraction_for_rowids * (full_buf_end - full_buf));
|
2009-12-22 15:33:21 +03:00
|
|
|
|
2010-12-19 13:56:12 +03:00
|
|
|
ptrdiff_t bytes_for_keys= (full_buf_end - full_buf) - bytes_for_rowids;
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2017-08-03 19:28:05 +03:00
|
|
|
if (bytes_for_keys < key_buff_elem_size + 1 ||
|
|
|
|
bytes_for_rowids < (ptrdiff_t)rowid_buf_elem_size + 1)
|
|
|
|
return TRUE; /* Failed to provide minimum space for one of the buffers */
|
2010-08-08 11:13:54 +04:00
|
|
|
|
2010-08-12 21:18:41 +04:00
|
|
|
rowid_buffer_end= full_buf + bytes_for_rowids;
|
2010-09-21 20:19:54 +04:00
|
|
|
rowid_buffer.set_buffer_space(full_buf, rowid_buffer_end);
|
|
|
|
key_buffer= &backward_key_buf;
|
|
|
|
key_buffer->set_buffer_space(rowid_buffer_end, full_buf_end);
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2017-08-03 19:28:05 +03:00
|
|
|
/* The above code guarantees that the buffers are big enough */
|
|
|
|
DBUG_ASSERT(key_buffer->have_space_for(key_buff_elem_size) &&
|
|
|
|
rowid_buffer.have_space_for((size_t)rowid_buf_elem_size));
|
2010-12-02 14:10:52 +03:00
|
|
|
|
|
|
|
return FALSE;
|
2009-12-22 15:33:21 +03:00
|
|
|
}
|
2009-12-15 10:16:46 +03:00
|
|
|
|
|
|
|
|
2010-12-02 14:10:52 +03:00
|
|
|
void DsMrr_impl::do_nothing(void *dsmrr_arg)
|
2009-12-22 15:33:21 +03:00
|
|
|
{
|
2010-12-02 14:10:52 +03:00
|
|
|
/* Do nothing */
|
|
|
|
}
|
2009-12-15 10:16:46 +03:00
|
|
|
|
|
|
|
|
2010-12-02 14:10:52 +03:00
|
|
|
void DsMrr_impl::reset_buffer_sizes(void *dsmrr_arg)
|
|
|
|
{
|
|
|
|
DsMrr_impl *dsmrr= (DsMrr_impl*)dsmrr_arg;
|
|
|
|
dsmrr->rowid_buffer.set_buffer_space(dsmrr->full_buf,
|
|
|
|
dsmrr->rowid_buffer_end);
|
|
|
|
dsmrr->key_buffer->set_buffer_space(dsmrr->rowid_buffer_end,
|
|
|
|
dsmrr->full_buf_end);
|
2010-06-19 15:40:19 +04:00
|
|
|
}
|
2009-12-15 10:16:46 +03:00
|
|
|
|
|
|
|
|
2010-11-08 15:15:50 +03:00
|
|
|
/*
|
2010-09-28 12:19:50 +04:00
|
|
|
Take unused space from the key buffer and give it to the rowid buffer
|
2009-12-15 10:16:46 +03:00
|
|
|
*/
|
2010-11-08 15:15:50 +03:00
|
|
|
|
2010-12-02 14:10:52 +03:00
|
|
|
void DsMrr_impl::redistribute_buffer_space(void *dsmrr_arg)
|
2010-09-20 13:02:17 +04:00
|
|
|
{
|
2010-12-02 14:10:52 +03:00
|
|
|
DsMrr_impl *dsmrr= (DsMrr_impl*)dsmrr_arg;
|
2010-09-20 13:02:17 +04:00
|
|
|
uchar *unused_start, *unused_end;
|
2010-12-02 14:10:52 +03:00
|
|
|
dsmrr->key_buffer->remove_unused_space(&unused_start, &unused_end);
|
|
|
|
dsmrr->rowid_buffer.grow(unused_start, unused_end);
|
2009-12-22 15:33:21 +03:00
|
|
|
}
|
2009-12-15 10:16:46 +03:00
|
|
|
|
|
|
|
|
2010-11-08 15:15:50 +03:00
|
|
|
/*
|
|
|
|
@brief Initialize the iterator
|
|
|
|
|
|
|
|
@note
|
|
|
|
Initialize the iterator to produce matches for the key of the first element
|
|
|
|
in owner_arg->key_buffer
|
|
|
|
|
|
|
|
@retval 0 OK
|
|
|
|
@retval HA_ERR_END_OF_FILE Either the owner->key_buffer is empty or
|
|
|
|
no matches for the key we've tried (check
|
|
|
|
key_buffer->is_empty() to tell these apart)
|
|
|
|
@retval other code Fatal error
|
2009-12-15 10:16:46 +03:00
|
|
|
*/
|
|
|
|
|
2010-11-08 15:15:50 +03:00
|
|
|
int Key_value_records_iterator::init(Mrr_ordered_index_reader *owner_arg)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2009-12-22 15:33:21 +03:00
|
|
|
int res;
|
2010-10-26 15:35:13 +04:00
|
|
|
owner= owner_arg;
|
2010-10-03 14:48:42 +04:00
|
|
|
|
2010-10-26 15:35:13 +04:00
|
|
|
identical_key_it.init(owner->key_buffer);
|
2010-12-16 21:18:35 +03:00
|
|
|
owner->key_buffer->setup_reading(owner->keypar.key_size_in_keybuf,
|
|
|
|
owner->is_mrr_assoc ? sizeof(void*) : 0);
|
2010-10-26 15:35:13 +04:00
|
|
|
|
2010-10-03 14:48:42 +04:00
|
|
|
if (identical_key_it.read())
|
2010-11-08 15:15:50 +03:00
|
|
|
return HA_ERR_END_OF_FILE;
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2010-12-16 21:18:35 +03:00
|
|
|
uchar *key_in_buf= last_identical_key_ptr= identical_key_it.read_ptr1;
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2010-12-16 21:18:35 +03:00
|
|
|
uchar *index_tuple= key_in_buf;
|
2010-10-26 15:35:13 +04:00
|
|
|
if (owner->keypar.use_key_pointers)
|
2010-12-16 21:18:35 +03:00
|
|
|
memcpy(&index_tuple, key_in_buf, sizeof(char*));
|
2009-12-22 15:33:21 +03:00
|
|
|
|
2010-10-03 14:48:42 +04:00
|
|
|
/* Check out how many more identical keys are following */
|
|
|
|
while (!identical_key_it.read())
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2010-12-19 13:56:12 +03:00
|
|
|
if (Mrr_ordered_index_reader::compare_keys(owner, key_in_buf,
|
2010-12-16 21:18:35 +03:00
|
|
|
identical_key_it.read_ptr1))
|
2010-10-03 14:48:42 +04:00
|
|
|
break;
|
2010-12-16 21:18:35 +03:00
|
|
|
last_identical_key_ptr= identical_key_it.read_ptr1;
|
2010-10-03 14:48:42 +04:00
|
|
|
}
|
2010-10-26 15:35:13 +04:00
|
|
|
identical_key_it.init(owner->key_buffer);
|
2010-12-02 16:21:52 +03:00
|
|
|
res= owner->file->ha_index_read_map(owner->file->get_table()->record[0],
|
2010-12-16 21:18:35 +03:00
|
|
|
index_tuple,
|
|
|
|
owner->keypar.key_tuple_map,
|
|
|
|
HA_READ_KEY_EXACT);
|
2010-10-03 14:48:42 +04:00
|
|
|
|
|
|
|
if (res)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2010-11-22 19:34:03 +03:00
|
|
|
/* Failed to find any matching records */
|
|
|
|
move_to_next_key_value();
|
2010-11-08 15:15:50 +03:00
|
|
|
return res;
|
2010-10-03 14:48:42 +04:00
|
|
|
}
|
2010-12-09 00:47:33 +03:00
|
|
|
owner->have_saved_rowid= FALSE;
|
2010-10-03 14:48:42 +04:00
|
|
|
get_next_row= FALSE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-28 20:20:09 +04:00
|
|
|
|
2011-03-04 12:06:03 +03:00
|
|
|
int Key_value_records_iterator::get_next(range_id_t *range_info)
|
2010-09-28 20:20:09 +04:00
|
|
|
{
|
2010-10-03 14:48:42 +04:00
|
|
|
int res;
|
|
|
|
|
|
|
|
if (get_next_row)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2010-10-26 15:35:13 +04:00
|
|
|
if (owner->keypar.index_ranges_unique)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2010-11-22 19:34:03 +03:00
|
|
|
/* We're using a full unique key, no point to call index_next_same */
|
|
|
|
return HA_ERR_END_OF_FILE;
|
2009-12-22 15:33:21 +03:00
|
|
|
}
|
2010-10-26 15:35:13 +04:00
|
|
|
|
2010-12-02 16:21:52 +03:00
|
|
|
handler *h= owner->file;
|
2014-06-06 21:28:42 +04:00
|
|
|
uchar *lookup_key;
|
|
|
|
if (owner->keypar.use_key_pointers)
|
|
|
|
memcpy(&lookup_key, identical_key_it.read_ptr1, sizeof(void*));
|
|
|
|
else
|
|
|
|
lookup_key= identical_key_it.read_ptr1;
|
|
|
|
|
2010-10-26 15:35:13 +04:00
|
|
|
if ((res= h->ha_index_next_same(h->get_table()->record[0],
|
2014-06-06 21:28:42 +04:00
|
|
|
lookup_key,
|
2010-10-26 15:35:13 +04:00
|
|
|
owner->keypar.key_tuple_length)))
|
2009-12-22 15:33:21 +03:00
|
|
|
{
|
2010-11-08 15:15:50 +03:00
|
|
|
/* It's either HA_ERR_END_OF_FILE or some other error */
|
|
|
|
return res;
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
2010-10-26 15:35:13 +04:00
|
|
|
identical_key_it.init(owner->key_buffer);
|
2010-12-09 00:47:33 +03:00
|
|
|
owner->have_saved_rowid= FALSE;
|
2010-10-04 00:37:30 +04:00
|
|
|
get_next_row= FALSE;
|
2010-10-03 14:48:42 +04:00
|
|
|
}
|
2009-12-22 15:33:21 +03:00
|
|
|
|
2010-11-08 15:15:50 +03:00
|
|
|
identical_key_it.read(); /* This gets us next range_id */
|
2011-03-04 12:06:03 +03:00
|
|
|
memcpy(range_info, identical_key_it.read_ptr2, sizeof(range_id_t));
|
2009-12-22 15:33:21 +03:00
|
|
|
|
2010-12-16 21:18:35 +03:00
|
|
|
if (!last_identical_key_ptr ||
|
|
|
|
(identical_key_it.read_ptr1 == last_identical_key_ptr))
|
2009-12-22 15:33:21 +03:00
|
|
|
{
|
2010-11-22 19:34:03 +03:00
|
|
|
/*
|
|
|
|
We've reached the last of the identical keys that current record is a
|
|
|
|
match for. Set get_next_row=TRUE so that we read the next index record
|
|
|
|
on the next call to this function.
|
|
|
|
*/
|
2010-10-03 14:48:42 +04:00
|
|
|
get_next_row= TRUE;
|
2010-09-28 20:20:09 +04:00
|
|
|
}
|
2010-10-03 14:48:42 +04:00
|
|
|
return 0;
|
2010-09-28 20:20:09 +04:00
|
|
|
}
|
2009-12-22 15:33:21 +03:00
|
|
|
|
2010-11-08 15:15:50 +03:00
|
|
|
|
2010-11-22 19:34:03 +03:00
|
|
|
void Key_value_records_iterator::move_to_next_key_value()
|
2010-10-03 14:48:42 +04:00
|
|
|
{
|
2010-10-26 15:35:13 +04:00
|
|
|
while (!owner->key_buffer->read() &&
|
2010-12-16 21:18:35 +03:00
|
|
|
(owner->key_buffer->read_ptr1 != last_identical_key_ptr)) {}
|
2010-09-28 20:20:09 +04:00
|
|
|
}
|
|
|
|
|
2010-10-04 10:31:40 +04:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
/**
|
2010-09-28 12:19:50 +04:00
|
|
|
DS-MRR implementation: multi_range_read_next() function.
|
|
|
|
|
|
|
|
Calling convention is like multi_range_read_next() has.
|
2009-12-15 10:16:46 +03:00
|
|
|
*/
|
|
|
|
|
2011-03-04 12:06:03 +03:00
|
|
|
int DsMrr_impl::dsmrr_next(range_id_t *range_info)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2010-11-01 13:52:10 +03:00
|
|
|
int res;
|
2010-11-26 00:30:39 +03:00
|
|
|
if (strategy_exhausted)
|
|
|
|
return HA_ERR_END_OF_FILE;
|
|
|
|
|
2010-11-01 13:52:10 +03:00
|
|
|
while ((res= strategy->get_next(range_info)) == HA_ERR_END_OF_FILE)
|
2009-12-22 15:33:21 +03:00
|
|
|
{
|
2010-11-25 14:35:21 +03:00
|
|
|
if ((res= strategy->refill_buffer(FALSE)))
|
2010-11-01 13:52:10 +03:00
|
|
|
break; /* EOF or error */
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
2009-12-22 15:33:21 +03:00
|
|
|
return res;
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
/**
|
|
|
|
DS-MRR implementation: multi_range_read_info() function
|
|
|
|
*/
|
2010-06-19 15:40:19 +04:00
|
|
|
ha_rows DsMrr_impl::dsmrr_info(uint keyno, uint n_ranges, uint rows,
|
|
|
|
uint key_parts,
|
2012-08-01 17:27:34 +03:00
|
|
|
uint *bufsz, uint *flags, Cost_estimate *cost)
|
2009-12-22 15:33:21 +03:00
|
|
|
{
|
2012-08-14 19:59:28 +03:00
|
|
|
ha_rows res __attribute__((unused));
|
2009-12-22 15:33:21 +03:00
|
|
|
uint def_flags= *flags;
|
|
|
|
uint def_bufsz= *bufsz;
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
/* Get cost/flags/mem_usage of default MRR implementation */
|
2010-12-02 16:21:52 +03:00
|
|
|
res= primary_file->handler::multi_range_read_info(keyno, n_ranges, rows,
|
|
|
|
key_parts, &def_bufsz,
|
|
|
|
&def_flags, cost);
|
2009-12-22 15:33:21 +03:00
|
|
|
DBUG_ASSERT(!res);
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
if ((*flags & HA_MRR_USE_DEFAULT_IMPL) ||
|
2011-04-02 14:04:45 +04:00
|
|
|
choose_mrr_impl(keyno, rows, flags, bufsz, cost))
|
2009-12-22 15:33:21 +03:00
|
|
|
{
|
2020-12-20 21:07:38 +02:00
|
|
|
/* Default implementation is chosen */
|
|
|
|
DBUG_PRINT("info", ("Default MRR implementation chosen"));
|
2009-12-22 15:33:21 +03:00
|
|
|
*flags= def_flags;
|
|
|
|
*bufsz= def_bufsz;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* *flags and *bufsz were set by choose_mrr_impl */
|
2020-12-20 21:07:38 +02:00
|
|
|
DBUG_PRINT("info", ("DS-MRR implementation chosen"));
|
2009-12-22 15:33:21 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2009-12-15 10:16:46 +03:00
|
|
|
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
/**
|
|
|
|
DS-MRR Implementation: multi_range_read_info_const() function
|
2009-12-15 10:16:46 +03:00
|
|
|
*/
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
ha_rows DsMrr_impl::dsmrr_info_const(uint keyno, RANGE_SEQ_IF *seq,
|
2022-09-23 14:48:13 +03:00
|
|
|
void *seq_init_param, uint n_ranges,
|
|
|
|
uint *bufsz, uint *flags, ha_rows limit,
|
|
|
|
Cost_estimate *cost)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2009-12-22 15:33:21 +03:00
|
|
|
ha_rows rows;
|
|
|
|
uint def_flags= *flags;
|
|
|
|
uint def_bufsz= *bufsz;
|
|
|
|
/* Get cost/flags/mem_usage of default MRR implementation */
|
2010-12-02 16:21:52 +03:00
|
|
|
rows= primary_file->handler::multi_range_read_info_const(keyno, seq,
|
|
|
|
seq_init_param,
|
|
|
|
n_ranges,
|
|
|
|
&def_bufsz,
|
2022-09-23 14:48:13 +03:00
|
|
|
&def_flags,
|
|
|
|
limit,
|
|
|
|
cost);
|
2009-12-22 15:33:21 +03:00
|
|
|
if (rows == HA_POS_ERROR)
|
|
|
|
{
|
|
|
|
/* Default implementation can't perform MRR scan => we can't either */
|
|
|
|
return rows;
|
|
|
|
}
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
/*
|
|
|
|
If HA_MRR_USE_DEFAULT_IMPL has been passed to us, that is an order to
|
|
|
|
use the default MRR implementation (we need it for UPDATE/DELETE).
|
2011-06-03 00:25:58 +04:00
|
|
|
Otherwise, make a choice based on cost and @@optimizer_switch settings
|
2009-12-22 15:33:21 +03:00
|
|
|
*/
|
|
|
|
if ((*flags & HA_MRR_USE_DEFAULT_IMPL) ||
|
|
|
|
choose_mrr_impl(keyno, rows, flags, bufsz, cost))
|
|
|
|
{
|
2020-12-20 21:07:38 +02:00
|
|
|
DBUG_PRINT("info", ("Default MRR implementation chosen"));
|
2009-12-22 15:33:21 +03:00
|
|
|
*flags= def_flags;
|
|
|
|
*bufsz= def_bufsz;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* *flags and *bufsz were set by choose_mrr_impl */
|
2020-12-20 21:07:38 +02:00
|
|
|
DBUG_PRINT("info", ("DS-MRR implementation chosen"));
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
2009-12-22 15:33:21 +03:00
|
|
|
return rows;
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
/**
|
|
|
|
Check if key has partially-covered columns
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
We can't use DS-MRR to perform range scans when the ranges are over
|
|
|
|
partially-covered keys, because we'll not have full key part values
|
|
|
|
(we'll have their prefixes from the index) and will not be able to check
|
|
|
|
if we've reached the end the range.
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
@param keyno Key to check
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
@todo
|
|
|
|
Allow use of DS-MRR in cases where the index has partially-covered
|
|
|
|
components but they are not used for scanning.
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
@retval TRUE Yes
|
|
|
|
@retval FALSE No
|
|
|
|
*/
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2013-09-20 14:47:38 +04:00
|
|
|
bool key_uses_partial_cols(TABLE_SHARE *share, uint keyno)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2013-09-20 14:47:38 +04:00
|
|
|
KEY_PART_INFO *kp= share->key_info[keyno].key_part;
|
2013-09-26 21:20:15 +03:00
|
|
|
KEY_PART_INFO *kp_end= kp + share->key_info[keyno].user_defined_key_parts;
|
2009-12-22 15:33:21 +03:00
|
|
|
for (; kp != kp_end; kp++)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2009-12-22 15:33:21 +03:00
|
|
|
if (!kp->field->part_of_key.is_set(keyno))
|
|
|
|
return TRUE;
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
2009-12-22 15:33:21 +03:00
|
|
|
return FALSE;
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
|
|
|
|
2010-06-22 21:24:22 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
Check if key/flags allow DS-MRR/CPK strategy to be used
|
|
|
|
|
2010-09-28 12:19:50 +04:00
|
|
|
@param thd
|
|
|
|
@param keyno Index that will be used
|
|
|
|
@param mrr_flags
|
2010-06-22 21:24:22 +04:00
|
|
|
|
2010-09-28 12:19:50 +04:00
|
|
|
@retval TRUE DS-MRR/CPK should be used
|
|
|
|
@retval FALSE Otherwise
|
2010-06-22 21:24:22 +04:00
|
|
|
*/
|
|
|
|
|
2013-09-20 14:47:38 +04:00
|
|
|
bool DsMrr_impl::check_cpk_scan(THD *thd, TABLE_SHARE *share, uint keyno,
|
|
|
|
uint mrr_flags)
|
2010-06-22 21:24:22 +04:00
|
|
|
{
|
2014-02-19 14:05:15 +04:00
|
|
|
return MY_TEST((mrr_flags & HA_MRR_SINGLE_POINT) &&
|
2020-02-26 14:52:23 +02:00
|
|
|
primary_file->is_clustering_key(keyno) &&
|
2014-02-19 14:05:15 +04:00
|
|
|
optimizer_flag(thd, OPTIMIZER_SWITCH_MRR_SORT_KEYS));
|
2010-06-22 21:24:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2009-12-22 15:33:21 +03:00
|
|
|
DS-MRR Internals: Choose between Default MRR implementation and DS-MRR
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
Make the choice between using Default MRR implementation and DS-MRR.
|
|
|
|
This function contains common functionality factored out of dsmrr_info()
|
|
|
|
and dsmrr_info_const(). The function assumes that the default MRR
|
|
|
|
implementation's applicability requirements are satisfied.
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
@param keyno Index number
|
|
|
|
@param rows E(full rows to be retrieved)
|
|
|
|
@param flags IN MRR flags provided by the MRR user
|
2020-12-20 21:07:38 +02:00
|
|
|
OUT If DS-MRR is chosen, flags of DS-MRR implementation
|
2009-12-22 15:33:21 +03:00
|
|
|
else the value is not modified
|
2020-12-20 21:07:38 +02:00
|
|
|
@param bufsz IN If DS-MRR is chosen, buffer use of DS-MRR implementation
|
2009-12-22 15:33:21 +03:00
|
|
|
else the value is not modified
|
|
|
|
@param cost IN Cost of default MRR implementation
|
2020-12-20 21:07:38 +02:00
|
|
|
OUT If DS-MRR is chosen, cost of DS-MRR scan
|
2009-12-22 15:33:21 +03:00
|
|
|
else the value is not modified
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
@retval TRUE Default MRR implementation should be used
|
|
|
|
@retval FALSE DS-MRR implementation should be used
|
2009-12-15 10:16:46 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
bool DsMrr_impl::choose_mrr_impl(uint keyno, ha_rows rows, uint *flags,
|
2012-08-01 17:27:34 +03:00
|
|
|
uint *bufsz, Cost_estimate *cost)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2012-08-01 17:27:34 +03:00
|
|
|
Cost_estimate dsmrr_cost;
|
2009-12-22 15:33:21 +03:00
|
|
|
bool res;
|
2015-04-28 15:20:48 +04:00
|
|
|
THD *thd= primary_file->get_table()->in_use;
|
2013-09-20 14:47:38 +04:00
|
|
|
TABLE_SHARE *share= primary_file->get_table_share();
|
2010-06-19 15:40:19 +04:00
|
|
|
|
2013-09-20 14:47:38 +04:00
|
|
|
bool doing_cpk_scan= check_cpk_scan(thd, share, keyno, *flags);
|
2020-02-26 14:52:23 +02:00
|
|
|
bool using_cpk= primary_file->is_clustering_key(keyno);
|
2011-04-02 14:04:45 +04:00
|
|
|
*flags &= ~HA_MRR_IMPLEMENTATION_FLAGS;
|
2011-06-03 00:25:58 +04:00
|
|
|
if (!optimizer_flag(thd, OPTIMIZER_SWITCH_MRR) ||
|
|
|
|
*flags & HA_MRR_INDEX_ONLY ||
|
2013-09-20 14:47:38 +04:00
|
|
|
(using_cpk && !doing_cpk_scan) || key_uses_partial_cols(share, keyno))
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2009-12-22 15:33:21 +03:00
|
|
|
/* Use the default implementation */
|
|
|
|
*flags |= HA_MRR_USE_DEFAULT_IMPL;
|
2011-04-02 14:04:45 +04:00
|
|
|
*flags &= ~HA_MRR_IMPLEMENTATION_FLAGS;
|
2009-12-22 15:33:21 +03:00
|
|
|
return TRUE;
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
2010-07-17 18:03:50 +04:00
|
|
|
|
2013-09-20 14:47:38 +04:00
|
|
|
uint add_len= share->key_info[keyno].key_length + primary_file->ref_length;
|
2019-11-15 23:37:28 +03:00
|
|
|
if (get_disk_sweep_mrr_cost(keyno, rows, *flags, bufsz, add_len,
|
|
|
|
&dsmrr_cost))
|
2009-12-22 15:33:21 +03:00
|
|
|
return TRUE;
|
2019-11-15 23:37:28 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
bool force_dsmrr;
|
|
|
|
/*
|
2011-06-03 00:25:58 +04:00
|
|
|
If mrr_cost_based flag is not set, then set cost of DS-MRR to be minimum of
|
2009-12-22 15:33:21 +03:00
|
|
|
DS-MRR and Default implementations cost. This allows one to force use of
|
|
|
|
DS-MRR whenever it is applicable without affecting other cost-based
|
|
|
|
choices.
|
2009-12-15 10:16:46 +03:00
|
|
|
*/
|
2011-06-03 00:25:58 +04:00
|
|
|
if ((force_dsmrr= !optimizer_flag(thd, OPTIMIZER_SWITCH_MRR_COST_BASED)) &&
|
2009-12-22 15:33:21 +03:00
|
|
|
dsmrr_cost.total_cost() > cost->total_cost())
|
|
|
|
dsmrr_cost= *cost;
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
if (force_dsmrr || dsmrr_cost.total_cost() <= cost->total_cost())
|
|
|
|
{
|
|
|
|
*flags &= ~HA_MRR_USE_DEFAULT_IMPL; /* Use the DS-MRR implementation */
|
|
|
|
*flags &= ~HA_MRR_SORTED; /* We will return unordered output */
|
|
|
|
*cost= dsmrr_cost;
|
|
|
|
res= FALSE;
|
2010-07-17 18:03:50 +04:00
|
|
|
|
2011-04-02 14:04:45 +04:00
|
|
|
|
|
|
|
if ((using_cpk && doing_cpk_scan) ||
|
|
|
|
(optimizer_flag(thd, OPTIMIZER_SWITCH_MRR_SORT_KEYS) &&
|
|
|
|
*flags & HA_MRR_SINGLE_POINT))
|
|
|
|
{
|
|
|
|
*flags |= DSMRR_IMPL_SORT_KEYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(using_cpk && doing_cpk_scan) &&
|
|
|
|
!(*flags & HA_MRR_INDEX_ONLY))
|
|
|
|
{
|
|
|
|
*flags |= DSMRR_IMPL_SORT_ROWIDS;
|
|
|
|
}
|
|
|
|
/*
|
2010-07-17 18:03:50 +04:00
|
|
|
if ((*flags & HA_MRR_SINGLE_POINT) &&
|
|
|
|
optimizer_flag(thd, OPTIMIZER_SWITCH_MRR_SORT_KEYS))
|
|
|
|
*flags |= HA_MRR_MATERIALIZED_KEYS;
|
2011-04-02 14:04:45 +04:00
|
|
|
*/
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
2009-12-22 15:33:21 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Use the default MRR implementation */
|
|
|
|
res= TRUE;
|
|
|
|
}
|
|
|
|
return res;
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
|
|
|
|
2011-04-02 14:04:45 +04:00
|
|
|
/*
|
|
|
|
Take the flags we've returned previously and print one of
|
|
|
|
- Key-ordered scan
|
|
|
|
- Rowid-ordered scan
|
|
|
|
- Key-ordered Rowid-ordered scan
|
|
|
|
*/
|
|
|
|
|
|
|
|
int DsMrr_impl::dsmrr_explain_info(uint mrr_mode, char *str, size_t size)
|
|
|
|
{
|
|
|
|
const char *key_ordered= "Key-ordered scan";
|
|
|
|
const char *rowid_ordered= "Rowid-ordered scan";
|
|
|
|
const char *both_ordered= "Key-ordered Rowid-ordered scan";
|
|
|
|
const char *used_str="";
|
|
|
|
const uint BOTH_FLAGS= (DSMRR_IMPL_SORT_KEYS | DSMRR_IMPL_SORT_ROWIDS);
|
|
|
|
|
|
|
|
if (!(mrr_mode & HA_MRR_USE_DEFAULT_IMPL))
|
|
|
|
{
|
|
|
|
if ((mrr_mode & BOTH_FLAGS) == BOTH_FLAGS)
|
|
|
|
used_str= both_ordered;
|
|
|
|
else if (mrr_mode & DSMRR_IMPL_SORT_KEYS)
|
|
|
|
used_str= key_ordered;
|
|
|
|
else if (mrr_mode & DSMRR_IMPL_SORT_ROWIDS)
|
|
|
|
used_str= rowid_ordered;
|
|
|
|
|
2018-02-06 12:55:58 +00:00
|
|
|
size_t used_str_len= strlen(used_str);
|
|
|
|
size_t copy_len= MY_MIN(used_str_len, size);
|
2013-04-06 15:51:08 +02:00
|
|
|
memcpy(str, used_str, copy_len);
|
2018-02-06 12:55:58 +00:00
|
|
|
return (int)copy_len;
|
2011-04-02 14:04:45 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2022-09-30 17:10:37 +03:00
|
|
|
static void get_sort_and_sweep_cost(TABLE *table, ha_rows nrows,
|
|
|
|
Cost_estimate *cost);
|
2009-12-15 10:16:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-22 15:33:21 +03:00
|
|
|
Get cost of DS-MRR scan
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
@param keynr Index to be used
|
|
|
|
@param rows E(Number of rows to be scanned)
|
|
|
|
@param flags Scan parameters (HA_MRR_* flags)
|
|
|
|
@param buffer_size INOUT Buffer size
|
2019-11-15 23:37:28 +03:00
|
|
|
IN: Buffer of size 0 means the function
|
|
|
|
will determine the best size and return it.
|
|
|
|
@param extra_mem_overhead Extra memory overhead of the MRR implementation
|
|
|
|
(the function assumes this many bytes of buffer
|
|
|
|
space will not be usable by DS-MRR)
|
2009-12-22 15:33:21 +03:00
|
|
|
@param cost OUT The cost
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
@retval FALSE OK
|
|
|
|
@retval TRUE Error, DS-MRR cannot be used (the buffer is too small
|
|
|
|
for even 1 rowid)
|
2009-12-15 10:16:46 +03:00
|
|
|
*/
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
bool DsMrr_impl::get_disk_sweep_mrr_cost(uint keynr, ha_rows rows, uint flags,
|
2019-11-15 23:37:28 +03:00
|
|
|
uint *buffer_size,
|
|
|
|
uint extra_mem_overhead,
|
|
|
|
Cost_estimate *cost)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2009-12-22 15:33:21 +03:00
|
|
|
ulong max_buff_entries, elem_size;
|
2011-10-18 13:44:12 +03:00
|
|
|
ha_rows rows_in_full_step;
|
|
|
|
ha_rows rows_in_last_step;
|
2009-12-22 15:33:21 +03:00
|
|
|
uint n_full_steps;
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2010-12-02 16:21:52 +03:00
|
|
|
elem_size= primary_file->ref_length +
|
2014-02-19 14:05:15 +04:00
|
|
|
sizeof(void*) * (!MY_TEST(flags & HA_MRR_NO_ASSOCIATION));
|
2009-12-22 15:33:21 +03:00
|
|
|
|
2019-11-15 23:37:28 +03:00
|
|
|
if (!*buffer_size)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
We are requested to determine how much memory we need.
|
|
|
|
Request memory to finish the scan in one pass but do not request
|
|
|
|
more than @@mrr_buff_size.
|
|
|
|
*/
|
2019-11-16 13:18:24 +03:00
|
|
|
*buffer_size= (uint) MY_MIN(extra_mem_overhead + elem_size*(ulong)rows,
|
|
|
|
MY_MAX(table->in_use->variables.mrr_buff_size,
|
|
|
|
extra_mem_overhead));
|
2019-11-15 23:37:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (elem_size + extra_mem_overhead > *buffer_size)
|
2009-12-22 15:33:21 +03:00
|
|
|
return TRUE; /* Buffer has not enough space for even 1 rowid */
|
|
|
|
|
2019-11-15 23:37:28 +03:00
|
|
|
max_buff_entries = (*buffer_size - extra_mem_overhead) / elem_size;
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
/* Number of iterations we'll make with full buffer */
|
|
|
|
n_full_steps= (uint)floor(rows2double(rows) / max_buff_entries);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Get numbers of rows we'll be processing in
|
|
|
|
- non-last sweep, with full buffer
|
|
|
|
- last iteration, with non-full buffer
|
|
|
|
*/
|
|
|
|
rows_in_full_step= max_buff_entries;
|
|
|
|
rows_in_last_step= rows % max_buff_entries;
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2022-09-30 17:10:37 +03:00
|
|
|
cost->reset(primary_file);
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
/* Adjust buffer size if we expect to use only part of the buffer */
|
|
|
|
if (n_full_steps)
|
|
|
|
{
|
2011-10-18 13:44:12 +03:00
|
|
|
get_sort_and_sweep_cost(table, rows_in_full_step, cost);
|
2009-12-22 15:33:21 +03:00
|
|
|
cost->multiply(n_full_steps);
|
|
|
|
}
|
|
|
|
else
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2021-10-06 12:31:19 +03:00
|
|
|
*buffer_size= ((uint) MY_MAX(*buffer_size,
|
|
|
|
(size_t)(1.2*rows_in_last_step) * elem_size +
|
|
|
|
primary_file->ref_length +
|
|
|
|
table->key_info[keynr].key_length));
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
2009-12-22 15:33:21 +03:00
|
|
|
|
2012-08-01 17:27:34 +03:00
|
|
|
Cost_estimate last_step_cost;
|
2022-09-30 17:10:37 +03:00
|
|
|
last_step_cost.avg_io_cost= cost->avg_io_cost;
|
2009-12-22 15:33:21 +03:00
|
|
|
get_sort_and_sweep_cost(table, rows_in_last_step, &last_step_cost);
|
|
|
|
cost->add(&last_step_cost);
|
|
|
|
|
|
|
|
/* Total cost of all index accesses */
|
2022-09-30 17:10:37 +03:00
|
|
|
cost->index_cost= primary_file->ha_keyread_and_copy_time(keynr, 1, rows, 0);
|
2023-06-06 16:50:57 +03:00
|
|
|
cost->comp_cost= rows2double(rows) * primary_file->WHERE_COST;
|
|
|
|
cost->setup_cost= primary_file->MULTI_RANGE_READ_SETUP_COST;
|
2009-12-22 15:33:21 +03:00
|
|
|
return FALSE;
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
/*
|
|
|
|
Get cost of one sort-and-sweep step
|
2010-09-28 12:19:50 +04:00
|
|
|
|
|
|
|
It consists of two parts:
|
|
|
|
- sort an array of #nrows ROWIDs using qsort
|
|
|
|
- read #nrows records from table in a sweep.
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2010-09-28 12:19:50 +04:00
|
|
|
@param table Table being accessed
|
|
|
|
@param nrows Number of rows to be sorted and retrieved
|
|
|
|
@param cost OUT The cost of scan
|
2009-12-15 10:16:46 +03:00
|
|
|
*/
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
static
|
2012-08-01 17:27:34 +03:00
|
|
|
void get_sort_and_sweep_cost(TABLE *table, ha_rows nrows, Cost_estimate *cost)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2009-12-22 15:33:21 +03:00
|
|
|
if (nrows)
|
|
|
|
{
|
|
|
|
get_sweep_read_cost(table, nrows, FALSE, cost);
|
|
|
|
/* Add cost of qsort call: n * log2(n) * cost(rowid_comparison) */
|
2022-04-05 20:12:29 +03:00
|
|
|
double cmp_op= rows2double(nrows) * ROWID_COMPARE_COST_THD(table->in_use);
|
2009-12-22 15:33:21 +03:00
|
|
|
if (cmp_op < 3)
|
|
|
|
cmp_op= 3;
|
|
|
|
cost->cpu_cost += cmp_op * log2(cmp_op);
|
|
|
|
}
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-22 15:33:21 +03:00
|
|
|
Get cost of reading nrows table records in a "disk sweep"
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
@param table Table to be accessed
|
|
|
|
@param nrows Number of rows to retrieve
|
|
|
|
@param interrupted TRUE <=> Assume that the disk sweep will be
|
|
|
|
interrupted by other disk IO. FALSE - otherwise.
|
|
|
|
@param cost OUT The cost.
|
2009-12-15 10:16:46 +03:00
|
|
|
*/
|
|
|
|
|
2022-09-30 17:10:37 +03:00
|
|
|
static void get_sweep_read_cost(TABLE *table, ha_rows nrows, bool interrupted,
|
|
|
|
Cost_estimate *cost)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2009-12-22 15:33:21 +03:00
|
|
|
DBUG_ENTER("get_sweep_read_cost");
|
2009-12-15 10:16:46 +03:00
|
|
|
|
Update row and key fetch cost models to take into account data copy costs
Before this patch, when calculating the cost of fetching and using a
row/key from the engine, we took into account the cost of finding a
row or key from the engine, but did not consistently take into account
index only accessed, clustered key or covered keys for all access
paths.
The cost of the WHERE clause (TIME_FOR_COMPARE) was not consistently
considered in best_access_path(). TIME_FOR_COMPARE was used in
calculation in other places, like greedy_search(), but was in some
cases (like scans) done an a different number of rows than was
accessed.
The cost calculation of row and index scans didn't take into account
the number of rows that where accessed, only the number of accepted
rows.
When using a filter, the cost of index_only_reads and cost of
accessing and disregarding 'filtered rows' where not taken into
account, which made filters cost less than there actually where.
To remedy the above, the following key & row fetch related costs
has been added:
- The cost of fetching and using a row is now split into different costs:
- key + Row fetch cost (as before) but multiplied with the variable
'optimizer_cache_cost' (default to 0.5). This allows the user to
tell the optimizer the likehood of finding the key and row in the
engine cache.
- ROW_COPY_COST, The cost copying a row from the engine to the
sql layer or creating a row from the join_cache to the record
buffer. Mostly affects table scan costs.
- ROW_LOOKUP_COST, the cost of fetching a row by rowid.
- KEY_COPY_COST the cost of finding the next key and copying it from
the engine to the SQL layer. This is used when we calculate the cost
index only reads. It makes index scans more expensive than before if
they cover a lot of rows. (main.index_merge_myisam)
- KEY_LOOKUP_COST, the cost of finding the first key in a range.
This replaces the old define IDX_LOOKUP_COST, but with a higher cost.
- KEY_NEXT_FIND_COST, the cost of finding the next key (and rowid).
when doing a index scan and comparing the rowid to the filter.
Before this cost was assumed to be 0.
All of the above constants/variables are now tuned to be somewhat in
proportion of executing complexity to each other. There is tuning
need for these in the future, but that can wait until the above are
made user variables as that will make tuning much easier.
To make the usage of the above easy, there are new (not virtual)
cost calclation functions in handler:
- ha_read_time(), like read_time(), but take optimizer_cache_cost into
account.
- ha_read_and_copy_time(), like ha_read_time() but take into account
ROW_COPY_TIME
- ha_read_and_compare_time(), like ha_read_and_copy_time() but take
TIME_FOR_COMPARE into account.
- ha_rnd_pos_time(). Read row with row id, taking ROW_COPY_COST
into account. This is used with filesort where we don't need
to execute the WHERE clause again.
- ha_keyread_time(), like keyread_time() but take
optimizer_cache_cost into account.
- ha_keyread_and_copy_time(), like ha_keyread_time(), but add
KEY_COPY_COST.
- ha_key_scan_time(), like key_scan_time() but take
optimizer_cache_cost nto account.
- ha_key_scan_and_compare_time(), like ha_key_scan_time(), but add
KEY_COPY_COST & TIME_FOR_COMPARE.
I also added some setup costs for doing different types of scans and
creating temporary tables (on disk and in memory). This encourages
the optimizer to not use these for simple 'a few row' lookups if
there are adequate key lookup strategies.
- TABLE_SCAN_SETUP_COST, cost of starting a table scan.
- INDEX_SCAN_SETUP_COST, cost of starting an index scan.
- HEAP_TEMPTABLE_CREATE_COST, cost of creating in memory
temporary table.
- DISK_TEMPTABLE_CREATE_COST, cost of creating an on disk temporary
table.
When calculating cost of fetching ranges, we had a cost of
IDX_LOOKUP_COST (0.125) for doing a key div for a new range. This is
now replaced with 'io_cost * KEY_LOOKUP_COST (1.0) *
optimizer_cache_cost', which matches the cost we use for 'ref' and
other key lookups. The effect is that the cost is now a bit higher
when we have many ranges for a key.
Allmost all calculation with TIME_FOR_COMPARE is now done in
best_access_path(). 'JOIN::read_time' now includes the full
cost for finding the rows in the table.
In the result files, many of the changes are now again close to what
they where before the "Update cost for hash and cached joins" commit,
as that commit didn't fix the filter cost (too complex to do
everything in one commit).
The above changes showed a lot of a lot of inconsistencies in
optimizer cost calculation. The main objective with the other changes
was to do calculation as similar (and accurate) as possible and to make
different plans more comparable.
Detailed list of changes:
- Calculate index_only_cost consistently and correctly for all scan
and ref accesses. The row fetch_cost and index_only_cost now
takes into account clustered keys, covered keys and index
only accesses.
- cost_for_index_read now returns both full cost and index_only_cost
- Fixed cost calculation of get_sweep_read_cost() to match other
similar costs. This is bases on the assumption that data is more
often stored on SSD than a hard disk.
- Replaced constant 2.0 with new define TABLE_SCAN_SETUP_COST.
- Some scan cost estimates did not take into account
TIME_FOR_COMPARE. Now all scan costs takes this into
account. (main.show_explain)
- Added session variable optimizer_cache_hit_ratio (default 50%). By
adjusting this on can reduce or increase the cost of index or direct
record lookups. The effect of the default is that key lookups is now
a bit cheaper than before. See usage of 'optimizer_cache_cost' in
handler.h.
- JOIN_TAB::scan_time() did not take into account index only scans,
which produced a wrong cost when index scan was used. Changed
JOIN_TAB:::scan_time() to take into consideration clustered and
covered keys. The values are now cached and we only have to call
this function once. Other calls are changed to use the cached
values. Function renamed to JOIN_TAB::estimate_scan_time().
- Fixed that most index cost calculations are done the same way and
more close to 'range' calculations. The cost is now lower than
before for small data sets and higher for large data sets as we take
into account how many keys are read (main.opt_trace_selectivity,
main.limit_rows_examined).
- Ensured that index_scan_cost() ==
range(scan_of_all_rows_in_table_using_one_range) +
MULTI_RANGE_READ_INFO_CONST. One effect of this is that if there
is choice of doing a full index scan and a range-index scan over
almost the whole table then index scan will be preferred (no
range-read setup cost). (innodb.innodb, main.show_explain,
main.range)
- Fixed the EQ_REF and REF takes into account clustered and covered
keys. This changes some plans to use covered or clustered indexes
as these are much cheaper. (main.subselect_mat_cost,
main.state_tables_innodb, main.limit_rows_examined)
- Rowid filter setup cost and filter compare cost now takes into
account fetching and checking the rowid (KEY_NEXT_FIND_COST).
(main.partition_pruning heap.heap_btree main.log_state)
- Added KEY_NEXT_FIND_COST to
Range_rowid_filter_cost_info::lookup_cost to account of the time
to find and check the next key value against the container
- Introduced ha_keyread_time(rows) that takes into account finding
the next row and copying the key value to 'record'
(KEY_COPY_COST).
- Introduced ha_key_scan_time() for calculating an index scan over
all rows.
- Added IDX_LOOKUP_COST to keyread_time() as a startup cost.
- Added index_only_fetch_cost() as a convenience function to
OPT_RANGE.
- keyread_time() cost is slightly reduced to prefer shorter keys.
(main.index_merge_myisam)
- All of the above caused some index_merge combinations to be
rejected because of cost (main.index_intersect). In some cases
'ref' where replaced with index_merge because of the low
cost calculation of get_sweep_read_cost().
- Some index usage moved from PRIMARY to a covering index.
(main.subselect_innodb)
- Changed cost calculation of filter to take KEY_LOOKUP_COST and
TIME_FOR_COMPARE into account. See sql_select.cc::apply_filter().
filter parameters and costs are now written to optimizer_trace.
- Don't use matchings_records_in_range() to try to estimate the number
of filtered rows for ranges. The reason is that we want to ensure
that 'range' is calculated similar to 'ref'. There is also more work
needed to calculate the selectivity when using ranges and ranges and
filtering. This causes filtering column in EXPLAIN EXTENDED to be
100.00 for some cases where range cannot use filtering.
(main.rowid_filter)
- Introduced ha_scan_time() that takes into account the CPU cost of
finding the next row and copying the row from the engine to
'record'. This causes costs of table scan to slightly increase and
some test to changed their plan from ALL to RANGE or ALL to ref.
(innodb.innodb_mysql, main.select_pkeycache)
In a few cases where scan time of very small tables have lower cost
than a ref or range, things changed from ref/range to ALL.
(main.myisam, main.func_group, main.limit_rows_examined,
main.subselect2)
- Introduced ha_scan_and_compare_time() which is like ha_scan_time()
but also adds the cost of the where clause (TIME_FOR_COMPARE).
- Added small cost for creating temporary table for
materialization. This causes some very small tables to use scan
instead of materialization.
- Added checking of the WHERE clause (TIME_FOR_COMPARE) of the
accepted rows to ROR costs in get_best_ror_intersect()
- Removed '- 0.001' from 'join->best_read' and optimize_straight_join()
to ensure that the 'Last_query_cost' status variable contains the
same value as the one that was calculated by the optimizer.
- Take avg_io_cost() into account in handler::keyread_time() and
handler::read_time(). This should have no effect as it's 1.0 by
default, except for heap that overrides these functions.
- Some 'ref_or_null' accesses changed to 'range' because of cost
adjustments (main.order_by)
- Added scan type "scan_with_join_cache" for optimizer_trace. This is
just to show in the trace what kind of scan was used.
- When using 'scan_with_join_cache' take into account number of
preceding tables (as have to restore all fields for all previous
table combination when checking the where clause)
The new cost added is:
(row_combinations * ROW_COPY_COST * number_of_cached_tables).
This increases the cost of join buffering in proportion of the
number of tables in the join buffer. One effect is that full scans
are now done earlier as the cost is then smaller.
(main.join_outer_innodb, main.greedy_optimizer)
- Removed the usage of 'worst_seeks' in cost_for_index_read as it
caused wrong plans to be created; It prefered JT_EQ_REF even if it
would be much more expensive than a full table scan. A related
issue was that worst_seeks only applied to full lookup, not to
clustered or index only lookups, which is not consistent. This
caused some plans to use index scan instead of eq_ref (main.union)
- Changed federated block size from 4096 to 1500, which is the
typical size of an IO packet.
- Added costs for reading rows to Federated. Needed as there is no
caching of rows in the federated engine.
- Added ha_innobase::rnd_pos_time() cost function.
- A lot of extra things added to optimizer trace
- More costs, especially for materialization and index_merge.
- Make lables more uniform
- Fixed a lot of minor bugs
- Added 'trace_started()' around a lot of trace blocks.
- When calculating ORDER BY with LIMIT cost for using an index
the cost did not take into account the number of row retrivals
that has to be done or the cost of comparing the rows with the
WHERE clause. The cost calculated would be just a fraction of
the real cost. Now we calculate the cost as we do for ranges
and 'ref'.
- 'Using index for group-by' is used a bit more than before as
now take into account the WHERE clause cost when comparing
with 'ref' and prefer the method with fewer row combinations.
(main.group_min_max).
Bugs fixed:
- Fixed that we don't calculate TIME_FOR_COMPARE twice for some plans,
like in optimize_straight_join() and greedy_search()
- Fixed bug in save_explain_data where we could test for the wrong
index when displaying 'Using index'. This caused some old plans to
show 'Using index'. (main.subselect_innodb, main.subselect2)
- Fixed bug in get_best_ror_intersect() where 'min_cost' was not
updated, and the cost we compared with was not the one that was
used.
- Fixed very wrong cost calculation for priority queues in
check_if_pq_applicable(). (main.order_by now correctly uses priority
queue)
- When calculating cost of EQ_REF or REF, we added the cost of
comparing the WHERE clause with the found rows, not all row
combinations. This made ref and eq_ref to be regarded way to cheap
compared to other access methods.
- FORCE INDEX cost calculation didn't take into account clustered or
covered indexes.
- JT_EQ_REF cost was estimated as avg_io_cost(), which is half the
cost of a JT_REF key. This may be true for InnoDB primary key, but
not for other unique keys or other engines. Now we use handler
function to calculate the cost, which allows us to handle
consistently clustered, covered keys and not covered keys.
- ha_start_keyread() didn't call extra_opt() if keyread was already
enabled but still changed the 'keyread' variable (which is wrong).
Fixed by not doing anything if keyread is already enabled.
- multi_range_read_info_cost() didn't take into account io_cost when
calculating the cost of ranges.
- fix_semijoin_strategies_for_picked_join_order() used the wrong
record_count when calling best_access_path() for SJ_OPT_FIRST_MATCH
and SJ_OPT_LOOSE_SCAN.
- Hash joins didn't provide correct best_cost to the upper level, which
means that the cost for hash_joins more expensive than calculated
in best_access_path (a difference of 10x * TIME_OF_COMPARE).
This is fixed in the new code thanks to that we now include
TIME_OF_COMPARE cost in 'read_time'.
Other things:
- Added some 'if (thd->trace_started())' to speed up code
- Removed not used function Cost_estimate::is_zero()
- Simplified testing of HA_POS_ERROR in get_best_ror_intersect().
(No cost changes)
- Moved ha_start_keyread() from join_read_const_table() to join_read_const()
to enable keyread for all types of JT_CONST tables.
- Made a few very short functions inline in handler.h
Notes:
- In main.rowid_filter the join order of order and lineitem is swapped.
This is because the cost of doing a range fetch of lineitem(98 rows) is
almost as big as the whole join of order,lineitem. The filtering will
also ensure that we only have to do very small key fetches of the rows
in lineitem.
- main.index_merge_myisam had a few changes where we are now using
less keys for index_merge. This is because index scans are now more
expensive than before.
- handler->optimizer_cache_cost is updated in ha_external_lock().
This ensures that it is up to date per statements.
Not an optimal solution (for locked tables), but should be ok for now.
- 'DELETE FROM t1 WHERE t1.a > 0 ORDER BY t1.a' does not take cost of
filesort into consideration when table scan is chosen.
(main.myisam_explain_non_select_all)
- perfschema.table_aggregate_global_* has changed because an update
on a table with 1 row will now use table scan instead of key lookup.
TODO in upcomming commits:
- Fix selectivity calculation for ranges with and without filtering and
when there is a ref access but scan is chosen.
For this we have to store the lowest known value for
'accepted_records' in the OPT_RANGE structure.
- Change that records_read does not include filtered rows.
- test_if_cheaper_ordering() needs to be updated to properly calculate
costs. This will fix tests like main.order_by_innodb,
main.single_delete_update
- Extend get_range_limit_read_cost() to take into considering
cost_for_index_read() if there where no quick keys. This will reduce
the computed cost for ORDER BY with LIMIT in some cases.
(main.innodb_ext_key)
- Fix that we take into account selectivity when counting the number
of rows we have to read when considering using a index table scan to
resolve ORDER BY.
- Add new calculation for rnd_pos_time() where we take into account the
benefit of reading multiple rows from the same page.
2021-11-01 12:34:24 +02:00
|
|
|
#ifndef OLD_SWEEP_COST
|
2022-09-30 17:10:37 +03:00
|
|
|
cost->row_cost= table->file->ha_rnd_pos_call_time(nrows);
|
Update row and key fetch cost models to take into account data copy costs
Before this patch, when calculating the cost of fetching and using a
row/key from the engine, we took into account the cost of finding a
row or key from the engine, but did not consistently take into account
index only accessed, clustered key or covered keys for all access
paths.
The cost of the WHERE clause (TIME_FOR_COMPARE) was not consistently
considered in best_access_path(). TIME_FOR_COMPARE was used in
calculation in other places, like greedy_search(), but was in some
cases (like scans) done an a different number of rows than was
accessed.
The cost calculation of row and index scans didn't take into account
the number of rows that where accessed, only the number of accepted
rows.
When using a filter, the cost of index_only_reads and cost of
accessing and disregarding 'filtered rows' where not taken into
account, which made filters cost less than there actually where.
To remedy the above, the following key & row fetch related costs
has been added:
- The cost of fetching and using a row is now split into different costs:
- key + Row fetch cost (as before) but multiplied with the variable
'optimizer_cache_cost' (default to 0.5). This allows the user to
tell the optimizer the likehood of finding the key and row in the
engine cache.
- ROW_COPY_COST, The cost copying a row from the engine to the
sql layer or creating a row from the join_cache to the record
buffer. Mostly affects table scan costs.
- ROW_LOOKUP_COST, the cost of fetching a row by rowid.
- KEY_COPY_COST the cost of finding the next key and copying it from
the engine to the SQL layer. This is used when we calculate the cost
index only reads. It makes index scans more expensive than before if
they cover a lot of rows. (main.index_merge_myisam)
- KEY_LOOKUP_COST, the cost of finding the first key in a range.
This replaces the old define IDX_LOOKUP_COST, but with a higher cost.
- KEY_NEXT_FIND_COST, the cost of finding the next key (and rowid).
when doing a index scan and comparing the rowid to the filter.
Before this cost was assumed to be 0.
All of the above constants/variables are now tuned to be somewhat in
proportion of executing complexity to each other. There is tuning
need for these in the future, but that can wait until the above are
made user variables as that will make tuning much easier.
To make the usage of the above easy, there are new (not virtual)
cost calclation functions in handler:
- ha_read_time(), like read_time(), but take optimizer_cache_cost into
account.
- ha_read_and_copy_time(), like ha_read_time() but take into account
ROW_COPY_TIME
- ha_read_and_compare_time(), like ha_read_and_copy_time() but take
TIME_FOR_COMPARE into account.
- ha_rnd_pos_time(). Read row with row id, taking ROW_COPY_COST
into account. This is used with filesort where we don't need
to execute the WHERE clause again.
- ha_keyread_time(), like keyread_time() but take
optimizer_cache_cost into account.
- ha_keyread_and_copy_time(), like ha_keyread_time(), but add
KEY_COPY_COST.
- ha_key_scan_time(), like key_scan_time() but take
optimizer_cache_cost nto account.
- ha_key_scan_and_compare_time(), like ha_key_scan_time(), but add
KEY_COPY_COST & TIME_FOR_COMPARE.
I also added some setup costs for doing different types of scans and
creating temporary tables (on disk and in memory). This encourages
the optimizer to not use these for simple 'a few row' lookups if
there are adequate key lookup strategies.
- TABLE_SCAN_SETUP_COST, cost of starting a table scan.
- INDEX_SCAN_SETUP_COST, cost of starting an index scan.
- HEAP_TEMPTABLE_CREATE_COST, cost of creating in memory
temporary table.
- DISK_TEMPTABLE_CREATE_COST, cost of creating an on disk temporary
table.
When calculating cost of fetching ranges, we had a cost of
IDX_LOOKUP_COST (0.125) for doing a key div for a new range. This is
now replaced with 'io_cost * KEY_LOOKUP_COST (1.0) *
optimizer_cache_cost', which matches the cost we use for 'ref' and
other key lookups. The effect is that the cost is now a bit higher
when we have many ranges for a key.
Allmost all calculation with TIME_FOR_COMPARE is now done in
best_access_path(). 'JOIN::read_time' now includes the full
cost for finding the rows in the table.
In the result files, many of the changes are now again close to what
they where before the "Update cost for hash and cached joins" commit,
as that commit didn't fix the filter cost (too complex to do
everything in one commit).
The above changes showed a lot of a lot of inconsistencies in
optimizer cost calculation. The main objective with the other changes
was to do calculation as similar (and accurate) as possible and to make
different plans more comparable.
Detailed list of changes:
- Calculate index_only_cost consistently and correctly for all scan
and ref accesses. The row fetch_cost and index_only_cost now
takes into account clustered keys, covered keys and index
only accesses.
- cost_for_index_read now returns both full cost and index_only_cost
- Fixed cost calculation of get_sweep_read_cost() to match other
similar costs. This is bases on the assumption that data is more
often stored on SSD than a hard disk.
- Replaced constant 2.0 with new define TABLE_SCAN_SETUP_COST.
- Some scan cost estimates did not take into account
TIME_FOR_COMPARE. Now all scan costs takes this into
account. (main.show_explain)
- Added session variable optimizer_cache_hit_ratio (default 50%). By
adjusting this on can reduce or increase the cost of index or direct
record lookups. The effect of the default is that key lookups is now
a bit cheaper than before. See usage of 'optimizer_cache_cost' in
handler.h.
- JOIN_TAB::scan_time() did not take into account index only scans,
which produced a wrong cost when index scan was used. Changed
JOIN_TAB:::scan_time() to take into consideration clustered and
covered keys. The values are now cached and we only have to call
this function once. Other calls are changed to use the cached
values. Function renamed to JOIN_TAB::estimate_scan_time().
- Fixed that most index cost calculations are done the same way and
more close to 'range' calculations. The cost is now lower than
before for small data sets and higher for large data sets as we take
into account how many keys are read (main.opt_trace_selectivity,
main.limit_rows_examined).
- Ensured that index_scan_cost() ==
range(scan_of_all_rows_in_table_using_one_range) +
MULTI_RANGE_READ_INFO_CONST. One effect of this is that if there
is choice of doing a full index scan and a range-index scan over
almost the whole table then index scan will be preferred (no
range-read setup cost). (innodb.innodb, main.show_explain,
main.range)
- Fixed the EQ_REF and REF takes into account clustered and covered
keys. This changes some plans to use covered or clustered indexes
as these are much cheaper. (main.subselect_mat_cost,
main.state_tables_innodb, main.limit_rows_examined)
- Rowid filter setup cost and filter compare cost now takes into
account fetching and checking the rowid (KEY_NEXT_FIND_COST).
(main.partition_pruning heap.heap_btree main.log_state)
- Added KEY_NEXT_FIND_COST to
Range_rowid_filter_cost_info::lookup_cost to account of the time
to find and check the next key value against the container
- Introduced ha_keyread_time(rows) that takes into account finding
the next row and copying the key value to 'record'
(KEY_COPY_COST).
- Introduced ha_key_scan_time() for calculating an index scan over
all rows.
- Added IDX_LOOKUP_COST to keyread_time() as a startup cost.
- Added index_only_fetch_cost() as a convenience function to
OPT_RANGE.
- keyread_time() cost is slightly reduced to prefer shorter keys.
(main.index_merge_myisam)
- All of the above caused some index_merge combinations to be
rejected because of cost (main.index_intersect). In some cases
'ref' where replaced with index_merge because of the low
cost calculation of get_sweep_read_cost().
- Some index usage moved from PRIMARY to a covering index.
(main.subselect_innodb)
- Changed cost calculation of filter to take KEY_LOOKUP_COST and
TIME_FOR_COMPARE into account. See sql_select.cc::apply_filter().
filter parameters and costs are now written to optimizer_trace.
- Don't use matchings_records_in_range() to try to estimate the number
of filtered rows for ranges. The reason is that we want to ensure
that 'range' is calculated similar to 'ref'. There is also more work
needed to calculate the selectivity when using ranges and ranges and
filtering. This causes filtering column in EXPLAIN EXTENDED to be
100.00 for some cases where range cannot use filtering.
(main.rowid_filter)
- Introduced ha_scan_time() that takes into account the CPU cost of
finding the next row and copying the row from the engine to
'record'. This causes costs of table scan to slightly increase and
some test to changed their plan from ALL to RANGE or ALL to ref.
(innodb.innodb_mysql, main.select_pkeycache)
In a few cases where scan time of very small tables have lower cost
than a ref or range, things changed from ref/range to ALL.
(main.myisam, main.func_group, main.limit_rows_examined,
main.subselect2)
- Introduced ha_scan_and_compare_time() which is like ha_scan_time()
but also adds the cost of the where clause (TIME_FOR_COMPARE).
- Added small cost for creating temporary table for
materialization. This causes some very small tables to use scan
instead of materialization.
- Added checking of the WHERE clause (TIME_FOR_COMPARE) of the
accepted rows to ROR costs in get_best_ror_intersect()
- Removed '- 0.001' from 'join->best_read' and optimize_straight_join()
to ensure that the 'Last_query_cost' status variable contains the
same value as the one that was calculated by the optimizer.
- Take avg_io_cost() into account in handler::keyread_time() and
handler::read_time(). This should have no effect as it's 1.0 by
default, except for heap that overrides these functions.
- Some 'ref_or_null' accesses changed to 'range' because of cost
adjustments (main.order_by)
- Added scan type "scan_with_join_cache" for optimizer_trace. This is
just to show in the trace what kind of scan was used.
- When using 'scan_with_join_cache' take into account number of
preceding tables (as have to restore all fields for all previous
table combination when checking the where clause)
The new cost added is:
(row_combinations * ROW_COPY_COST * number_of_cached_tables).
This increases the cost of join buffering in proportion of the
number of tables in the join buffer. One effect is that full scans
are now done earlier as the cost is then smaller.
(main.join_outer_innodb, main.greedy_optimizer)
- Removed the usage of 'worst_seeks' in cost_for_index_read as it
caused wrong plans to be created; It prefered JT_EQ_REF even if it
would be much more expensive than a full table scan. A related
issue was that worst_seeks only applied to full lookup, not to
clustered or index only lookups, which is not consistent. This
caused some plans to use index scan instead of eq_ref (main.union)
- Changed federated block size from 4096 to 1500, which is the
typical size of an IO packet.
- Added costs for reading rows to Federated. Needed as there is no
caching of rows in the federated engine.
- Added ha_innobase::rnd_pos_time() cost function.
- A lot of extra things added to optimizer trace
- More costs, especially for materialization and index_merge.
- Make lables more uniform
- Fixed a lot of minor bugs
- Added 'trace_started()' around a lot of trace blocks.
- When calculating ORDER BY with LIMIT cost for using an index
the cost did not take into account the number of row retrivals
that has to be done or the cost of comparing the rows with the
WHERE clause. The cost calculated would be just a fraction of
the real cost. Now we calculate the cost as we do for ranges
and 'ref'.
- 'Using index for group-by' is used a bit more than before as
now take into account the WHERE clause cost when comparing
with 'ref' and prefer the method with fewer row combinations.
(main.group_min_max).
Bugs fixed:
- Fixed that we don't calculate TIME_FOR_COMPARE twice for some plans,
like in optimize_straight_join() and greedy_search()
- Fixed bug in save_explain_data where we could test for the wrong
index when displaying 'Using index'. This caused some old plans to
show 'Using index'. (main.subselect_innodb, main.subselect2)
- Fixed bug in get_best_ror_intersect() where 'min_cost' was not
updated, and the cost we compared with was not the one that was
used.
- Fixed very wrong cost calculation for priority queues in
check_if_pq_applicable(). (main.order_by now correctly uses priority
queue)
- When calculating cost of EQ_REF or REF, we added the cost of
comparing the WHERE clause with the found rows, not all row
combinations. This made ref and eq_ref to be regarded way to cheap
compared to other access methods.
- FORCE INDEX cost calculation didn't take into account clustered or
covered indexes.
- JT_EQ_REF cost was estimated as avg_io_cost(), which is half the
cost of a JT_REF key. This may be true for InnoDB primary key, but
not for other unique keys or other engines. Now we use handler
function to calculate the cost, which allows us to handle
consistently clustered, covered keys and not covered keys.
- ha_start_keyread() didn't call extra_opt() if keyread was already
enabled but still changed the 'keyread' variable (which is wrong).
Fixed by not doing anything if keyread is already enabled.
- multi_range_read_info_cost() didn't take into account io_cost when
calculating the cost of ranges.
- fix_semijoin_strategies_for_picked_join_order() used the wrong
record_count when calling best_access_path() for SJ_OPT_FIRST_MATCH
and SJ_OPT_LOOSE_SCAN.
- Hash joins didn't provide correct best_cost to the upper level, which
means that the cost for hash_joins more expensive than calculated
in best_access_path (a difference of 10x * TIME_OF_COMPARE).
This is fixed in the new code thanks to that we now include
TIME_OF_COMPARE cost in 'read_time'.
Other things:
- Added some 'if (thd->trace_started())' to speed up code
- Removed not used function Cost_estimate::is_zero()
- Simplified testing of HA_POS_ERROR in get_best_ror_intersect().
(No cost changes)
- Moved ha_start_keyread() from join_read_const_table() to join_read_const()
to enable keyread for all types of JT_CONST tables.
- Made a few very short functions inline in handler.h
Notes:
- In main.rowid_filter the join order of order and lineitem is swapped.
This is because the cost of doing a range fetch of lineitem(98 rows) is
almost as big as the whole join of order,lineitem. The filtering will
also ensure that we only have to do very small key fetches of the rows
in lineitem.
- main.index_merge_myisam had a few changes where we are now using
less keys for index_merge. This is because index scans are now more
expensive than before.
- handler->optimizer_cache_cost is updated in ha_external_lock().
This ensures that it is up to date per statements.
Not an optimal solution (for locked tables), but should be ok for now.
- 'DELETE FROM t1 WHERE t1.a > 0 ORDER BY t1.a' does not take cost of
filesort into consideration when table scan is chosen.
(main.myisam_explain_non_select_all)
- perfschema.table_aggregate_global_* has changed because an update
on a table with 1 row will now use table scan instead of key lookup.
TODO in upcomming commits:
- Fix selectivity calculation for ranges with and without filtering and
when there is a ref access but scan is chosen.
For this we have to store the lowest known value for
'accepted_records' in the OPT_RANGE structure.
- Change that records_read does not include filtered rows.
- test_if_cheaper_ordering() needs to be updated to properly calculate
costs. This will fix tests like main.order_by_innodb,
main.single_delete_update
- Extend get_range_limit_read_cost() to take into considering
cost_for_index_read() if there where no quick keys. This will reduce
the computed cost for ORDER BY with LIMIT in some cases.
(main.innodb_ext_key)
- Fix that we take into account selectivity when counting the number
of rows we have to read when considering using a index table scan to
resolve ORDER BY.
- Add new calculation for rnd_pos_time() where we take into account the
benefit of reading multiple rows from the same page.
2021-11-01 12:34:24 +02:00
|
|
|
#else
|
2020-02-26 14:52:23 +02:00
|
|
|
if (table->file->pk_is_clustering_key(table->s->primary_key))
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
Update row and key fetch cost models to take into account data copy costs
Before this patch, when calculating the cost of fetching and using a
row/key from the engine, we took into account the cost of finding a
row or key from the engine, but did not consistently take into account
index only accessed, clustered key or covered keys for all access
paths.
The cost of the WHERE clause (TIME_FOR_COMPARE) was not consistently
considered in best_access_path(). TIME_FOR_COMPARE was used in
calculation in other places, like greedy_search(), but was in some
cases (like scans) done an a different number of rows than was
accessed.
The cost calculation of row and index scans didn't take into account
the number of rows that where accessed, only the number of accepted
rows.
When using a filter, the cost of index_only_reads and cost of
accessing and disregarding 'filtered rows' where not taken into
account, which made filters cost less than there actually where.
To remedy the above, the following key & row fetch related costs
has been added:
- The cost of fetching and using a row is now split into different costs:
- key + Row fetch cost (as before) but multiplied with the variable
'optimizer_cache_cost' (default to 0.5). This allows the user to
tell the optimizer the likehood of finding the key and row in the
engine cache.
- ROW_COPY_COST, The cost copying a row from the engine to the
sql layer or creating a row from the join_cache to the record
buffer. Mostly affects table scan costs.
- ROW_LOOKUP_COST, the cost of fetching a row by rowid.
- KEY_COPY_COST the cost of finding the next key and copying it from
the engine to the SQL layer. This is used when we calculate the cost
index only reads. It makes index scans more expensive than before if
they cover a lot of rows. (main.index_merge_myisam)
- KEY_LOOKUP_COST, the cost of finding the first key in a range.
This replaces the old define IDX_LOOKUP_COST, but with a higher cost.
- KEY_NEXT_FIND_COST, the cost of finding the next key (and rowid).
when doing a index scan and comparing the rowid to the filter.
Before this cost was assumed to be 0.
All of the above constants/variables are now tuned to be somewhat in
proportion of executing complexity to each other. There is tuning
need for these in the future, but that can wait until the above are
made user variables as that will make tuning much easier.
To make the usage of the above easy, there are new (not virtual)
cost calclation functions in handler:
- ha_read_time(), like read_time(), but take optimizer_cache_cost into
account.
- ha_read_and_copy_time(), like ha_read_time() but take into account
ROW_COPY_TIME
- ha_read_and_compare_time(), like ha_read_and_copy_time() but take
TIME_FOR_COMPARE into account.
- ha_rnd_pos_time(). Read row with row id, taking ROW_COPY_COST
into account. This is used with filesort where we don't need
to execute the WHERE clause again.
- ha_keyread_time(), like keyread_time() but take
optimizer_cache_cost into account.
- ha_keyread_and_copy_time(), like ha_keyread_time(), but add
KEY_COPY_COST.
- ha_key_scan_time(), like key_scan_time() but take
optimizer_cache_cost nto account.
- ha_key_scan_and_compare_time(), like ha_key_scan_time(), but add
KEY_COPY_COST & TIME_FOR_COMPARE.
I also added some setup costs for doing different types of scans and
creating temporary tables (on disk and in memory). This encourages
the optimizer to not use these for simple 'a few row' lookups if
there are adequate key lookup strategies.
- TABLE_SCAN_SETUP_COST, cost of starting a table scan.
- INDEX_SCAN_SETUP_COST, cost of starting an index scan.
- HEAP_TEMPTABLE_CREATE_COST, cost of creating in memory
temporary table.
- DISK_TEMPTABLE_CREATE_COST, cost of creating an on disk temporary
table.
When calculating cost of fetching ranges, we had a cost of
IDX_LOOKUP_COST (0.125) for doing a key div for a new range. This is
now replaced with 'io_cost * KEY_LOOKUP_COST (1.0) *
optimizer_cache_cost', which matches the cost we use for 'ref' and
other key lookups. The effect is that the cost is now a bit higher
when we have many ranges for a key.
Allmost all calculation with TIME_FOR_COMPARE is now done in
best_access_path(). 'JOIN::read_time' now includes the full
cost for finding the rows in the table.
In the result files, many of the changes are now again close to what
they where before the "Update cost for hash and cached joins" commit,
as that commit didn't fix the filter cost (too complex to do
everything in one commit).
The above changes showed a lot of a lot of inconsistencies in
optimizer cost calculation. The main objective with the other changes
was to do calculation as similar (and accurate) as possible and to make
different plans more comparable.
Detailed list of changes:
- Calculate index_only_cost consistently and correctly for all scan
and ref accesses. The row fetch_cost and index_only_cost now
takes into account clustered keys, covered keys and index
only accesses.
- cost_for_index_read now returns both full cost and index_only_cost
- Fixed cost calculation of get_sweep_read_cost() to match other
similar costs. This is bases on the assumption that data is more
often stored on SSD than a hard disk.
- Replaced constant 2.0 with new define TABLE_SCAN_SETUP_COST.
- Some scan cost estimates did not take into account
TIME_FOR_COMPARE. Now all scan costs takes this into
account. (main.show_explain)
- Added session variable optimizer_cache_hit_ratio (default 50%). By
adjusting this on can reduce or increase the cost of index or direct
record lookups. The effect of the default is that key lookups is now
a bit cheaper than before. See usage of 'optimizer_cache_cost' in
handler.h.
- JOIN_TAB::scan_time() did not take into account index only scans,
which produced a wrong cost when index scan was used. Changed
JOIN_TAB:::scan_time() to take into consideration clustered and
covered keys. The values are now cached and we only have to call
this function once. Other calls are changed to use the cached
values. Function renamed to JOIN_TAB::estimate_scan_time().
- Fixed that most index cost calculations are done the same way and
more close to 'range' calculations. The cost is now lower than
before for small data sets and higher for large data sets as we take
into account how many keys are read (main.opt_trace_selectivity,
main.limit_rows_examined).
- Ensured that index_scan_cost() ==
range(scan_of_all_rows_in_table_using_one_range) +
MULTI_RANGE_READ_INFO_CONST. One effect of this is that if there
is choice of doing a full index scan and a range-index scan over
almost the whole table then index scan will be preferred (no
range-read setup cost). (innodb.innodb, main.show_explain,
main.range)
- Fixed the EQ_REF and REF takes into account clustered and covered
keys. This changes some plans to use covered or clustered indexes
as these are much cheaper. (main.subselect_mat_cost,
main.state_tables_innodb, main.limit_rows_examined)
- Rowid filter setup cost and filter compare cost now takes into
account fetching and checking the rowid (KEY_NEXT_FIND_COST).
(main.partition_pruning heap.heap_btree main.log_state)
- Added KEY_NEXT_FIND_COST to
Range_rowid_filter_cost_info::lookup_cost to account of the time
to find and check the next key value against the container
- Introduced ha_keyread_time(rows) that takes into account finding
the next row and copying the key value to 'record'
(KEY_COPY_COST).
- Introduced ha_key_scan_time() for calculating an index scan over
all rows.
- Added IDX_LOOKUP_COST to keyread_time() as a startup cost.
- Added index_only_fetch_cost() as a convenience function to
OPT_RANGE.
- keyread_time() cost is slightly reduced to prefer shorter keys.
(main.index_merge_myisam)
- All of the above caused some index_merge combinations to be
rejected because of cost (main.index_intersect). In some cases
'ref' where replaced with index_merge because of the low
cost calculation of get_sweep_read_cost().
- Some index usage moved from PRIMARY to a covering index.
(main.subselect_innodb)
- Changed cost calculation of filter to take KEY_LOOKUP_COST and
TIME_FOR_COMPARE into account. See sql_select.cc::apply_filter().
filter parameters and costs are now written to optimizer_trace.
- Don't use matchings_records_in_range() to try to estimate the number
of filtered rows for ranges. The reason is that we want to ensure
that 'range' is calculated similar to 'ref'. There is also more work
needed to calculate the selectivity when using ranges and ranges and
filtering. This causes filtering column in EXPLAIN EXTENDED to be
100.00 for some cases where range cannot use filtering.
(main.rowid_filter)
- Introduced ha_scan_time() that takes into account the CPU cost of
finding the next row and copying the row from the engine to
'record'. This causes costs of table scan to slightly increase and
some test to changed their plan from ALL to RANGE or ALL to ref.
(innodb.innodb_mysql, main.select_pkeycache)
In a few cases where scan time of very small tables have lower cost
than a ref or range, things changed from ref/range to ALL.
(main.myisam, main.func_group, main.limit_rows_examined,
main.subselect2)
- Introduced ha_scan_and_compare_time() which is like ha_scan_time()
but also adds the cost of the where clause (TIME_FOR_COMPARE).
- Added small cost for creating temporary table for
materialization. This causes some very small tables to use scan
instead of materialization.
- Added checking of the WHERE clause (TIME_FOR_COMPARE) of the
accepted rows to ROR costs in get_best_ror_intersect()
- Removed '- 0.001' from 'join->best_read' and optimize_straight_join()
to ensure that the 'Last_query_cost' status variable contains the
same value as the one that was calculated by the optimizer.
- Take avg_io_cost() into account in handler::keyread_time() and
handler::read_time(). This should have no effect as it's 1.0 by
default, except for heap that overrides these functions.
- Some 'ref_or_null' accesses changed to 'range' because of cost
adjustments (main.order_by)
- Added scan type "scan_with_join_cache" for optimizer_trace. This is
just to show in the trace what kind of scan was used.
- When using 'scan_with_join_cache' take into account number of
preceding tables (as have to restore all fields for all previous
table combination when checking the where clause)
The new cost added is:
(row_combinations * ROW_COPY_COST * number_of_cached_tables).
This increases the cost of join buffering in proportion of the
number of tables in the join buffer. One effect is that full scans
are now done earlier as the cost is then smaller.
(main.join_outer_innodb, main.greedy_optimizer)
- Removed the usage of 'worst_seeks' in cost_for_index_read as it
caused wrong plans to be created; It prefered JT_EQ_REF even if it
would be much more expensive than a full table scan. A related
issue was that worst_seeks only applied to full lookup, not to
clustered or index only lookups, which is not consistent. This
caused some plans to use index scan instead of eq_ref (main.union)
- Changed federated block size from 4096 to 1500, which is the
typical size of an IO packet.
- Added costs for reading rows to Federated. Needed as there is no
caching of rows in the federated engine.
- Added ha_innobase::rnd_pos_time() cost function.
- A lot of extra things added to optimizer trace
- More costs, especially for materialization and index_merge.
- Make lables more uniform
- Fixed a lot of minor bugs
- Added 'trace_started()' around a lot of trace blocks.
- When calculating ORDER BY with LIMIT cost for using an index
the cost did not take into account the number of row retrivals
that has to be done or the cost of comparing the rows with the
WHERE clause. The cost calculated would be just a fraction of
the real cost. Now we calculate the cost as we do for ranges
and 'ref'.
- 'Using index for group-by' is used a bit more than before as
now take into account the WHERE clause cost when comparing
with 'ref' and prefer the method with fewer row combinations.
(main.group_min_max).
Bugs fixed:
- Fixed that we don't calculate TIME_FOR_COMPARE twice for some plans,
like in optimize_straight_join() and greedy_search()
- Fixed bug in save_explain_data where we could test for the wrong
index when displaying 'Using index'. This caused some old plans to
show 'Using index'. (main.subselect_innodb, main.subselect2)
- Fixed bug in get_best_ror_intersect() where 'min_cost' was not
updated, and the cost we compared with was not the one that was
used.
- Fixed very wrong cost calculation for priority queues in
check_if_pq_applicable(). (main.order_by now correctly uses priority
queue)
- When calculating cost of EQ_REF or REF, we added the cost of
comparing the WHERE clause with the found rows, not all row
combinations. This made ref and eq_ref to be regarded way to cheap
compared to other access methods.
- FORCE INDEX cost calculation didn't take into account clustered or
covered indexes.
- JT_EQ_REF cost was estimated as avg_io_cost(), which is half the
cost of a JT_REF key. This may be true for InnoDB primary key, but
not for other unique keys or other engines. Now we use handler
function to calculate the cost, which allows us to handle
consistently clustered, covered keys and not covered keys.
- ha_start_keyread() didn't call extra_opt() if keyread was already
enabled but still changed the 'keyread' variable (which is wrong).
Fixed by not doing anything if keyread is already enabled.
- multi_range_read_info_cost() didn't take into account io_cost when
calculating the cost of ranges.
- fix_semijoin_strategies_for_picked_join_order() used the wrong
record_count when calling best_access_path() for SJ_OPT_FIRST_MATCH
and SJ_OPT_LOOSE_SCAN.
- Hash joins didn't provide correct best_cost to the upper level, which
means that the cost for hash_joins more expensive than calculated
in best_access_path (a difference of 10x * TIME_OF_COMPARE).
This is fixed in the new code thanks to that we now include
TIME_OF_COMPARE cost in 'read_time'.
Other things:
- Added some 'if (thd->trace_started())' to speed up code
- Removed not used function Cost_estimate::is_zero()
- Simplified testing of HA_POS_ERROR in get_best_ror_intersect().
(No cost changes)
- Moved ha_start_keyread() from join_read_const_table() to join_read_const()
to enable keyread for all types of JT_CONST tables.
- Made a few very short functions inline in handler.h
Notes:
- In main.rowid_filter the join order of order and lineitem is swapped.
This is because the cost of doing a range fetch of lineitem(98 rows) is
almost as big as the whole join of order,lineitem. The filtering will
also ensure that we only have to do very small key fetches of the rows
in lineitem.
- main.index_merge_myisam had a few changes where we are now using
less keys for index_merge. This is because index scans are now more
expensive than before.
- handler->optimizer_cache_cost is updated in ha_external_lock().
This ensures that it is up to date per statements.
Not an optimal solution (for locked tables), but should be ok for now.
- 'DELETE FROM t1 WHERE t1.a > 0 ORDER BY t1.a' does not take cost of
filesort into consideration when table scan is chosen.
(main.myisam_explain_non_select_all)
- perfschema.table_aggregate_global_* has changed because an update
on a table with 1 row will now use table scan instead of key lookup.
TODO in upcomming commits:
- Fix selectivity calculation for ranges with and without filtering and
when there is a ref access but scan is chosen.
For this we have to store the lowest known value for
'accepted_records' in the OPT_RANGE structure.
- Change that records_read does not include filtered rows.
- test_if_cheaper_ordering() needs to be updated to properly calculate
costs. This will fix tests like main.order_by_innodb,
main.single_delete_update
- Extend get_range_limit_read_cost() to take into considering
cost_for_index_read() if there where no quick keys. This will reduce
the computed cost for ORDER BY with LIMIT in some cases.
(main.innodb_ext_key)
- Fix that we take into account selectivity when counting the number
of rows we have to read when considering using a index table scan to
resolve ORDER BY.
- Add new calculation for rnd_pos_time() where we take into account the
benefit of reading multiple rows from the same page.
2021-11-01 12:34:24 +02:00
|
|
|
cost->cpu_cost= table->file->ha_read_and_copy_time(table->s->primary_key,
|
|
|
|
(uint) nrows, nrows);
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
2020-02-28 12:59:30 +02:00
|
|
|
else if ((cost->avg_io_cost= table->file->avg_io_cost()) >= 0.999)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2009-12-22 15:33:21 +03:00
|
|
|
double n_blocks=
|
|
|
|
ceil(ulonglong2double(table->file->stats.data_file_length) / IO_SIZE);
|
|
|
|
double busy_blocks=
|
|
|
|
n_blocks * (1.0 - pow(1.0 - 1.0/n_blocks, rows2double(nrows)));
|
|
|
|
if (busy_blocks < 1.0)
|
|
|
|
busy_blocks= 1.0;
|
2009-12-15 10:16:46 +03:00
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
DBUG_PRINT("info",("sweep: nblocks=%g, busy_blocks=%g", n_blocks,
|
|
|
|
busy_blocks));
|
|
|
|
cost->io_count= busy_blocks;
|
|
|
|
|
|
|
|
if (!interrupted)
|
2009-12-15 10:16:46 +03:00
|
|
|
{
|
2009-12-22 15:33:21 +03:00
|
|
|
/* Assume reading is done in one 'sweep' */
|
|
|
|
cost->avg_io_cost= (DISK_SEEK_BASE_COST +
|
|
|
|
DISK_SEEK_PROP_COST*n_blocks/busy_blocks);
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
|
|
|
}
|
Update row and key fetch cost models to take into account data copy costs
Before this patch, when calculating the cost of fetching and using a
row/key from the engine, we took into account the cost of finding a
row or key from the engine, but did not consistently take into account
index only accessed, clustered key or covered keys for all access
paths.
The cost of the WHERE clause (TIME_FOR_COMPARE) was not consistently
considered in best_access_path(). TIME_FOR_COMPARE was used in
calculation in other places, like greedy_search(), but was in some
cases (like scans) done an a different number of rows than was
accessed.
The cost calculation of row and index scans didn't take into account
the number of rows that where accessed, only the number of accepted
rows.
When using a filter, the cost of index_only_reads and cost of
accessing and disregarding 'filtered rows' where not taken into
account, which made filters cost less than there actually where.
To remedy the above, the following key & row fetch related costs
has been added:
- The cost of fetching and using a row is now split into different costs:
- key + Row fetch cost (as before) but multiplied with the variable
'optimizer_cache_cost' (default to 0.5). This allows the user to
tell the optimizer the likehood of finding the key and row in the
engine cache.
- ROW_COPY_COST, The cost copying a row from the engine to the
sql layer or creating a row from the join_cache to the record
buffer. Mostly affects table scan costs.
- ROW_LOOKUP_COST, the cost of fetching a row by rowid.
- KEY_COPY_COST the cost of finding the next key and copying it from
the engine to the SQL layer. This is used when we calculate the cost
index only reads. It makes index scans more expensive than before if
they cover a lot of rows. (main.index_merge_myisam)
- KEY_LOOKUP_COST, the cost of finding the first key in a range.
This replaces the old define IDX_LOOKUP_COST, but with a higher cost.
- KEY_NEXT_FIND_COST, the cost of finding the next key (and rowid).
when doing a index scan and comparing the rowid to the filter.
Before this cost was assumed to be 0.
All of the above constants/variables are now tuned to be somewhat in
proportion of executing complexity to each other. There is tuning
need for these in the future, but that can wait until the above are
made user variables as that will make tuning much easier.
To make the usage of the above easy, there are new (not virtual)
cost calclation functions in handler:
- ha_read_time(), like read_time(), but take optimizer_cache_cost into
account.
- ha_read_and_copy_time(), like ha_read_time() but take into account
ROW_COPY_TIME
- ha_read_and_compare_time(), like ha_read_and_copy_time() but take
TIME_FOR_COMPARE into account.
- ha_rnd_pos_time(). Read row with row id, taking ROW_COPY_COST
into account. This is used with filesort where we don't need
to execute the WHERE clause again.
- ha_keyread_time(), like keyread_time() but take
optimizer_cache_cost into account.
- ha_keyread_and_copy_time(), like ha_keyread_time(), but add
KEY_COPY_COST.
- ha_key_scan_time(), like key_scan_time() but take
optimizer_cache_cost nto account.
- ha_key_scan_and_compare_time(), like ha_key_scan_time(), but add
KEY_COPY_COST & TIME_FOR_COMPARE.
I also added some setup costs for doing different types of scans and
creating temporary tables (on disk and in memory). This encourages
the optimizer to not use these for simple 'a few row' lookups if
there are adequate key lookup strategies.
- TABLE_SCAN_SETUP_COST, cost of starting a table scan.
- INDEX_SCAN_SETUP_COST, cost of starting an index scan.
- HEAP_TEMPTABLE_CREATE_COST, cost of creating in memory
temporary table.
- DISK_TEMPTABLE_CREATE_COST, cost of creating an on disk temporary
table.
When calculating cost of fetching ranges, we had a cost of
IDX_LOOKUP_COST (0.125) for doing a key div for a new range. This is
now replaced with 'io_cost * KEY_LOOKUP_COST (1.0) *
optimizer_cache_cost', which matches the cost we use for 'ref' and
other key lookups. The effect is that the cost is now a bit higher
when we have many ranges for a key.
Allmost all calculation with TIME_FOR_COMPARE is now done in
best_access_path(). 'JOIN::read_time' now includes the full
cost for finding the rows in the table.
In the result files, many of the changes are now again close to what
they where before the "Update cost for hash and cached joins" commit,
as that commit didn't fix the filter cost (too complex to do
everything in one commit).
The above changes showed a lot of a lot of inconsistencies in
optimizer cost calculation. The main objective with the other changes
was to do calculation as similar (and accurate) as possible and to make
different plans more comparable.
Detailed list of changes:
- Calculate index_only_cost consistently and correctly for all scan
and ref accesses. The row fetch_cost and index_only_cost now
takes into account clustered keys, covered keys and index
only accesses.
- cost_for_index_read now returns both full cost and index_only_cost
- Fixed cost calculation of get_sweep_read_cost() to match other
similar costs. This is bases on the assumption that data is more
often stored on SSD than a hard disk.
- Replaced constant 2.0 with new define TABLE_SCAN_SETUP_COST.
- Some scan cost estimates did not take into account
TIME_FOR_COMPARE. Now all scan costs takes this into
account. (main.show_explain)
- Added session variable optimizer_cache_hit_ratio (default 50%). By
adjusting this on can reduce or increase the cost of index or direct
record lookups. The effect of the default is that key lookups is now
a bit cheaper than before. See usage of 'optimizer_cache_cost' in
handler.h.
- JOIN_TAB::scan_time() did not take into account index only scans,
which produced a wrong cost when index scan was used. Changed
JOIN_TAB:::scan_time() to take into consideration clustered and
covered keys. The values are now cached and we only have to call
this function once. Other calls are changed to use the cached
values. Function renamed to JOIN_TAB::estimate_scan_time().
- Fixed that most index cost calculations are done the same way and
more close to 'range' calculations. The cost is now lower than
before for small data sets and higher for large data sets as we take
into account how many keys are read (main.opt_trace_selectivity,
main.limit_rows_examined).
- Ensured that index_scan_cost() ==
range(scan_of_all_rows_in_table_using_one_range) +
MULTI_RANGE_READ_INFO_CONST. One effect of this is that if there
is choice of doing a full index scan and a range-index scan over
almost the whole table then index scan will be preferred (no
range-read setup cost). (innodb.innodb, main.show_explain,
main.range)
- Fixed the EQ_REF and REF takes into account clustered and covered
keys. This changes some plans to use covered or clustered indexes
as these are much cheaper. (main.subselect_mat_cost,
main.state_tables_innodb, main.limit_rows_examined)
- Rowid filter setup cost and filter compare cost now takes into
account fetching and checking the rowid (KEY_NEXT_FIND_COST).
(main.partition_pruning heap.heap_btree main.log_state)
- Added KEY_NEXT_FIND_COST to
Range_rowid_filter_cost_info::lookup_cost to account of the time
to find and check the next key value against the container
- Introduced ha_keyread_time(rows) that takes into account finding
the next row and copying the key value to 'record'
(KEY_COPY_COST).
- Introduced ha_key_scan_time() for calculating an index scan over
all rows.
- Added IDX_LOOKUP_COST to keyread_time() as a startup cost.
- Added index_only_fetch_cost() as a convenience function to
OPT_RANGE.
- keyread_time() cost is slightly reduced to prefer shorter keys.
(main.index_merge_myisam)
- All of the above caused some index_merge combinations to be
rejected because of cost (main.index_intersect). In some cases
'ref' where replaced with index_merge because of the low
cost calculation of get_sweep_read_cost().
- Some index usage moved from PRIMARY to a covering index.
(main.subselect_innodb)
- Changed cost calculation of filter to take KEY_LOOKUP_COST and
TIME_FOR_COMPARE into account. See sql_select.cc::apply_filter().
filter parameters and costs are now written to optimizer_trace.
- Don't use matchings_records_in_range() to try to estimate the number
of filtered rows for ranges. The reason is that we want to ensure
that 'range' is calculated similar to 'ref'. There is also more work
needed to calculate the selectivity when using ranges and ranges and
filtering. This causes filtering column in EXPLAIN EXTENDED to be
100.00 for some cases where range cannot use filtering.
(main.rowid_filter)
- Introduced ha_scan_time() that takes into account the CPU cost of
finding the next row and copying the row from the engine to
'record'. This causes costs of table scan to slightly increase and
some test to changed their plan from ALL to RANGE or ALL to ref.
(innodb.innodb_mysql, main.select_pkeycache)
In a few cases where scan time of very small tables have lower cost
than a ref or range, things changed from ref/range to ALL.
(main.myisam, main.func_group, main.limit_rows_examined,
main.subselect2)
- Introduced ha_scan_and_compare_time() which is like ha_scan_time()
but also adds the cost of the where clause (TIME_FOR_COMPARE).
- Added small cost for creating temporary table for
materialization. This causes some very small tables to use scan
instead of materialization.
- Added checking of the WHERE clause (TIME_FOR_COMPARE) of the
accepted rows to ROR costs in get_best_ror_intersect()
- Removed '- 0.001' from 'join->best_read' and optimize_straight_join()
to ensure that the 'Last_query_cost' status variable contains the
same value as the one that was calculated by the optimizer.
- Take avg_io_cost() into account in handler::keyread_time() and
handler::read_time(). This should have no effect as it's 1.0 by
default, except for heap that overrides these functions.
- Some 'ref_or_null' accesses changed to 'range' because of cost
adjustments (main.order_by)
- Added scan type "scan_with_join_cache" for optimizer_trace. This is
just to show in the trace what kind of scan was used.
- When using 'scan_with_join_cache' take into account number of
preceding tables (as have to restore all fields for all previous
table combination when checking the where clause)
The new cost added is:
(row_combinations * ROW_COPY_COST * number_of_cached_tables).
This increases the cost of join buffering in proportion of the
number of tables in the join buffer. One effect is that full scans
are now done earlier as the cost is then smaller.
(main.join_outer_innodb, main.greedy_optimizer)
- Removed the usage of 'worst_seeks' in cost_for_index_read as it
caused wrong plans to be created; It prefered JT_EQ_REF even if it
would be much more expensive than a full table scan. A related
issue was that worst_seeks only applied to full lookup, not to
clustered or index only lookups, which is not consistent. This
caused some plans to use index scan instead of eq_ref (main.union)
- Changed federated block size from 4096 to 1500, which is the
typical size of an IO packet.
- Added costs for reading rows to Federated. Needed as there is no
caching of rows in the federated engine.
- Added ha_innobase::rnd_pos_time() cost function.
- A lot of extra things added to optimizer trace
- More costs, especially for materialization and index_merge.
- Make lables more uniform
- Fixed a lot of minor bugs
- Added 'trace_started()' around a lot of trace blocks.
- When calculating ORDER BY with LIMIT cost for using an index
the cost did not take into account the number of row retrivals
that has to be done or the cost of comparing the rows with the
WHERE clause. The cost calculated would be just a fraction of
the real cost. Now we calculate the cost as we do for ranges
and 'ref'.
- 'Using index for group-by' is used a bit more than before as
now take into account the WHERE clause cost when comparing
with 'ref' and prefer the method with fewer row combinations.
(main.group_min_max).
Bugs fixed:
- Fixed that we don't calculate TIME_FOR_COMPARE twice for some plans,
like in optimize_straight_join() and greedy_search()
- Fixed bug in save_explain_data where we could test for the wrong
index when displaying 'Using index'. This caused some old plans to
show 'Using index'. (main.subselect_innodb, main.subselect2)
- Fixed bug in get_best_ror_intersect() where 'min_cost' was not
updated, and the cost we compared with was not the one that was
used.
- Fixed very wrong cost calculation for priority queues in
check_if_pq_applicable(). (main.order_by now correctly uses priority
queue)
- When calculating cost of EQ_REF or REF, we added the cost of
comparing the WHERE clause with the found rows, not all row
combinations. This made ref and eq_ref to be regarded way to cheap
compared to other access methods.
- FORCE INDEX cost calculation didn't take into account clustered or
covered indexes.
- JT_EQ_REF cost was estimated as avg_io_cost(), which is half the
cost of a JT_REF key. This may be true for InnoDB primary key, but
not for other unique keys or other engines. Now we use handler
function to calculate the cost, which allows us to handle
consistently clustered, covered keys and not covered keys.
- ha_start_keyread() didn't call extra_opt() if keyread was already
enabled but still changed the 'keyread' variable (which is wrong).
Fixed by not doing anything if keyread is already enabled.
- multi_range_read_info_cost() didn't take into account io_cost when
calculating the cost of ranges.
- fix_semijoin_strategies_for_picked_join_order() used the wrong
record_count when calling best_access_path() for SJ_OPT_FIRST_MATCH
and SJ_OPT_LOOSE_SCAN.
- Hash joins didn't provide correct best_cost to the upper level, which
means that the cost for hash_joins more expensive than calculated
in best_access_path (a difference of 10x * TIME_OF_COMPARE).
This is fixed in the new code thanks to that we now include
TIME_OF_COMPARE cost in 'read_time'.
Other things:
- Added some 'if (thd->trace_started())' to speed up code
- Removed not used function Cost_estimate::is_zero()
- Simplified testing of HA_POS_ERROR in get_best_ror_intersect().
(No cost changes)
- Moved ha_start_keyread() from join_read_const_table() to join_read_const()
to enable keyread for all types of JT_CONST tables.
- Made a few very short functions inline in handler.h
Notes:
- In main.rowid_filter the join order of order and lineitem is swapped.
This is because the cost of doing a range fetch of lineitem(98 rows) is
almost as big as the whole join of order,lineitem. The filtering will
also ensure that we only have to do very small key fetches of the rows
in lineitem.
- main.index_merge_myisam had a few changes where we are now using
less keys for index_merge. This is because index scans are now more
expensive than before.
- handler->optimizer_cache_cost is updated in ha_external_lock().
This ensures that it is up to date per statements.
Not an optimal solution (for locked tables), but should be ok for now.
- 'DELETE FROM t1 WHERE t1.a > 0 ORDER BY t1.a' does not take cost of
filesort into consideration when table scan is chosen.
(main.myisam_explain_non_select_all)
- perfschema.table_aggregate_global_* has changed because an update
on a table with 1 row will now use table scan instead of key lookup.
TODO in upcomming commits:
- Fix selectivity calculation for ranges with and without filtering and
when there is a ref access but scan is chosen.
For this we have to store the lowest known value for
'accepted_records' in the OPT_RANGE structure.
- Change that records_read does not include filtered rows.
- test_if_cheaper_ordering() needs to be updated to properly calculate
costs. This will fix tests like main.order_by_innodb,
main.single_delete_update
- Extend get_range_limit_read_cost() to take into considering
cost_for_index_read() if there where no quick keys. This will reduce
the computed cost for ORDER BY with LIMIT in some cases.
(main.innodb_ext_key)
- Fix that we take into account selectivity when counting the number
of rows we have to read when considering using a index table scan to
resolve ORDER BY.
- Add new calculation for rnd_pos_time() where we take into account the
benefit of reading multiple rows from the same page.
2021-11-01 12:34:24 +02:00
|
|
|
cost->cpu_cost+= rows2double(n_rows) * ROW_COPY_COST;
|
|
|
|
#endif
|
|
|
|
DBUG_PRINT("info",("returning cost: %g", cost->total_cost()));
|
2009-12-22 15:33:21 +03:00
|
|
|
DBUG_VOID_RETURN;
|
2009-12-15 10:16:46 +03:00
|
|
|
}
|
|
|
|
|
2009-12-22 15:33:21 +03:00
|
|
|
|
|
|
|
/* **************************************************************************
|
|
|
|
* DS-MRR implementation ends
|
|
|
|
***************************************************************************/
|