2013-04-09 16:17:16 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 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
|
2019-05-11 22:19:05 +03:00
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA
|
2013-04-09 16:17:16 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
a engine that auto-creates tables with rows filled with sequential values
|
|
|
|
*/
|
|
|
|
|
2014-10-09 10:30:11 +02:00
|
|
|
#include <my_config.h>
|
2014-03-01 13:27:04 +01:00
|
|
|
#include <ctype.h>
|
2013-04-09 16:17:16 +02:00
|
|
|
#include <mysql_version.h>
|
2015-10-02 10:18:27 +02:00
|
|
|
#include <item.h>
|
|
|
|
#include <item_sum.h>
|
2013-04-09 16:17:16 +02:00
|
|
|
#include <handler.h>
|
|
|
|
#include <table.h>
|
|
|
|
#include <field.h>
|
2019-10-11 13:08:53 +02:00
|
|
|
#include <sql_limit.h>
|
2013-04-09 16:17:16 +02:00
|
|
|
|
MDEV-10139 Support for SEQUENCE objects
Working features:
CREATE OR REPLACE [TEMPORARY] SEQUENCE [IF NOT EXISTS] name
[ INCREMENT [ BY | = ] increment ]
[ MINVALUE [=] minvalue | NO MINVALUE ]
[ MAXVALUE [=] maxvalue | NO MAXVALUE ]
[ START [ WITH | = ] start ] [ CACHE [=] cache ] [ [ NO ] CYCLE ]
ENGINE=xxx COMMENT=".."
SELECT NEXT VALUE FOR sequence_name;
SELECT NEXTVAL(sequence_name);
SELECT PREVIOUS VALUE FOR sequence_name;
SELECT LASTVAL(sequence_name);
SHOW CREATE SEQUENCE sequence_name;
SHOW CREATE TABLE sequence_name;
CREATE TABLE sequence-structure ... SEQUENCE=1
ALTER TABLE sequence RENAME TO sequence2;
RENAME TABLE sequence TO sequence2;
DROP [TEMPORARY] SEQUENCE [IF EXISTS] sequence_names
Missing features
- SETVAL(value,sequence_name), to be used with replication.
- Check replication, including checking that sequence tables are marked
not transactional.
- Check that a commit happens for NEXT VALUE that changes table data (may
already work)
- ALTER SEQUENCE. ANSI SQL version of setval.
- Share identical sequence entries to not add things twice to table list.
- testing insert/delete/update/truncate/load data
- Run and fix Alibaba sequence tests (part of mysql-test/suite/sql_sequence)
- Write documentation for NEXT VALUE / PREVIOUS_VALUE
- NEXTVAL in DEFAULT
- Ensure that NEXTVAL in DEFAULT uses database from base table
- Two NEXTVAL for same row should give same answer.
- Oracle syntax sequence_table.nextval, without any FOR or FROM.
- Sequence tables are treated as 'not read constant tables' by SELECT; Would
be better if we would have a separate list for sequence tables so that
select doesn't know about them, except if refereed to with FROM.
Other things done:
- Improved output for safemalloc backtrack
- frm_type_enum changed to Table_type
- Removed lex->is_view and replaced with lex->table_type. This allows
use to more easy check if item is view, sequence or table.
- Added table flag HA_CAN_TABLES_WITHOUT_ROLLBACK, needed for handlers
that want's to support sequences
- Added handler calls:
- engine_name(), to simplify getting engine name for partition and sequences
- update_first_row(), to be able to do efficient sequence implementations.
- Made binlog_log_row() global to be able to call it from ha_sequence.cc
- Added handler variable: row_already_logged, to be able to flag that the
changed row is already logging to replication log.
- Added CF_DB_CHANGE and CF_SCHEMA_CHANGE flags to simplify
deny_updates_if_read_only_option()
- Added sp_add_cfetch() to avoid new conflicts in sql_yacc.yy
- Moved code for add_table_options() out from sql_show.cc::show_create_table()
- Added String::append_longlong() and used it in sql_show.cc to simplify code.
- Added extra option to dd_frm_type() and ha_table_exists to indicate if
the table is a sequence. Needed by DROP SQUENCE to not drop a table.
2017-03-25 23:36:56 +02:00
|
|
|
static handlerton *sequence_hton;
|
2015-10-02 10:18:27 +02:00
|
|
|
|
2013-07-24 16:51:48 +04:00
|
|
|
class Sequence_share : public Handler_share {
|
2013-07-23 17:38:44 +04:00
|
|
|
public:
|
2013-04-09 16:17:16 +02:00
|
|
|
const char *name;
|
|
|
|
THR_LOCK lock;
|
|
|
|
|
|
|
|
ulonglong from, to, step;
|
|
|
|
bool reverse;
|
2013-07-23 17:38:44 +04:00
|
|
|
|
2013-07-24 16:51:48 +04:00
|
|
|
Sequence_share(const char *name_arg, ulonglong from_arg, ulonglong to_arg,
|
|
|
|
ulonglong step_arg, bool reverse_arg):
|
2013-07-23 17:38:44 +04:00
|
|
|
name(name_arg), from(from_arg), to(to_arg), step(step_arg),
|
|
|
|
reverse(reverse_arg)
|
|
|
|
{
|
|
|
|
thr_lock_init(&lock);
|
|
|
|
}
|
2013-07-24 16:51:48 +04:00
|
|
|
~Sequence_share()
|
2013-07-23 17:38:44 +04:00
|
|
|
{
|
|
|
|
thr_lock_delete(&lock);
|
|
|
|
}
|
|
|
|
};
|
2013-04-09 16:17:16 +02:00
|
|
|
|
2020-07-27 18:46:37 +03:00
|
|
|
class ha_seq final : public handler
|
2013-04-09 16:17:16 +02:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
THR_LOCK_DATA lock;
|
2013-07-24 16:51:48 +04:00
|
|
|
Sequence_share *get_share();
|
2013-04-09 16:17:16 +02:00
|
|
|
ulonglong cur;
|
|
|
|
|
|
|
|
public:
|
2015-10-02 10:18:27 +02:00
|
|
|
Sequence_share *seqs;
|
2013-04-09 16:17:16 +02:00
|
|
|
ha_seq(handlerton *hton, TABLE_SHARE *table_arg)
|
|
|
|
: handler(hton, table_arg), seqs(0) { }
|
2022-11-21 14:24:00 +02:00
|
|
|
ulonglong table_flags() const override
|
2013-06-16 17:19:53 +02:00
|
|
|
{ return HA_BINLOG_ROW_CAPABLE | HA_BINLOG_STMT_CAPABLE; }
|
2013-04-09 16:17:16 +02:00
|
|
|
|
|
|
|
/* open/close/locking */
|
|
|
|
int create(const char *name, TABLE *table_arg,
|
2022-11-21 14:24:00 +02:00
|
|
|
HA_CREATE_INFO *create_info) override
|
2020-06-01 23:27:14 +03:00
|
|
|
{ return HA_ERR_WRONG_COMMAND; }
|
2013-04-09 16:17:16 +02:00
|
|
|
|
2022-11-21 14:24:00 +02:00
|
|
|
int open(const char *name, int mode, uint test_if_locked) override;
|
|
|
|
int close(void) override;
|
|
|
|
int delete_table(const char *name) override
|
2020-06-01 23:27:14 +03:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2022-11-21 14:24:00 +02:00
|
|
|
THR_LOCK_DATA **store_lock(THD *, THR_LOCK_DATA **, enum thr_lock_type)
|
|
|
|
override;
|
2013-04-09 16:17:16 +02:00
|
|
|
|
|
|
|
/* table scan */
|
2022-11-21 14:24:00 +02:00
|
|
|
int rnd_init(bool scan) override;
|
|
|
|
int rnd_next(unsigned char *buf) override;
|
|
|
|
void position(const uchar *record) override;
|
|
|
|
int rnd_pos(uchar *buf, uchar *pos) override;
|
|
|
|
int info(uint flag) override;
|
2022-09-30 17:10:37 +03:00
|
|
|
IO_AND_CPU_COST keyread_time(uint index, ulong ranges, ha_rows rows,
|
|
|
|
ulonglong blocks) override
|
|
|
|
{
|
|
|
|
/* Avoids assert in total_cost() and makes DBUG_PRINT more consistent */
|
|
|
|
return {0,0};
|
|
|
|
}
|
2022-11-21 14:24:00 +02:00
|
|
|
IO_AND_CPU_COST scan_time() override
|
2022-09-30 17:10:37 +03:00
|
|
|
{
|
|
|
|
/* Avoids assert in total_cost() and makes DBUG_PRINT more consistent */
|
|
|
|
return {0, 0};
|
|
|
|
}
|
2013-04-09 16:17:16 +02:00
|
|
|
/* indexes */
|
2022-11-21 14:24:00 +02:00
|
|
|
ulong index_flags(uint inx, uint part, bool all_parts) const override
|
2013-04-09 16:17:16 +02:00
|
|
|
{ return HA_READ_NEXT | HA_READ_PREV | HA_READ_ORDER |
|
|
|
|
HA_READ_RANGE | HA_KEYREAD_ONLY; }
|
2022-11-21 14:24:00 +02:00
|
|
|
uint max_supported_keys() const override { return 1; }
|
2013-04-09 16:17:16 +02:00
|
|
|
int index_read_map(uchar *buf, const uchar *key, key_part_map keypart_map,
|
2022-11-21 14:24:00 +02:00
|
|
|
enum ha_rkey_function find_flag) override;
|
|
|
|
int index_next(uchar *buf) override;
|
|
|
|
int index_prev(uchar *buf) override;
|
|
|
|
int index_first(uchar *buf) override;
|
|
|
|
int index_last(uchar *buf) override;
|
2020-02-27 19:12:27 +02:00
|
|
|
ha_rows records_in_range(uint inx, const key_range *start_key,
|
2022-11-21 14:24:00 +02:00
|
|
|
const key_range *end_key, page_range *pages) override;
|
2013-04-09 16:17:16 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
void set(uchar *buf);
|
|
|
|
ulonglong nvalues() { return (seqs->to - seqs->from)/seqs->step; }
|
|
|
|
};
|
|
|
|
|
|
|
|
THR_LOCK_DATA **ha_seq::store_lock(THD *thd, THR_LOCK_DATA **to,
|
|
|
|
enum thr_lock_type lock_type)
|
|
|
|
{
|
|
|
|
if (lock_type != TL_IGNORE && lock.type == TL_UNLOCK)
|
|
|
|
lock.type= TL_WRITE_ALLOW_WRITE;
|
|
|
|
*to ++= &lock;
|
|
|
|
return to;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ha_seq::set(unsigned char *buf)
|
|
|
|
{
|
MDEV-17556 Assertion `bitmap_is_set_all(&table->s->all_set)' failed
The assertion failed in handler::ha_reset upon SELECT under
READ UNCOMMITTED from table with index on virtual column.
This was the debug-only failure, though the problem is mush wider:
* MY_BITMAP is a structure containing my_bitmap_map, the latter is a raw
bitmap.
* read_set, write_set and vcol_set of TABLE are the pointers to MY_BITMAP
* The rest of MY_BITMAPs are stored in TABLE and TABLE_SHARE
* The pointers to the stored MY_BITMAPs, like orig_read_set etc, and
sometimes all_set and tmp_set, are assigned to the pointers.
* Sometimes tmp_use_all_columns is used to substitute the raw bitmap
directly with all_set.bitmap
* Sometimes even bitmaps are directly modified, like in
TABLE::update_virtual_field(): bitmap_clear_all(&tmp_set) is called.
The last three bullets in the list, when used together (which is mostly
always) make the program flow cumbersome and impossible to follow,
notwithstanding the errors they cause, like this MDEV-17556, where tmp_set
pointer was assigned to read_set, write_set and vcol_set, then its bitmap
was substituted with all_set.bitmap by dbug_tmp_use_all_columns() call,
and then bitmap_clear_all(&tmp_set) was applied to all this.
To untangle this knot, the rule should be applied:
* Never substitute bitmaps! This patch is about this.
orig_*, all_set bitmaps are never substituted already.
This patch changes the following function prototypes:
* tmp_use_all_columns, dbug_tmp_use_all_columns
to accept MY_BITMAP** and to return MY_BITMAP * instead of my_bitmap_map*
* tmp_restore_column_map, dbug_tmp_restore_column_maps to accept
MY_BITMAP* instead of my_bitmap_map*
These functions now will substitute read_set/write_set/vcol_set directly,
and won't touch underlying bitmaps.
2020-12-29 13:38:16 +10:00
|
|
|
MY_BITMAP *old_map = dbug_tmp_use_all_columns(table, &table->write_set);
|
2013-04-09 16:17:16 +02:00
|
|
|
my_ptrdiff_t offset = (my_ptrdiff_t) (buf - table->record[0]);
|
|
|
|
Field *field = table->field[0];
|
|
|
|
field->move_field_offset(offset);
|
|
|
|
field->store(cur, true);
|
|
|
|
field->move_field_offset(-offset);
|
MDEV-17556 Assertion `bitmap_is_set_all(&table->s->all_set)' failed
The assertion failed in handler::ha_reset upon SELECT under
READ UNCOMMITTED from table with index on virtual column.
This was the debug-only failure, though the problem is mush wider:
* MY_BITMAP is a structure containing my_bitmap_map, the latter is a raw
bitmap.
* read_set, write_set and vcol_set of TABLE are the pointers to MY_BITMAP
* The rest of MY_BITMAPs are stored in TABLE and TABLE_SHARE
* The pointers to the stored MY_BITMAPs, like orig_read_set etc, and
sometimes all_set and tmp_set, are assigned to the pointers.
* Sometimes tmp_use_all_columns is used to substitute the raw bitmap
directly with all_set.bitmap
* Sometimes even bitmaps are directly modified, like in
TABLE::update_virtual_field(): bitmap_clear_all(&tmp_set) is called.
The last three bullets in the list, when used together (which is mostly
always) make the program flow cumbersome and impossible to follow,
notwithstanding the errors they cause, like this MDEV-17556, where tmp_set
pointer was assigned to read_set, write_set and vcol_set, then its bitmap
was substituted with all_set.bitmap by dbug_tmp_use_all_columns() call,
and then bitmap_clear_all(&tmp_set) was applied to all this.
To untangle this knot, the rule should be applied:
* Never substitute bitmaps! This patch is about this.
orig_*, all_set bitmaps are never substituted already.
This patch changes the following function prototypes:
* tmp_use_all_columns, dbug_tmp_use_all_columns
to accept MY_BITMAP** and to return MY_BITMAP * instead of my_bitmap_map*
* tmp_restore_column_map, dbug_tmp_restore_column_maps to accept
MY_BITMAP* instead of my_bitmap_map*
These functions now will substitute read_set/write_set/vcol_set directly,
and won't touch underlying bitmaps.
2020-12-29 13:38:16 +10:00
|
|
|
dbug_tmp_restore_column_map(&table->write_set, old_map);
|
2013-04-09 16:17:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int ha_seq::rnd_init(bool scan)
|
|
|
|
{
|
|
|
|
cur= seqs->reverse ? seqs->to : seqs->from;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ha_seq::rnd_next(unsigned char *buf)
|
|
|
|
{
|
|
|
|
if (seqs->reverse)
|
|
|
|
return index_prev(buf);
|
|
|
|
else
|
|
|
|
return index_next(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ha_seq::position(const uchar *record)
|
|
|
|
{
|
|
|
|
*(ulonglong*)ref= cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ha_seq::rnd_pos(uchar *buf, uchar *pos)
|
|
|
|
{
|
|
|
|
cur= *(ulonglong*)pos;
|
|
|
|
return rnd_next(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ha_seq::info(uint flag)
|
|
|
|
{
|
|
|
|
if (flag & HA_STATUS_VARIABLE)
|
|
|
|
stats.records = nvalues();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ha_seq::index_read_map(uchar *buf, const uchar *key_arg,
|
|
|
|
key_part_map keypart_map,
|
|
|
|
enum ha_rkey_function find_flag)
|
|
|
|
{
|
|
|
|
ulonglong key= uint8korr(key_arg);
|
|
|
|
switch (find_flag) {
|
|
|
|
case HA_READ_AFTER_KEY:
|
|
|
|
key++;
|
|
|
|
// fall through
|
|
|
|
case HA_READ_KEY_OR_NEXT:
|
|
|
|
if (key <= seqs->from)
|
|
|
|
cur= seqs->from;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cur= (key - seqs->from + seqs->step - 1) / seqs->step * seqs->step + seqs->from;
|
|
|
|
if (cur >= seqs->to)
|
|
|
|
return HA_ERR_KEY_NOT_FOUND;
|
|
|
|
}
|
|
|
|
return index_next(buf);
|
|
|
|
|
|
|
|
case HA_READ_KEY_EXACT:
|
|
|
|
if ((key - seqs->from) % seqs->step != 0 || key < seqs->from || key >= seqs->to)
|
|
|
|
return HA_ERR_KEY_NOT_FOUND;
|
|
|
|
cur= key;
|
|
|
|
return index_next(buf);
|
|
|
|
|
|
|
|
case HA_READ_BEFORE_KEY:
|
|
|
|
key--;
|
|
|
|
// fall through
|
|
|
|
case HA_READ_PREFIX_LAST_OR_PREV:
|
|
|
|
if (key >= seqs->to)
|
|
|
|
cur= seqs->to;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (key < seqs->from)
|
|
|
|
return HA_ERR_KEY_NOT_FOUND;
|
|
|
|
cur= (key - seqs->from) / seqs->step * seqs->step + seqs->from;
|
|
|
|
}
|
|
|
|
return index_prev(buf);
|
|
|
|
default: return HA_ERR_WRONG_COMMAND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ha_seq::index_next(uchar *buf)
|
|
|
|
{
|
|
|
|
if (cur == seqs->to)
|
|
|
|
return HA_ERR_END_OF_FILE;
|
|
|
|
set(buf);
|
|
|
|
cur+= seqs->step;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ha_seq::index_prev(uchar *buf)
|
|
|
|
{
|
|
|
|
if (cur == seqs->from)
|
|
|
|
return HA_ERR_END_OF_FILE;
|
|
|
|
cur-= seqs->step;
|
|
|
|
set(buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ha_seq::index_first(uchar *buf)
|
|
|
|
{
|
|
|
|
cur= seqs->from;
|
|
|
|
return index_next(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ha_seq::index_last(uchar *buf)
|
|
|
|
{
|
|
|
|
cur= seqs->to;
|
|
|
|
return index_prev(buf);
|
|
|
|
}
|
|
|
|
|
2020-02-27 19:12:27 +02:00
|
|
|
ha_rows ha_seq::records_in_range(uint inx, const key_range *min_key,
|
|
|
|
const key_range *max_key,
|
|
|
|
page_range *pages)
|
2013-04-09 16:17:16 +02:00
|
|
|
{
|
|
|
|
ulonglong kmin= min_key ? uint8korr(min_key->key) : seqs->from;
|
|
|
|
ulonglong kmax= max_key ? uint8korr(max_key->key) : seqs->to - 1;
|
|
|
|
if (kmin >= seqs->to || kmax < seqs->from || kmin > kmax)
|
|
|
|
return 0;
|
|
|
|
return (kmax - seqs->from) / seqs->step -
|
|
|
|
(kmin - seqs->from + seqs->step - 1) / seqs->step + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ha_seq::open(const char *name, int mode, uint test_if_locked)
|
|
|
|
{
|
2013-07-23 17:38:44 +04:00
|
|
|
if (!(seqs= get_share()))
|
|
|
|
return HA_ERR_OUT_OF_MEM;
|
2013-04-09 16:17:16 +02:00
|
|
|
DBUG_ASSERT(my_strcasecmp(table_alias_charset, name, seqs->name) == 0);
|
|
|
|
|
|
|
|
ref_length= sizeof(cur);
|
|
|
|
thr_lock_data_init(&seqs->lock,&lock,NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ha_seq::close(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static handler *create_handler(handlerton *hton, TABLE_SHARE *table,
|
|
|
|
MEM_ROOT *mem_root)
|
|
|
|
{
|
|
|
|
return new (mem_root) ha_seq(hton, table);
|
|
|
|
}
|
|
|
|
|
2013-06-16 17:07:15 +02:00
|
|
|
|
|
|
|
static bool parse_table_name(const char *name, size_t name_length,
|
|
|
|
ulonglong *from, ulonglong *to, ulonglong *step)
|
2013-04-09 16:17:16 +02:00
|
|
|
{
|
2014-03-01 13:27:04 +01:00
|
|
|
uint n0=0, n1= 0, n2= 0;
|
2013-06-16 17:07:15 +02:00
|
|
|
*step= 1;
|
|
|
|
|
|
|
|
// the table is discovered if its name matches the pattern of seq_1_to_10 or
|
|
|
|
// seq_1_to_10_step_3
|
2014-03-01 13:27:04 +01:00
|
|
|
sscanf(name, "seq_%llu_to_%n%llu%n_step_%llu%n",
|
|
|
|
from, &n0, to, &n1, step, &n2);
|
|
|
|
// I consider this a bug in sscanf() - when an unsigned number
|
|
|
|
// is requested, -5 should *not* be accepted. But is is :(
|
|
|
|
// hence the additional check below:
|
|
|
|
return
|
|
|
|
n0 == 0 || !isdigit(name[4]) || !isdigit(name[n0]) || // reject negative numbers
|
|
|
|
(n1 != name_length && n2 != name_length);
|
2013-06-16 17:07:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-24 16:51:48 +04:00
|
|
|
Sequence_share *ha_seq::get_share()
|
2013-07-23 17:38:44 +04:00
|
|
|
{
|
2013-07-24 16:51:48 +04:00
|
|
|
Sequence_share *tmp_share;
|
2013-07-23 17:38:44 +04:00
|
|
|
lock_shared_ha_data();
|
2013-07-24 16:51:48 +04:00
|
|
|
if (!(tmp_share= static_cast<Sequence_share*>(get_ha_share_ptr())))
|
2013-07-23 17:38:44 +04:00
|
|
|
{
|
|
|
|
bool reverse;
|
|
|
|
ulonglong from, to, step;
|
|
|
|
|
|
|
|
parse_table_name(table_share->table_name.str,
|
|
|
|
table_share->table_name.length, &from, &to, &step);
|
|
|
|
|
|
|
|
if ((reverse = from > to))
|
|
|
|
{
|
|
|
|
if (step > from - to)
|
|
|
|
to = from;
|
|
|
|
else
|
|
|
|
swap_variables(ulonglong, from, to);
|
|
|
|
/*
|
|
|
|
when keyread is allowed, optimizer will always prefer an index to a
|
|
|
|
table scan for our tables, and we'll never see the range reversed.
|
|
|
|
*/
|
|
|
|
table_share->keys_for_keyread.clear_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
to= (to - from) / step * step + step + from;
|
|
|
|
|
2013-07-24 16:51:48 +04:00
|
|
|
tmp_share= new Sequence_share(table_share->normalized_path.str, from, to, step, reverse);
|
2013-07-23 17:38:44 +04:00
|
|
|
|
|
|
|
if (!tmp_share)
|
|
|
|
goto err;
|
|
|
|
set_ha_share_ptr(static_cast<Handler_share*>(tmp_share));
|
|
|
|
}
|
|
|
|
err:
|
|
|
|
unlock_shared_ha_data();
|
|
|
|
return tmp_share;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-16 17:07:15 +02:00
|
|
|
static int discover_table(handlerton *hton, THD *thd, TABLE_SHARE *share)
|
|
|
|
{
|
|
|
|
ulonglong from, to, step;
|
|
|
|
if (parse_table_name(share->table_name.str, share->table_name.length,
|
|
|
|
&from, &to, &step))
|
2013-04-09 16:17:16 +02:00
|
|
|
return HA_ERR_NO_SUCH_TABLE;
|
|
|
|
|
|
|
|
if (step == 0)
|
|
|
|
return HA_WRONG_CREATE_OPTION;
|
|
|
|
|
|
|
|
const char *sql="create table seq (seq bigint unsigned primary key)";
|
2013-07-23 17:38:44 +04:00
|
|
|
return share->init_from_sql_statement_string(thd, 0, sql, strlen(sql));
|
2013-04-09 16:17:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-16 17:07:15 +02:00
|
|
|
static int discover_table_existence(handlerton *hton, const char *db,
|
|
|
|
const char *table_name)
|
|
|
|
{
|
|
|
|
ulonglong from, to, step;
|
|
|
|
return !parse_table_name(table_name, strlen(table_name), &from, &to, &step);
|
|
|
|
}
|
|
|
|
|
2019-03-12 16:27:19 +01:00
|
|
|
static int dummy_commit_rollback(handlerton *, THD *, bool) { return 0; }
|
|
|
|
|
|
|
|
static int dummy_savepoint(handlerton *, THD *, void *) { return 0; }
|
2013-04-09 16:17:16 +02:00
|
|
|
|
2015-10-02 10:18:27 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
Example of a simple group by handler for queries like:
|
|
|
|
SELECT SUM(seq) from sequence_table;
|
|
|
|
|
|
|
|
This implementation supports SUM() and COUNT() on primary key.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
class ha_seq_group_by_handler: public group_by_handler
|
|
|
|
{
|
2019-10-11 13:08:53 +02:00
|
|
|
Select_limit_counters limit;
|
2015-10-02 14:38:06 +02:00
|
|
|
List<Item> *fields;
|
|
|
|
TABLE_LIST *table_list;
|
2015-10-02 10:18:27 +02:00
|
|
|
bool first_row;
|
|
|
|
|
|
|
|
public:
|
2015-10-02 10:19:40 +02:00
|
|
|
ha_seq_group_by_handler(THD *thd_arg, List<Item> *fields_arg,
|
2019-10-11 13:08:53 +02:00
|
|
|
TABLE_LIST *table_list_arg,
|
|
|
|
Select_limit_counters *orig_lim)
|
|
|
|
: group_by_handler(thd_arg, sequence_hton), limit(orig_lim[0]),
|
|
|
|
fields(fields_arg), table_list(table_list_arg)
|
|
|
|
{
|
|
|
|
// Reset limit because we are handling it now
|
|
|
|
orig_lim->set_unlimited();
|
|
|
|
}
|
2023-02-07 13:57:20 +02:00
|
|
|
~ha_seq_group_by_handler() = default;
|
2015-10-02 10:18:27 +02:00
|
|
|
int init_scan() { first_row= 1 ; return 0; }
|
|
|
|
int next_row();
|
|
|
|
int end_scan() { return 0; }
|
|
|
|
};
|
|
|
|
|
|
|
|
static group_by_handler *
|
2015-10-02 18:40:38 +02:00
|
|
|
create_group_by_handler(THD *thd, Query *query)
|
2015-10-02 10:18:27 +02:00
|
|
|
{
|
|
|
|
ha_seq_group_by_handler *handler;
|
|
|
|
Item *item;
|
2015-10-02 18:40:38 +02:00
|
|
|
List_iterator_fast<Item> it(*query->select);
|
2015-10-02 10:18:27 +02:00
|
|
|
|
|
|
|
/* check that only one table is used in FROM clause and no sub queries */
|
2015-10-02 18:40:38 +02:00
|
|
|
if (query->from->next_local != 0)
|
2015-10-02 10:18:27 +02:00
|
|
|
return 0;
|
|
|
|
/* check that there is no where clause and no group_by */
|
2015-10-02 18:40:38 +02:00
|
|
|
if (query->where != 0 || query->group_by != 0)
|
2015-10-02 10:18:27 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Check that all fields are sum(primary_key) or count(primary_key)
|
|
|
|
For more ways to work with the field list and sum functions, see
|
|
|
|
opt_sum.cc::opt_sum_query().
|
|
|
|
*/
|
|
|
|
while ((item= it++))
|
|
|
|
{
|
|
|
|
Item *arg0;
|
|
|
|
Field *field;
|
|
|
|
if (item->type() != Item::SUM_FUNC_ITEM ||
|
|
|
|
(((Item_sum*) item)->sum_func() != Item_sum::SUM_FUNC &&
|
|
|
|
((Item_sum*) item)->sum_func() != Item_sum::COUNT_FUNC))
|
|
|
|
|
|
|
|
return 0; // Not a SUM() function
|
|
|
|
arg0= ((Item_sum*) item)->get_arg(0);
|
|
|
|
if (arg0->type() != Item::FIELD_ITEM)
|
|
|
|
{
|
|
|
|
if ((((Item_sum*) item)->sum_func() == Item_sum::COUNT_FUNC) &&
|
|
|
|
arg0->basic_const_item())
|
|
|
|
continue; // Allow count(1)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
field= ((Item_field*) arg0)->field;
|
|
|
|
/*
|
|
|
|
Check that we are using the sequence table (the only table in the FROM
|
|
|
|
clause) and not an outer table.
|
|
|
|
*/
|
2015-10-02 18:40:38 +02:00
|
|
|
if (field->table != query->from->table)
|
2015-10-02 10:18:27 +02:00
|
|
|
return 0;
|
|
|
|
/* Check that we are using a SUM() on the primary key */
|
2017-04-23 19:39:57 +03:00
|
|
|
if (strcmp(field->field_name.str, "seq"))
|
2015-10-02 10:18:27 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create handler and return it */
|
2019-10-11 13:08:53 +02:00
|
|
|
handler= new ha_seq_group_by_handler(thd, query->select, query->from,
|
|
|
|
query->limit);
|
2015-10-02 10:18:27 +02:00
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ha_seq_group_by_handler::next_row()
|
|
|
|
{
|
|
|
|
List_iterator_fast<Item> it(*fields);
|
|
|
|
Item_sum *item_sum;
|
|
|
|
Sequence_share *seqs= ((ha_seq*) table_list->table->file)->seqs;
|
2015-10-05 09:47:45 +02:00
|
|
|
DBUG_ENTER("ha_seq_group_by_handler::next_row");
|
2015-10-02 10:18:27 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Check if this is the first call to the function. If not, we have already
|
|
|
|
returned all data.
|
|
|
|
*/
|
2019-10-11 13:08:53 +02:00
|
|
|
if (!first_row ||
|
|
|
|
limit.get_offset_limit() > 0 ||
|
|
|
|
limit.get_select_limit() == 0)
|
2015-10-02 10:18:27 +02:00
|
|
|
DBUG_RETURN(HA_ERR_END_OF_FILE);
|
|
|
|
first_row= 0;
|
|
|
|
|
|
|
|
/* Pointer to first field in temporary table where we should store summary*/
|
|
|
|
Field **field_ptr= table->field;
|
|
|
|
ulonglong elements= (seqs->to - seqs->from + seqs->step - 1) / seqs->step;
|
|
|
|
|
|
|
|
while ((item_sum= (Item_sum*) it++))
|
|
|
|
{
|
|
|
|
Field *field= *(field_ptr++);
|
|
|
|
switch (item_sum->sum_func()) {
|
|
|
|
case Item_sum::COUNT_FUNC:
|
|
|
|
{
|
2016-02-22 12:52:29 +01:00
|
|
|
Item *arg0= ((Item_sum*) item_sum)->get_arg(0);
|
|
|
|
if (arg0->basic_const_item() && arg0->is_null())
|
|
|
|
field->store(0LL, 1);
|
|
|
|
else
|
|
|
|
field->store((longlong) elements, 1);
|
2015-10-02 10:18:27 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Item_sum::SUM_FUNC:
|
|
|
|
{
|
|
|
|
/* Calculate SUM(f, f+step, f+step*2 ... to) */
|
|
|
|
ulonglong sum;
|
|
|
|
sum= seqs->from * elements + seqs->step * (elements*elements-elements)/2;
|
|
|
|
field->store((longlong) sum, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
DBUG_ASSERT(0);
|
|
|
|
}
|
|
|
|
field->set_notnull();
|
|
|
|
}
|
|
|
|
DBUG_RETURN(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
|
|
|
static void sequence_update_optimizer_costs(OPTIMIZER_COSTS *costs)
|
|
|
|
{
|
2022-09-30 17:10:37 +03:00
|
|
|
costs->disk_read_cost= 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
|
|
|
costs->disk_read_ratio= 0.0; // No disk
|
2022-09-30 17:10:37 +03:00
|
|
|
costs->key_next_find_cost=
|
|
|
|
costs->key_lookup_cost=
|
|
|
|
costs->key_copy_cost=
|
|
|
|
costs->row_next_find_cost=
|
|
|
|
costs->row_lookup_cost=
|
|
|
|
costs->row_copy_cost= 0.0000062391530550;
|
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
|
|
|
}
|
2015-10-02 10:18:27 +02:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
Initialize the interface between the sequence engine and MariaDB
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2020-06-16 10:33:48 +02:00
|
|
|
static int drop_table(handlerton *hton, const char *path)
|
|
|
|
{
|
|
|
|
const char *name= strrchr(path, FN_LIBCHAR)+1;
|
|
|
|
ulonglong from, to, step;
|
|
|
|
if (parse_table_name(name, strlen(name), &from, &to, &step))
|
|
|
|
return ENOENT;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-09 16:17:16 +02:00
|
|
|
static int init(void *p)
|
|
|
|
{
|
2013-06-16 17:07:15 +02:00
|
|
|
handlerton *hton= (handlerton *)p;
|
2015-10-02 10:18:27 +02:00
|
|
|
sequence_hton= hton;
|
2013-06-16 17:07:15 +02:00
|
|
|
hton->create= create_handler;
|
2020-06-16 10:33:48 +02:00
|
|
|
hton->drop_table= drop_table;
|
2013-06-16 17:07:15 +02:00
|
|
|
hton->discover_table= discover_table;
|
|
|
|
hton->discover_table_existence= discover_table_existence;
|
2019-03-12 16:27:19 +01:00
|
|
|
hton->commit= hton->rollback= dummy_commit_rollback;
|
2013-04-09 16:17:16 +02:00
|
|
|
hton->savepoint_set= hton->savepoint_rollback= hton->savepoint_release=
|
2019-03-12 16:27:19 +01:00
|
|
|
dummy_savepoint;
|
2015-10-02 10:18:27 +02:00
|
|
|
hton->create_group_by= create_group_by_handler;
|
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
|
|
|
hton->update_optimizer_costs= sequence_update_optimizer_costs;
|
2013-04-09 16:17:16 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct st_mysql_storage_engine descriptor =
|
|
|
|
{ MYSQL_HANDLERTON_INTERFACE_VERSION };
|
|
|
|
|
2013-04-09 16:19:10 +02:00
|
|
|
maria_declare_plugin(sequence)
|
2013-04-09 16:17:16 +02:00
|
|
|
{
|
|
|
|
MYSQL_STORAGE_ENGINE_PLUGIN,
|
|
|
|
&descriptor,
|
|
|
|
"SEQUENCE",
|
|
|
|
"Sergei Golubchik",
|
|
|
|
"Generated tables filled with sequential values",
|
|
|
|
PLUGIN_LICENSE_GPL,
|
|
|
|
init,
|
|
|
|
NULL,
|
|
|
|
0x0100,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
"0.1",
|
2014-06-13 13:25:32 +02:00
|
|
|
MariaDB_PLUGIN_MATURITY_STABLE
|
2013-04-09 16:17:16 +02:00
|
|
|
}
|
|
|
|
maria_declare_plugin_end;
|