DEV Community

Michael
Michael

Posted on

Rewriting OR Conditions in GBase 8a: Three Manual Optimisations

When a business query contains OR and performance drops sharply compared with the same query without it, a manual rewrite is worth trying. This post covers two cases in the China-domestically developed GBase 8a MPP database from GBASE: OR in filter conditions and OR in JOIN conditions.

All of these rewrites are hand-shaped execution plans for cases where the default optimiser does a poor job. Always verify both the speed-up and the correctness of the final result before adopting one in a gbase cluster.

Same column, different values (and IN lists)

The core move is to collapse several OR comparisons into one comparison. For numeric values, use decode by mapping every accepted value onto the same target:

decode(A,XX,XX,YY,XX,'')=XX
Enter fullscreen mode Exit fullscreen mode

Here YY is rewritten to XX, so both branches become an equality test against XX. For character values, use case instead:

(case A when XX then XX when YY then XX end)=XX
Enter fullscreen mode Exit fullscreen mode
gbase> select * from t3 where id=1 or id=11;
+------+------+
| id   | id2  |
+------+------+
|    1 |    1 |
|   11 |   11 |
+------+------+
2 rows in set (Elapsed: 00:00:00.07)

gbase> select * from t3 where decode(id,1,1,11,1)=1;
+------+------+
| id   | id2  |
+------+------+
|    1 |    1 |
|   11 |   11 |
+------+------+
2 rows in set (Elapsed: 00:00:00.08)
Enter fullscreen mode Exit fullscreen mode

Different columns, different values

For A=XX OR B=YY or C=ZZ, concatenate one case expression per branch and match with like:

concat(case when a=XX then XX else '' end,case when B=YY then XX else '' end,case when C=ZZ then XX else '' end) like 'XX%'
Enter fullscreen mode Exit fullscreen mode

XX and YY may be the same value or different ones; the point is still turning multiple OR checks into a single check.

gbase> select * from t3 where id=111 or id2=9999;
+------+------+
| id   | id2  |
+------+------+
|  111 | 1111 |
+------+------+
1 row in set (Elapsed: 00:00:00.04)

gbase> select * from t3 where concat(case when id=111 then 111 else '' end,case when id2=9999 then 111 else '' end) like '111%';
+------+------+
| id   | id2  |
+------+------+
|  111 | 1111 |
+------+------+
1 row in set (Elapsed: 00:00:00.09)
Enter fullscreen mode Exit fullscreen mode

OR in a JOIN condition — only when the result set cannot grow

Original form:

select b.XX from a
left join b on (a.id=b.id or a.name=b.name) and ...other join conditions
Enter fullscreen mode Exit fullscreen mode

Rewrite as two joins and pick the column with coalesce:

select coalesce(b.XX,b2.XX) from a
left join b on a.id=b.id and ...other join conditions
left join b b2 on a.id<>b2.id and a.name=b2.name and ...other join conditions
Enter fullscreen mode Exit fullscreen mode

The result set must not expand: if table a has 10,000 rows, the output must still be 10,000 rows. That means a strict 1:1 relationship — the right table cannot contain duplicates that multiply the left side. With 1:n, the rewritten form would silently return fewer rows. Columns taken from b in the select list then need coalesce(b.XX,b2.XX) instead of b.XX.

gbase> select * from t1;
+------+------+
| id   | id2  |
+------+------+
|    1 |   66 |
|    2 |   77 |
+------+------+
2 rows in set (Elapsed: 00:00:00.06)

gbase> select * from t3;
+------+------+
| id   | id2  |
+------+------+
|    1 |   66 |
|    3 |   77 |
|    9 |   99 |
+------+------+
3 rows in set (Elapsed: 00:00:00.09)

gbase> select * from t1 left join t3 on t1.id=t3.id or t1.id2=t3.id2;
+------+------+------+------+
| id   | id2  | id   | id2  |
+------+------+------+------+
|    1 |   66 |    1 |   66 |
|    2 |   77 |    3 |   77 |
+------+------+------+------+
2 rows in set (Elapsed: 00:00:00.28)

gbase> select * from t1 left join t3 on t1.id=t3.id left join t3 t3_2 on t1.id<>t3_2.id and t1.id2=t3_2.id2;
+------+------+------+------+------+------+
| id   | id2  | id   | id2  | id   | id2  |
+------+------+------+------+------+------+
|    2 |   77 | NULL | NULL |    3 |   77 |
|    1 |   66 |    1 |   66 | NULL | NULL |
+------+------+------+------+------+------+
2 rows in set (Elapsed: 00:00:00.28)

gbase> select t1.id,t1.id2,coalesce(t3.id,t3_2.id)id,coalesce(t3.id2,t3_2.id2)id2  from t1 left join t3 on t1.id=t3.id left join t3 t3_2 on t1.id<>t3_2.id and t1.id2=t3_2.id2;
+------+------+------+------+
| id   | id2  | id   | id2  |
+------+------+------+------+
|    2 |   77 |    3 |   77 |
|    1 |   66 |    1 |   66 |
+------+------+------+------+
2 rows in set (Elapsed: 00:00:00.16)
Enter fullscreen mode Exit fullscreen mode

The final coalesce version lands at 0.16 seconds versus 0.28 seconds for the original.

Looking ahead, these are all ways of nudging the execution plan by hand, especially for filter conditions. The JOIN rewrite is the risky one: it is only valid for 1:1, never 1:n, or the result set can be wrong. In a gbase analytical database, treat the rewrite as a hypothesis and keep the original query as the correctness baseline.

Top comments (0)