MariaDB - 当连接中的列之间存在数据类型不匹配时,即使在强制转换后也不使用索引

发布于 2025-01-16 15:12:42 字数 2459 浏览 1 评论 0原文

我正在尝试连接两个表,其中连接列之间的数据类型不匹配。即使在显式转换后(或在强制索引后),索引也不会被拾取。可能是什么原因?

create table table_a (
    table_a_col1 decimal(18) 
); 

create table table_b (
    rec_id decimal(18) ,
    table_b_col2 varchar(18)
);

create index table_b_col2_ndx on table_b (table_b_col2);

insert into table_a values (1);
insert into table_a values (2);

insert into table_b values (1,'1');
insert into table_b values (2,'2');
insert into table_b values (4,'S');

commit;

不进行任何转换的

explain
select  *
from table_a
left join table_b 
on  (table_b_col2 = table_a_col1)
where table_a_col1 = 2;

|id |select_type|table  |type|possible_keys   |key|key_len|ref|rows|Extra                                         |
|---|-----------|-------|----|----------------|---|-------|---|----|----------------------------------------------|
|1  |SIMPLE     |table_a|ALL |                |   |       |   |2   |Using where                                   |
|1  |SIMPLE     |table_b|ALL |table_b_col2_ndx|   |       |   |3   |Range checked for each record (index map: 0x1)|

计划 显式数据类型转换后的

explain
select  *
from table_a
left join table_b 
on  (table_b_col2 = cast(table_a_col1 as character))
where table_a_col1 = 2;

|id |select_type|table  |type|possible_keys|key|key_len|ref|rows|Extra                                          |
|---|-----------|-------|----|-------------|---|-------|---|----|-----------------------------------------------|
|1  |SIMPLE     |table_a|ALL |             |   |       |   |2   |Using where                                    |
|1  |SIMPLE     |table_b|ALL |             |   |       |   |3   |Using where; Using join buffer (flat, BNL join)|

计划强制索引后的计划

explain
select  *
from table_a
left join table_b force index (table_b_col2_ndx)
on  (table_b_col2 = cast(table_a_col1 as character))
where table_a_col1 = 2;

|id |select_type|table  |type|possible_keys|key|key_len|ref|rows|Extra                                          |
|---|-----------|-------|----|-------------|---|-------|---|----|-----------------------------------------------|
|1  |SIMPLE     |table_a|ALL |             |   |       |   |2   |Using where                                    |
|1  |SIMPLE     |table_b|ALL |             |   |       |   |3   |Using where; Using join buffer (flat, BNL join)|


select version() ;

|version()         |
|------------------|
|10.2.9-MariaDB-log|

I am trying to join two tables where there is a datatype mismatch between the join columns. Index is not getting picked up even after an explicit conversion (or after forcing the index). What could be the reason?

create table table_a (
    table_a_col1 decimal(18) 
); 

create table table_b (
    rec_id decimal(18) ,
    table_b_col2 varchar(18)
);

create index table_b_col2_ndx on table_b (table_b_col2);

insert into table_a values (1);
insert into table_a values (2);

insert into table_b values (1,'1');
insert into table_b values (2,'2');
insert into table_b values (4,'S');

commit;

Plan without doing any conversion

explain
select  *
from table_a
left join table_b 
on  (table_b_col2 = table_a_col1)
where table_a_col1 = 2;

|id |select_type|table  |type|possible_keys   |key|key_len|ref|rows|Extra                                         |
|---|-----------|-------|----|----------------|---|-------|---|----|----------------------------------------------|
|1  |SIMPLE     |table_a|ALL |                |   |       |   |2   |Using where                                   |
|1  |SIMPLE     |table_b|ALL |table_b_col2_ndx|   |       |   |3   |Range checked for each record (index map: 0x1)|

Plan after explicit datatype conversion

explain
select  *
from table_a
left join table_b 
on  (table_b_col2 = cast(table_a_col1 as character))
where table_a_col1 = 2;

|id |select_type|table  |type|possible_keys|key|key_len|ref|rows|Extra                                          |
|---|-----------|-------|----|-------------|---|-------|---|----|-----------------------------------------------|
|1  |SIMPLE     |table_a|ALL |             |   |       |   |2   |Using where                                    |
|1  |SIMPLE     |table_b|ALL |             |   |       |   |3   |Using where; Using join buffer (flat, BNL join)|

Plan after forcing the index

explain
select  *
from table_a
left join table_b force index (table_b_col2_ndx)
on  (table_b_col2 = cast(table_a_col1 as character))
where table_a_col1 = 2;

|id |select_type|table  |type|possible_keys|key|key_len|ref|rows|Extra                                          |
|---|-----------|-------|----|-------------|---|-------|---|----|-----------------------------------------------|
|1  |SIMPLE     |table_a|ALL |             |   |       |   |2   |Using where                                    |
|1  |SIMPLE     |table_b|ALL |             |   |       |   |3   |Using where; Using join buffer (flat, BNL join)|


select version() ;

|version()         |
|------------------|
|10.2.9-MariaDB-log|

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文