apache / doris

Apache Doris is an easy-to-use, high performance and unified analytics database.
https://doris.apache.org
Apache License 2.0
12.34k stars 3.21k forks source link

[Enhancement] statistics error lead to OOM when multi-table join scenario #34946

Open yx-keith opened 4 months ago

yx-keith commented 4 months ago

Search before asking

Description

In multi-table join scenario, result of a join will be used as the input of the subsequent joins. During the interval between updating statistics, when we update data, statistics is not collected in time, If we run a multi-table join query within this time interval, the optimizer may choose a poor strategy because there is no correct statistics.

for example: select t1.* from example_tbl t1 join example_tbl02 t2 on t1.city=t2.city and t1.city="chengdu" join example_tbl03 t3 on t1.city=t3.city join example_tbl04 t4 on t1.city=t4.city join example_tbl05 t5 on t1.city=t5.city;

### Doris-2.0.4:

this is plan(2.0.4): image

in this case, example_tbl02, example_tbl04, example_tbl05 each has 30 million rows data of city="chengdu"; example_tbl, example_tbl03 don't have any data of city="chengdu". After doris triggered first statistics collection, we insert 2 rows data of city="chengdu" into example_tbl and example_tbl03 respectively, and run the query before the second time statistics collection, thus we obtained uncorrect statistics and choosed a poor plan in this query.

we can simplify this plan: image

actual execution situation: braodcast-01: distribute 2 rows of data broadcast-02: distribute 2 rows of data broadcast-03: distribute 120 million (2 2 30million) rows of data broadcast-04: ditribute 3600 trillion(2 2 30 million * 30 million) rows of data

as you can see, broadcast-03 and broadcast-04 distribute huge data because of uncorrct statistics, which easy to cause OOM.

### Doris-master:

same query'plan(with same statistics data) in master branch code: example_tbl02, example_tbl04, example_tbl05 each has 30 million rows data of city="chengdu"; example_tbl, example_tbl03 don't have any data of city="chengdu"

image

we can simplify this plan: image

After doris triggered first statistics collection, we insert 2 rows data of city="chengdu" into example_tbl and example_tbl03 respectively, and run the query before the second time statistics collection, thus we obtained uncorrect statistics and choosed a poor plan in this query.

this is actual execution situation: braodcast-01: distribute 2 rows of data broadcast-02: distribute 2 rows of data broadcast-03: distribute 120 million (2 2 30million) rows of data broadcast-04: ditribute 3600 trillion(2 2 30 million * 30 million) rows of data

as you can see, broadcast-03 and broadcast-04 distribute huge data because of uncorrct statistics, which easy to cause OOM.

Solution

I solved this problem, this is plan: image

we can simplify this plan: image

as you can see, the type of data's distribution has changed from broadcast to shuffle hash, which avoid OOM

Are you willing to submit PR?

Code of Conduct

englefly commented 4 months ago

https://github.com/apache/doris/pull/33685

englefly commented 4 months ago

there is a mis-understanding of physical plan. join |---->A +--->B here A is the left child, and B is the right child. The join children should be switched in the figures.