Hello Perf Gurus, Need help with below performanc...
# troubleshooting
r
Hello Perf Gurus, Need help with below performance issue - I have simple JOIN query
(fact table has 30M rows and dimension has 20M rows)
tables, I have very good filter on Fact table that should which should return only 30 row. But I am running into below exception regarding rows limit which I was not expecting since
INTERMEDIATE result of FACT Table should only have few rows
.
Copy code
Query:
select b.user_id
from fact_table a
  inner join dimension_table b on a.user_id = b.user_id
where a.phone = '919741xxxxxx'
Copy code
Execution Plan:
LogicalProject(user_id=[$1])
  LogicalJoin(condition=[=($0, $1)], joinType=[inner])
    PinotLogicalExchange(distribution=[hash[0]])
      LogicalProject(user_id=[$51])
        LogicalFilter(condition=[=($36, _UTF-8'919741511500')])
          LogicalTableScan(table=[[default, fact_table]])
    PinotLogicalExchange(distribution=[hash[0]])
      LogicalProject(user_id=[$9])
        LogicalTableScan(table=[[default, dimension_table]])
Error Code: 200
QueryExecutionError:
Received error query execution result block: {245=Cannot build in memory hash table for join operator, reached number of rows limit: 1048576. Consider increasing the limit for the maximum number of rows in a join either via the query option 'maxRowsInJoin' or the 'max_rows_in_join' hint in the 'joinOptions'. Alternatively, if partial results are acceptable, the join overflow mode can be set to 'BREAK' either via the query option 'joinOverflowMode' or the 'join_overflow_mode' hint in the 'joinOptions'.
k
Filter is applied post joining
r
That would be totally inefficient as per below screen on how filter happens before JOIN
Screenshot 2025-01-22 at 18.57.34.png
I am using Pinot 1.4.0 &
Use Multi-Stage Engine is checked
g
The explain plan doesn’t say that the limit is applied post joining
It looks to me that the query filter is less selective than you think. Can you run that part of the query? Also, I would recommend using segment explain plan if you are using one of the last versions
Oh, I see. The filter is on the left side, not the right side
the right side is the one that is hashed. Can you swap the order of the tables?
r
thanks @Gonzalo Ortiz, it is way better. Takes around 11193 ms, but still executes once I switch the order of join. I will try to see how to optimize this further - Execution Plan
LogicalProject(user_id=[$1])
LogicalJoin(condition=[=($0, $1)], joinType=[inner])
PinotLogicalExchange(distribution=[hash[0]])
LogicalProject(user_id=[$9])
LogicalTableScan(table=[[default, dimension_table]])
PinotLogicalExchange(distribution=[hash[0]])
LogicalProject(user_id=[$51])
LogicalFilter(condition=[=($36, _UTF-8'9741xxxx')])
LogicalTableScan(table=[[default, fact_table]])
g
We have an optimization for cases similar to this one. In your case we cannot apply it automatically because it slightly changes the semantics, but probably this corner case won’t affect your query. In your case, is it the same to write:
Copy code
select b.user_id
from dimension_table b
  inner join fact_table a on a.user_id = b.user_id
where a.phone = '919741xxxxxx'
Than
Copy code
select b.user_id
from dimension_table b
  inner join (select distinct user_id from fact_table where a.phone = '919741xxxxxx') a on a.user_id = b.user_id
? In that case, you (or Pinot automatically) can rewrite that as:
Copy code
select b.user_id
from dimension_table b
where b.user_id in (select user_id from fact_table where phone = '919741xxxxxx');
which is quite faster if only a few hundreds user_ids are expected.
or, said in a different way, are you expecting to have multiple rows in fact_table that are going to have a given id and the same user_id? imagine that fact_table has the values (user_id = 1, phone = ‘919741xxxxxx’), (user_id = 2, phone = ‘919741xxxxxx’), (user_id = 1, phone = ‘919741xxxxxx’). In this case there are two rows whose user_id is 1. The difference between your query (inner join) and the other query (IN select or semi-join) is that the inner join will duplicate elements of dimension_table whose user_id is 1 (because there are 2 rows in fact_table that contain that id). The semi-join will not apply that duplication.
It may be easier with an example. Here you have one (using postgres): https://onecompiler.com/postgresql/436weyfqr
In Pinot the semi-join is usually better than inner join (because less data needs to be shuffled and also because we opens the possibility of using indexes to filter dimension_table)