Commit 94e9dc95 authored by Marko Mäkelä's avatar Marko Mäkelä

MDEV-23600 Division by 0 in row_search_with_covering_prefix

The InnoDB index fields store bytes, not characters.
Remove some unnecessary conversions from characters to bytes.

This also fixes MDEV-20422 and the wrong-result bug MDEV-12486.
parent feac078f
[covering]
innodb_prefix_index_cluster_optimization=on
[unoptimized]
innodb_prefix_index_cluster_optimization=off
drop table if exists prefixinno;
SET @save_opt= @@GLOBAL.innodb_prefix_index_cluster_optimization;
set global innodb_prefix_index_cluster_optimization = ON;
show variables like 'innodb_prefix_index_cluster_optimization';
Variable_name Value
......@@ -346,10 +346,10 @@ f1
🐱🌑
select @cluster_lookups;
@cluster_lookups
2
1
select @cluster_lookups_avoided;
@cluster_lookups_avoided
0
1
# Eligible - record length is shorter than prefix length
SELECT f1 FROM t1 FORCE INDEX (`f1`) WHERE f1 like '🌑%';
f1
......@@ -366,10 +366,10 @@ f1
🌒
select @cluster_lookups;
@cluster_lookups
1
0
select @cluster_lookups_avoided;
@cluster_lookups_avoided
1
2
DROP TABLE t1;
CREATE TABLE t1(
col1 INT,
......@@ -398,4 +398,49 @@ select @cluster_lookups_avoided;
@cluster_lookups_avoided
0
DROP TABLE t1;
set global innodb_prefix_index_cluster_optimization = OFF;
#
# MDEV-23600 Division by 0 in row_search_with_covering_prefix()
#
CREATE TABLE t(c POINT UNIQUE) ENGINE=InnoDB;
INSERT t SET c=POINT(1,1);
SELECT * FROM t WHERE c > (SELECT MAX(c) FROM t);
c
DROP TABLE t;
#
# MDEV-12486 Wrong results with innodb_prefix_index_cluster_optimization
#
CREATE TABLE wp_blogs (
blog_id bigint(20) NOT NULL auto_increment,
site_id bigint(20) NOT NULL default '0',
domain varchar(200) NOT NULL default '',
path varchar(100) NOT NULL default '',
registered datetime NOT NULL default '0000-00-00 00:00:00',
last_updated datetime NOT NULL default '0000-00-00 00:00:00',
public tinyint(2) NOT NULL default '1',
archived tinyint(2) NOT NULL default '0',
mature tinyint(2) NOT NULL default '0',
spam tinyint(2) NOT NULL default '0',
deleted tinyint(2) NOT NULL default '0',
lang_id int(11) NOT NULL default '0',
PRIMARY KEY (blog_id),
KEY domain (domain(50),path(5)),
KEY lang_id (lang_id)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
INSERT INTO wp_blogs (domain, path) VALUES
('domain.no', '/fondsinvesteringer/'), ('domain.no', '/'),
('foo', 'bar'), ('bar', 'foo'), ('foo', 'foo'), ('bar', 'bar'),
('foo', 'foobar'), ('bar', 'foobar'), ('foobar', 'foobar');
SET GLOBAL innodb_prefix_index_cluster_optimization=off;
SELECT blog_id FROM wp_blogs WHERE domain IN ('domain.no')
AND path IN ( '/fondsinvesteringer/', '/' );
blog_id
2
1
SET GLOBAL innodb_prefix_index_cluster_optimization=on;
SELECT blog_id FROM wp_blogs WHERE domain IN ('domain.no')
AND path IN ( '/fondsinvesteringer/', '/' );
blog_id
2
1
DROP TABLE wp_blogs;
SET GLOBAL innodb_prefix_index_cluster_optimization = @save_opt;
This diff is collapsed.
This diff is collapsed.
-- source include/have_innodb.inc
--disable_warnings
drop table if exists prefixinno;
--enable_warnings
SET @save_opt= @@GLOBAL.innodb_prefix_index_cluster_optimization;
set global innodb_prefix_index_cluster_optimization = ON;
show variables like 'innodb_prefix_index_cluster_optimization';
......@@ -665,4 +662,49 @@ select @cluster_lookups;
select @cluster_lookups_avoided;
DROP TABLE t1;
set global innodb_prefix_index_cluster_optimization = OFF;
--echo #
--echo # MDEV-23600 Division by 0 in row_search_with_covering_prefix()
--echo #
CREATE TABLE t(c POINT UNIQUE) ENGINE=InnoDB;
INSERT t SET c=POINT(1,1);
SELECT * FROM t WHERE c > (SELECT MAX(c) FROM t);
DROP TABLE t;
--echo #
--echo # MDEV-12486 Wrong results with innodb_prefix_index_cluster_optimization
--echo #
CREATE TABLE wp_blogs (
blog_id bigint(20) NOT NULL auto_increment,
site_id bigint(20) NOT NULL default '0',
domain varchar(200) NOT NULL default '',
path varchar(100) NOT NULL default '',
registered datetime NOT NULL default '0000-00-00 00:00:00',
last_updated datetime NOT NULL default '0000-00-00 00:00:00',
public tinyint(2) NOT NULL default '1',
archived tinyint(2) NOT NULL default '0',
mature tinyint(2) NOT NULL default '0',
spam tinyint(2) NOT NULL default '0',
deleted tinyint(2) NOT NULL default '0',
lang_id int(11) NOT NULL default '0',
PRIMARY KEY (blog_id),
KEY domain (domain(50),path(5)),
KEY lang_id (lang_id)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
INSERT INTO wp_blogs (domain, path) VALUES
('domain.no', '/fondsinvesteringer/'), ('domain.no', '/'),
('foo', 'bar'), ('bar', 'foo'), ('foo', 'foo'), ('bar', 'bar'),
('foo', 'foobar'), ('bar', 'foobar'), ('foobar', 'foobar');
SET GLOBAL innodb_prefix_index_cluster_optimization=off;
SELECT blog_id FROM wp_blogs WHERE domain IN ('domain.no')
AND path IN ( '/fondsinvesteringer/', '/' );
SET GLOBAL innodb_prefix_index_cluster_optimization=on;
SELECT blog_id FROM wp_blogs WHERE domain IN ('domain.no')
AND path IN ( '/fondsinvesteringer/', '/' );
DROP TABLE wp_blogs;
SET GLOBAL innodb_prefix_index_cluster_optimization = @save_opt;
[on]
optimizer_switch=extended_keys=on
[off]
optimizer_switch=extended_keys=off
This diff is collapsed.
......@@ -2,7 +2,7 @@
Copyright (c) 1997, 2017, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2008, Google Inc.
Copyright (c) 2015, 2018, MariaDB Corporation.
Copyright (c) 2015, 2020, MariaDB Corporation.
Portions of this file contain modifications contributed and copyrighted by
Google, Inc. Those modifications are gratefully acknowledged and are described
......@@ -3723,9 +3723,16 @@ bool row_search_with_covering_prefix(
return false;
}
/* We can avoid a clustered index lookup if
all of the following hold:
(1) all columns are in the secondary index
(2) all values for columns that are prefix-only
indexes are shorter than the prefix size
This optimization can avoid many IOs for certain schemas. */
for (ulint i = 0; i < prebuilt->n_template; i++) {
mysql_row_templ_t* templ = prebuilt->mysql_template + i;
ulint j = templ->rec_prefix_field_no;
ut_ad(!templ->mbminlen == !templ->mbmaxlen);
/** Condition (1) : is the field in the index. */
if (j == ULINT_UNDEFINED) {
......@@ -3735,33 +3742,29 @@ bool row_search_with_covering_prefix(
/** Condition (2): If this is a prefix index then
row's value size shorter than prefix length. */
if (!templ->rec_field_is_prefix) {
if (!templ->rec_field_is_prefix
|| rec_offs_nth_sql_null(offsets, j)) {
continue;
}
ulint rec_size = rec_offs_nth_size(offsets, j);
const dict_field_t* field = dict_index_get_nth_field(index, j);
ulint max_chars = field->prefix_len / templ->mbmaxlen;
ut_a(field->prefix_len > 0);
if (rec_size < max_chars) {
/* Record in bytes shorter than the index
prefix length in char. */
if (!field->prefix_len) {
continue;
}
if (rec_size * templ->mbminlen >= field->prefix_len) {
const ulint rec_size = rec_offs_nth_size(offsets, j);
if (rec_size >= field->prefix_len) {
/* Shortest representation string by the
byte length of the record is longer than the
maximum possible index prefix. */
return false;
}
size_t num_chars = rec_field_len_in_chars(
field->col, j, rec, offsets);
if (num_chars >= max_chars) {
if (templ->mbminlen != templ->mbmaxlen
&& rec_field_len_in_chars(field->col, j, rec, offsets)
>= field->prefix_len / templ->mbmaxlen) {
/* No of chars to store the record exceeds
the index prefix character length. */
return false;
......
......@@ -2,7 +2,7 @@
Copyright (c) 1997, 2017, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2008, Google Inc.
Copyright (c) 2015, 2018, MariaDB Corporation.
Copyright (c) 2015, 2020, MariaDB Corporation.
Portions of this file contain modifications contributed and copyrighted by
Google, Inc. Those modifications are gratefully acknowledged and are described
......@@ -3740,9 +3740,16 @@ bool row_search_with_covering_prefix(
return false;
}
/* We can avoid a clustered index lookup if
all of the following hold:
(1) all columns are in the secondary index
(2) all values for columns that are prefix-only
indexes are shorter than the prefix size
This optimization can avoid many IOs for certain schemas. */
for (ulint i = 0; i < prebuilt->n_template; i++) {
mysql_row_templ_t* templ = prebuilt->mysql_template + i;
ulint j = templ->rec_prefix_field_no;
ut_ad(!templ->mbminlen == !templ->mbmaxlen);
/** Condition (1) : is the field in the index. */
if (j == ULINT_UNDEFINED) {
......@@ -3752,34 +3759,29 @@ bool row_search_with_covering_prefix(
/** Condition (2): If this is a prefix index then
row's value size shorter than prefix length. */
if (!templ->rec_field_is_prefix) {
if (!templ->rec_field_is_prefix
|| rec_offs_nth_sql_null(offsets, j)) {
continue;
}
ulint rec_size = rec_offs_nth_size(offsets, j);
const dict_field_t* field = dict_index_get_nth_field(index, j);
ulint max_chars = field->prefix_len / templ->mbmaxlen;
ut_a(field->prefix_len > 0);
if (rec_size < max_chars) {
/* Record in bytes shorter than the index
prefix length in char. */
if (!field->prefix_len) {
continue;
}
if (rec_size * templ->mbminlen >= field->prefix_len) {
const ulint rec_size = rec_offs_nth_size(offsets, j);
if (rec_size >= field->prefix_len) {
/* Shortest representation string by the
byte length of the record is longer than the
maximum possible index prefix. */
return false;
}
size_t num_chars = rec_field_len_in_chars(
field->col, j, rec, offsets);
if (num_chars >= max_chars) {
if (templ->mbminlen != templ->mbmaxlen
&& rec_field_len_in_chars(field->col, j, rec, offsets)
>= field->prefix_len / templ->mbmaxlen) {
/* No of chars to store the record exceeds
the index prefix character length. */
return false;
......
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