左连接 left join 超过2张表无法透明改写
Doris版本:2.1.5
架构:3FE+3BE
测试数据
CREATE TABLE IF NOT EXISTS users (
user_id INT NOT NULL,
username VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL
)
DUPLICATE KEY(user_id)
DISTRIBUTED BY HASH(user_id) BUCKETS 3
PROPERTIES ("replication_num" = "1");
INSERT INTO users VALUES
(1, 'Alice', 'alice@example.com'),
(2, 'Bob', 'bob@example.com');
CREATE TABLE IF NOT EXISTS orders (
order_id INT NOT NULL,
user_id INT NOT NULL,
order_date DATE NOT NULL
)
DUPLICATE KEY(order_id)
DISTRIBUTED BY HASH(order_id) BUCKETS 3
PROPERTIES ("replication_num" = "1");
INSERT INTO orders VALUES
(101, 1, '2024-01-01'),
(102, 2, '2024-01-02');
CREATE TABLE IF NOT EXISTS order_items (
order_item_id INT NOT NULL,
order_id INT NOT NULL,
product_id INT NOT NULL,
quantity INT NOT NULL
)
DUPLICATE KEY(order_item_id)
DISTRIBUTED BY HASH(order_item_id) BUCKETS 3
PROPERTIES ("replication_num" = "1");
INSERT INTO order_items VALUES
(1, 101, 1001, 1),
(2, 101, 1002, 2),
(3, 102, 1002, 1);
可以命中的SQL
drop MATERIALIZED VIEW if exists `mv3`;
CREATE MATERIALIZED VIEW `mv3`
BUILD IMMEDIATE REFRESH AUTO ON SCHEDULE EVERY 30 MINUTE
DISTRIBUTED BY RANDOM BUCKETS 2
PROPERTIES (
"replication_num" = "2"
)
AS
SELECT
t1.username,
t2.order_id,
t2.order_date
FROM
users t1
JOIN
orders t2 ON t1.user_id = t2.user_id
JOIN
order_items t3 ON t2.order_id = t3.order_id;
explain
SELECT
t1.username
FROM
users t1
JOIN
orders t2 ON t1.user_id = t2.user_id
JOIN
order_items t3 ON t2.order_id = t3.order_id;
无法命中的SQL
drop MATERIALIZED VIEW if exists `mv3`;
CREATE MATERIALIZED VIEW `mv3`
BUILD IMMEDIATE REFRESH AUTO ON SCHEDULE EVERY 30 MINUTE
DISTRIBUTED BY RANDOM BUCKETS 2
PROPERTIES (
"replication_num" = "2"
)
AS
SELECT
t1.username,
t2.order_id,
t2.order_date
FROM
users t1
LEFT JOIN
orders t2 ON t1.user_id = t2.user_id
LEFT JOIN
order_items t3 ON t2.order_id = t3.order_id;