Commit 695ce3ad authored by Rich Prohaska's avatar Rich Prohaska

DB-756 set cardinality data for partitioned tokudb tables

parent 10532d81
set default_storage_engine='tokudb';
drop table if exists t;
create table t (id int, x int, primary key (id), key (x)) partition by hash(id) partitions 2;
show indexes from t;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
t 0 PRIMARY 1 id A 2 NULL NULL BTREE
t 1 x 1 x A NULL NULL NULL YES BTREE
insert into t values (1,1),(3,1),(5,1);
insert into t values (2,1),(4,1),(6,1);
show indexes from t;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
t 0 PRIMARY 1 id A 6 NULL NULL BTREE
t 1 x 1 x A NULL NULL NULL YES BTREE
analyze table t;
Table Op Msg_type Msg_text
test.t analyze status OK
show indexes from t;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
t 0 PRIMARY 1 id A 6 NULL NULL BTREE
t 1 x 1 x A 2 NULL NULL YES BTREE
drop table t;
set default_storage_engine='tokudb';
drop table if exists t;
create table t (id int, x int, primary key (id), key (x)) partition by hash(id) partitions 2;
show indexes from t;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
t 0 PRIMARY 1 id A 2 NULL NULL BTREE
t 1 x 1 x A NULL NULL NULL YES BTREE
insert into t values (1,1),(3,1),(5,1);
show indexes from t;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
t 0 PRIMARY 1 id A 4 NULL NULL BTREE
t 1 x 1 x A NULL NULL NULL YES BTREE
analyze table t;
Table Op Msg_type Msg_text
test.t analyze status OK
show indexes from t;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
t 0 PRIMARY 1 id A 4 NULL NULL BTREE
t 1 x 1 x A 1 NULL NULL YES BTREE
drop table t;
set default_storage_engine='tokudb';
drop table if exists t;
create table t (id int, x int, primary key (id), key (x)) partition by hash(id) partitions 2;
show indexes from t;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
t 0 PRIMARY 1 id A 2 NULL NULL BTREE
t 1 x 1 x A NULL NULL NULL YES BTREE
insert into t values (1,1),(3,2),(5,3);
insert into t values (2,1),(4,1),(6,1),(8,1);
show indexes from t;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
t 0 PRIMARY 1 id A 7 NULL NULL BTREE
t 1 x 1 x A NULL NULL NULL YES BTREE
analyze table t;
Table Op Msg_type Msg_text
test.t analyze status OK
show indexes from t;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
t 0 PRIMARY 1 id A 7 NULL NULL BTREE
t 1 x 1 x A 1 NULL NULL YES BTREE
drop table t;
set default_storage_engine='tokudb';
drop table if exists t;
create table t (id int, x int, primary key (id), key (x)) partition by hash(id) partitions 2;
show indexes from t;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
t 0 PRIMARY 1 id A 2 NULL NULL BTREE
t 1 x 1 x A NULL NULL NULL YES BTREE
insert into t values (2,1),(4,1),(6,1);
show indexes from t;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
t 0 PRIMARY 1 id A 4 NULL NULL BTREE
t 1 x 1 x A NULL NULL NULL YES BTREE
analyze table t;
Table Op Msg_type Msg_text
test.t analyze status OK
show indexes from t;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
t 0 PRIMARY 1 id A 4 NULL NULL BTREE
t 1 x 1 x A 1 NULL NULL YES BTREE
drop table t;
set default_storage_engine='tokudb';
drop table if exists t;
create table t (id int, x int, primary key (id), key (x)) partition by hash(id) partitions 2;
show indexes from t;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
t 0 PRIMARY 1 id A 2 NULL NULL BTREE
t 1 x 1 x A NULL NULL NULL YES BTREE
insert into t values (1,1),(3,2),(5,3),(7,4);
insert into t values (2,1),(4,1),(6,1);
show indexes from t;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
t 0 PRIMARY 1 id A 7 NULL NULL BTREE
t 1 x 1 x A NULL NULL NULL YES BTREE
analyze table t;
Table Op Msg_type Msg_text
test.t analyze status OK
show indexes from t;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
t 0 PRIMARY 1 id A 7 NULL NULL BTREE
t 1 x 1 x A 7 NULL NULL YES BTREE
drop table t;
# DB-756 verify that cardinality is picked from SOME partition
source include/have_tokudb.inc;
source include/have_partition.inc;
set default_storage_engine='tokudb';
disable_warnings;
drop table if exists t;
enable_warnings;
create table t (id int, x int, primary key (id), key (x)) partition by hash(id) partitions 2;
show indexes from t;
insert into t values (1,1),(3,1),(5,1);
insert into t values (2,1),(4,1),(6,1);
show indexes from t;
analyze table t;
show indexes from t;
drop table t;
# DB-756 verify that cardinality is picked from the partition where the rows are mapped. in this case, the last partition.
source include/have_tokudb.inc;
source include/have_partition.inc;
set default_storage_engine='tokudb';
disable_warnings;
drop table if exists t;
enable_warnings;
create table t (id int, x int, primary key (id), key (x)) partition by hash(id) partitions 2;
show indexes from t;
insert into t values (1,1),(3,1),(5,1);
show indexes from t;
analyze table t;
show indexes from t;
drop table t;
# DB-756 verify that cardinality is chosen from the first partition
source include/have_tokudb.inc;
source include/have_partition.inc;
set default_storage_engine='tokudb';
disable_warnings;
drop table if exists t;
enable_warnings;
create table t (id int, x int, primary key (id), key (x)) partition by hash(id) partitions 2;
show indexes from t;
insert into t values (1,1),(3,2),(5,3);
insert into t values (2,1),(4,1),(6,1),(8,1);
show indexes from t;
analyze table t;
show indexes from t;
drop table t;
# DB-756 verify that cardinality is picked from the partition where the rows are mapped. in this case, the first partition.
source include/have_tokudb.inc;
source include/have_partition.inc;
set default_storage_engine='tokudb';
disable_warnings;
drop table if exists t;
enable_warnings;
create table t (id int, x int, primary key (id), key (x)) partition by hash(id) partitions 2;
show indexes from t;
insert into t values (2,1),(4,1),(6,1);
show indexes from t;
analyze table t;
show indexes from t;
drop table t;
# DB-756 verify that cardinality is chosen from the last partition
source include/have_tokudb.inc;
source include/have_partition.inc;
set default_storage_engine='tokudb';
disable_warnings;
drop table if exists t;
enable_warnings;
create table t (id int, x int, primary key (id), key (x)) partition by hash(id) partitions 2;
show indexes from t;
insert into t values (1,1),(3,2),(5,3),(7,4);
insert into t values (2,1),(4,1),(6,1);
show indexes from t;
analyze table t;
show indexes from t;
drop table t;
......@@ -243,23 +243,20 @@ void TOKUDB_SHARE::destroy(void) {
tokudb_pthread_mutex_destroy(&mutex);
rwlock_destroy(&num_DBs_lock);
tokudb_pthread_cond_destroy(&m_openclose_cond);
tokudb_my_free(rec_per_key);
rec_per_key = NULL;
}
// MUST have tokudb_mutex locked on input
static TOKUDB_SHARE *get_share(const char *table_name, TABLE_SHARE* table_share) {
TOKUDB_SHARE *share = NULL;
int error = 0;
uint length;
length = (uint) strlen(table_name);
uint length = (uint) strlen(table_name);
if (!(share = (TOKUDB_SHARE *) my_hash_search(&tokudb_open_tables, (uchar *) table_name, length))) {
char *tmp_name;
//
// create share and fill it with all zeroes
// hence, all pointers are initialized to NULL
//
share = (TOKUDB_SHARE *) tokudb_my_multi_malloc(MYF(MY_WME | MY_ZEROFILL),
&share, sizeof(*share),
&tmp_name, length + 1,
......@@ -1596,11 +1593,7 @@ bool ha_tokudb::can_replace_into_be_fast(TABLE_SHARE* table_share, KEY_AND_COL_I
return ret_val;
}
int ha_tokudb::initialize_share(
const char* name,
int mode
)
{
int ha_tokudb::initialize_share(const char* name, int mode) {
int error = 0;
uint64_t num_rows = 0;
DB_TXN* txn = NULL;
......@@ -1737,17 +1730,12 @@ int ha_tokudb::initialize_share(
init_hidden_prim_key_info(txn);
// initialize cardinality info from the status dictionary
{
uint total_key_parts = tokudb::compute_total_key_parts(table_share);
uint64_t rec_per_key[total_key_parts];
error = tokudb::get_card_from_status(share->status_block, txn, total_key_parts, rec_per_key);
if (error == 0) {
tokudb::set_card_in_key_info(table, total_key_parts, rec_per_key);
} else {
for (uint i = 0; i < total_key_parts; i++)
rec_per_key[i] = 0;
tokudb::set_card_in_key_info(table, total_key_parts, rec_per_key);
}
share->n_rec_per_key = tokudb::compute_total_key_parts(table_share);
share->rec_per_key = (uint64_t *) tokudb_my_realloc(share->rec_per_key, share->n_rec_per_key * sizeof (uint64_t), MYF(MY_FAE));
error = tokudb::get_card_from_status(share->status_block, txn, share->n_rec_per_key, share->rec_per_key);
if (error) {
for (uint i = 0; i < share->n_rec_per_key; i++)
share->rec_per_key[i] = 0;
}
error = 0;
......@@ -5943,6 +5931,7 @@ int ha_tokudb::info(uint flag) {
}
if ((flag & HA_STATUS_CONST)) {
stats.max_data_file_length= 9223372036854775807ULL;
tokudb::set_card_in_key_info(table, share->n_rec_per_key, share->rec_per_key);
}
/* Don't return key if we got an error for the internal primary key */
......
......@@ -184,6 +184,9 @@ class TOKUDB_SHARE {
enum { CLOSED, OPENING, OPENED, CLOSING, ERROR } m_state;
int m_error;
int m_initialize_count;
uint n_rec_per_key;
uint64_t *rec_per_key;
};
typedef struct st_filter_key_part_info {
......
......@@ -441,6 +441,8 @@ static inline void *tokudb_my_malloc(size_t s, myf flags) {
}
static inline void *tokudb_my_realloc(void *p, size_t s, myf flags) {
if (s == 0)
return p;
#if 50700 <= MYSQL_VERSION_ID && MYSQL_VERSION_ID <= 50799
return my_realloc(0, p, s, flags);
#else
......@@ -449,7 +451,8 @@ static inline void *tokudb_my_realloc(void *p, size_t s, myf flags) {
}
static inline void tokudb_my_free(void *ptr) {
my_free(ptr);
if (ptr)
my_free(ptr);
}
static inline char *tokudb_my_strdup(const char *p, myf flags) {
......
......@@ -217,7 +217,7 @@ namespace tokudb {
// For each key part, put records per key part in *rec_per_key_part[key_part_index].
// Returns 0 if success, otherwise an error number.
// TODO statistical dives into the FT
int analyze_card(DB *db, DB_TXN *txn, bool is_unique __attribute__((unused)), uint64_t num_key_parts, uint64_t *rec_per_key_part,
int analyze_card(DB *db, DB_TXN *txn, bool is_unique, uint64_t num_key_parts, uint64_t *rec_per_key_part,
int (*key_compare)(DB *, const DBT *, const DBT *, uint),
int (*analyze_progress)(void *extra, uint64_t rows), void *progress_extra) {
int error = 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment