Which Scenarios Does ClickHouse Applies to?
Tags: bigdata, performance, database, sql
Published: 2023-10-28 23:33:16

ClickHouse has been popular in recent years as an analytic database. It is noted for the high speed and is often used as the benchmark by vendors of many other analytic databases whose selling points are high performance. Many users will consider turning to ClickHouse when they encounter database performance problems.
ClickHouse has an outstanding feature. Its column-oriented wide table runs fast, thanks to the excellent compression probably. However, it does not have any other merits. Users who hope to use it to solve database performance problems will likely feel disappointed.
 
We compare the TPC-H 100G performance of ClickHouse with that of Oracle in the same hardware environment. Below only lists the test results (Time unit: second). Find complete test report [HERE](http://c.raqsoft.com/article/1698110342313).

<table> 
 <thead> 
  <tr>
   <th> TPC-H No. </th>
   <th> ClickHouse </th>
   <th> Oracle </th>
  </tr> 
 </thead> 
 <tbody> 
  <tr>
   <td> 1 </td>
   <td> 15.4 </td>
   <td> 114.3 </td>
  </tr> 
  <tr>
   <td> 2 </td>
   <td> 17.3 </td>
   <td> 1.9 </td>
  </tr> 
  <tr>
   <td> 3 </td>
   <td> Memory overflow </td>
   <td> 165.8 </td>
  </tr> 
  <tr>
   <td> 4 </td>
   <td> Memory overflow </td>
   <td> 158.4 </td>
  </tr> 
  <tr>
   <td> 5 </td>
   <td> Memory overflow </td>
   <td> 174.5 </td>
  </tr> 
  <tr>
   <td> 6 </td>
   <td> 4.8 </td>
   <td> 126.7 </td>
  </tr> 
  <tr>
   <td> 7 </td>
   <td> Memory overflow </td>
   <td> 181.5 </td>
  </tr> 
  <tr>
   <td> 8 </td>
   <td> Memory overflow </td>
   <td> 209.7 </td>
  </tr> 
  <tr>
   <td> 9 </td>
   <td> Memory overflow </td>
   <td> 256.0 </td>
  </tr> 
  <tr>
   <td> 10 </td>
   <td> 58.3 </td>
   <td> 195.6 </td>
  </tr> 
  <tr>
   <td> 11 </td>
   <td> 6.7 </td>
   <td> 8.7 </td>
  </tr> 
  <tr>
   <td> 12 </td>
   <td> 10.7 </td>
   <td> 186.0 </td>
  </tr> 
  <tr>
   <td> 13 </td>
   <td> 134.1 </td>
   <td> 33.3 </td>
  </tr> 
  <tr>
   <td> 14 </td>
   <td> 10.2 </td>
   <td> 170.0 </td>
  </tr> 
  <tr>
   <td> 15 </td>
   <td> 11.2 </td>
   <td> 161.8 </td>
  </tr> 
  <tr>
   <td> 16 </td>
   <td> 4.0 </td>
   <td> 10.8 </td>
  </tr> 
  <tr>
   <td> 17 </td>
   <td> 44.6 </td>
   <td> 156.5 </td>
  </tr> 
  <tr>
   <td> 18 </td>
   <td> Memory overflow </td>
   <td> 416.8 </td>
  </tr> 
  <tr>
   <td> 19 </td>
   <td> &gt;600 </td>
   <td> 144.1 </td>
  </tr> 
  <tr>
   <td> 20 </td>
   <td> 31.2 </td>
   <td> 171.0 </td>
  </tr> 
  <tr>
   <td> 21 </td>
   <td> Syntactic error </td>
   <td> 360.7 </td>
  </tr> 
  <tr>
   <td> 22 </td>
   <td> 8.4 </td>
   <td> 37.7 </td>
  </tr> 
  <tr>
   <td> Total </td>
   <td> - </td>
   <td> 3441.8 </td>
  </tr> 
 </tbody> 
</table> 

TPC-H database performance test is relatively basic and – generally speaking, not complicated. But not all tests are simple single table traversal, there are also JOINs and subqueries. In those cases, ClickHouse’s performance is very poor. There are quite a few computing tasks that can’t be finished at all; SQL statements of a few tasks are even considered too complicated to be executed. ClickHouse is not even as good as Oracle in handling them. Oracle is not a specialized analytic database and is much slower, but it can complete all tasks.
The specific reason is probably as mentioned above. ClickHouse only does a good job in data storage compression, which makes simple traversal very fast; but for slightly more complex computations, it has rather limited optimization capabilities and just cannot finish the execution.
So, users who want to use ClickHouse to solve performance problems should first examine the complexity of their computing tasks by comparing them with TPC-H.
 
Now we still use the TPC-H data to generate a table having a lot of columns and perform the multi-dimensional analysis ClickHouse is best at. Results are listed below (Time unit: second). Find complete test report [HERE](http://c.raqsoft.com/article/1690170794600).

<table> 
 <thead> 
  <tr>
   <th> </th>
   <th> Wide table </th>
   <th> Two-table join </th>
   <th> Seven-table join </th>
  </tr> 
 </thead> 
 <tbody> 
  <tr>
   <td> 4C16G </td>
   <td> 74.3 </td>
   <td> 204.1 </td>
   <td> Memory overflow </td>
  </tr> 
  <tr>
   <td> 8C32G </td>
   <td> 33.2 </td>
   <td> 89.3 </td>
   <td> Memory overflow </td>
  </tr> 
 </tbody> 
</table> 

ClickHouse is good at handling scenarios involving wide table traversal and has the highest performance. Its performance, however, drops sharply once a join is involved. More complicated joins will cause memory overflow, which once again verifies the above cause analysis.
**It seems that ClickHouse is fast only when it handles the simple single table traversals that do not involve any associations. But such “fast speed” ClickHouse can achieve applies too narrow application scenarios**. Is it worthwhile to introduce a database specifically to do just such a simple job?
 
Unlike ClickHouse that has an exaggerated fame, esProc SPL mentioned in the above test report is the real performance champion.
esProc SPL is also open-source. It is developed in Java, but it can far outperform the C++ developed ClickHouse in many performance optimization scenarios.
Strictly speaking, esProc isn’t an analytic database. But it offers high-performance storage schemas (column-oriented storage, compression, etc.) and corresponding algorithm libraries, which enables it to completely replace the analytic database’s computing functionality.
Unlike ClickHouse’s other database competitors, esProc does not adopt the SQL syntax, but uses the more concise SPL (Structured Process Language) instead. The design can overcome SQL’s defects and help achieve high-performance algorithms SQL finds it difficult to or cannot implement. Find easy to understand illustrations HERE.  If the database based on the SQL system is still used, the product will still have the SQL limits and cannot make the most use of the hardware resources to obtain the best performance possible, even if it can surpass ClickHouse in certain aspects.
 
Let’s compare the esProc performances in the above test report with those of ClickHouse:

<table> 
 <thead> 
  <tr>
   <th> TPC-H No. </th>
   <th> esProc SPL </th>
   <th> ClickHouse </th>
   <th> Oracle </th>
  </tr> 
 </thead> 
 <tbody> 
  <tr>
   <td> 1 </td>
   <td> 9.7 </td>
   <td> 15.4 </td>
   <td> 114.3 </td>
  </tr> 
  <tr>
   <td> 2 </td>
   <td> 1.3 </td>
   <td> 17.3 </td>
   <td> 1.9 </td>
  </tr> 
  <tr>
   <td> 3 </td>
   <td> 8.8 </td>
   <td> Memory overflow </td>
   <td> 165.8 </td>
  </tr> 
  <tr>
   <td> 4 </td>
   <td> 4.9 </td>
   <td> Memory overflow </td>
   <td> 158.4 </td>
  </tr> 
  <tr>
   <td> 5 </td>
   <td> 8.9 </td>
   <td> Memory overflow </td>
   <td> 174.5 </td>
  </tr> 
  <tr>
   <td> 6 </td>
   <td> 4.5 </td>
   <td> 4.8 </td>
   <td> 126.7 </td>
  </tr> 
  <tr>
   <td> 7 </td>
   <td> 10.5 </td>
   <td> Memory overflow </td>
   <td> 181.5 </td>
  </tr> 
  <tr>
   <td> 8 </td>
   <td> 6.9 </td>
   <td> Memory overflow </td>
   <td> 209.7 </td>
  </tr> 
  <tr>
   <td> 9 </td>
   <td> 16.8 </td>
   <td> Memory overflow </td>
   <td> 256.0 </td>
  </tr> 
  <tr>
   <td> 10 </td>
   <td> 8.3 </td>
   <td> 58.3 </td>
   <td> 195.6 </td>
  </tr> 
  <tr>
   <td> 11 </td>
   <td> 0.9 </td>
   <td> 6.7 </td>
   <td> 8.7 </td>
  </tr> 
  <tr>
   <td> 12 </td>
   <td> 4.9 </td>
   <td> 10.7 </td>
   <td> 186.0 </td>
  </tr> 
  <tr>
   <td> 13 </td>
   <td> 12.1 </td>
   <td> 134.1 </td>
   <td> 33.3 </td>
  </tr> 
  <tr>
   <td> 14 </td>
   <td> 3.3 </td>
   <td> 10.2 </td>
   <td> 170.0 </td>
  </tr> 
  <tr>
   <td> 15 </td>
   <td> 4.7 </td>
   <td> 11.2 </td>
   <td> 161.8 </td>
  </tr> 
  <tr>
   <td> 16 </td>
   <td> 2.7 </td>
   <td> 4.0 </td>
   <td> 10.8 </td>
  </tr> 
  <tr>
   <td> 17 </td>
   <td> 5.3 </td>
   <td> 44.6 </td>
   <td> 156.5 </td>
  </tr> 
  <tr>
   <td> 18 </td>
   <td> 6.4 </td>
   <td> Memory overflow </td>
   <td> 416.8 </td>
  </tr> 
  <tr>
   <td> 19 </td>
   <td> 5.8 </td>
   <td> &gt;600 </td>
   <td> 144.1 </td>
  </tr> 
  <tr>
   <td> 20 </td>
   <td> 5.2 </td>
   <td> 31.2 </td>
   <td> 171.0 </td>
  </tr> 
  <tr>
   <td> 21 </td>
   <td> 11.9 </td>
   <td> Syntactic error </td>
   <td> 360.7 </td>
  </tr> 
  <tr>
   <td> 22 </td>
   <td> 2.5 </td>
   <td> 8.4 </td>
   <td> 37.7 </td>
  </tr> 
  <tr>
   <td> Total </td>
   <td> 146.3 </td>
   <td> - </td>
   <td> 3441.8 </td>
  </tr> 
 </tbody> 
</table> 

esProc SPL noticeably outstrips ClickHouse in performance as it executes all tasks very fast. It has comprehensive, overwhelming advantages over ClickHouse.

<table> 
 <thead> 
  <tr>
   <th> </th>
   <th colspan="2"> 4C16G </th>
   <th colspan="2"> 8C32G </th>
  </tr> 
  <tr>
   <th> </th>
   <th> esProc SPL </th>
   <th> ClickHouse </th>
   <th> esProc SPL </th>
   <th> ClickHouse </th>
  </tr> 
 </thead> 
 <tbody> 
  <tr>
   <td> Wide table </td>
   <td> 114.2 </td>
   <td> 74.3 </td>
   <td> 57.7 </td>
   <td> 33.2 </td>
  </tr> 
  <tr>
   <td> Two-table join </td>
   <td> 21.5 </td>
   <td> 204.1 </td>
   <td> 11.5 </td>
   <td> 89.3 </td>
  </tr> 
  <tr>
   <td> Seven-table join </td>
   <td> 55.6 </td>
   <td> Memory overflow </td>
   <td> 30.6 </td>
   <td> Memory overflow </td>
  </tr> 
 </tbody> 
</table> 

Seven-table join 55.6 Memory overflow 30.6 Memory overflow
When data volume grows, ClickHouse is indeed better and faster than esProc SPL in handling traversal of a single wide table it is good at. Yet, as wide tables are used in most cases to avoid low-speed join operations (by increasing data volume and performing more complicated data preparations), they become unnecessary when esProc SPL’s unique join optimization strategy enables higher speed than ClickHouse wide tables. Wide tables do not have any merits any longer and become useless once they lose the performance advantages.
 
Though ClickHouse is faster in handling simple, non-association summarization on single tables, it isn’t orders of magnitude faster than esProc (after all, CPU and hard disk merely have this speed). In this case, even if task characteristics can be used to perform optimization, we will still find it difficult to work it out based on ClickHouse. Many optimization logics cannot be achieved in SQL and C++ programming is needed in the higher level to complete the goal. This is complicated and difficult. SPL, however, has much more powerful programming ability and can make good use of the task characteristics to write optimization code.
For example, contemporary multi-dimensional analyses almost always involve multi-index summarization. We can write multi-purpose traversal algorithm in SPL to compute multiple aggregate values during one traversal. Even if SPL is slightly slower than ClickHouse in handling the single-index computation, it can significantly surpass it when dealing with the multi-index summarization:

<table> 
 <thead> 
  <tr>
   <th> </th>
   <th colspan="3"> 4C16G </th>
   <th colspan="3"> 8C32G </th>
  </tr> 
  <tr>
   <th> Number of summarization indexes </th>
   <th> 1</th>
   <th> 2 </th>
   <th> 3 </th>
   <th> 1 </th>
   <th> 2 </th>
   <th> 3 </th>
  </tr> 
 </thead> 
 <tbody> 
  <tr>
   <td> ClickHouse wide table </td>
   <td> 77.4 </td>
   <td> 156.0 </td>
   <td> 249.6 </td>
   <td> 34.7 </td>
   <td> 69.0 </td>
   <td> 106.4 </td>
  </tr> 
  <tr>
   <td> esProc SPL wide table </td>
   <td> 114.2 </td>
   <td> 119.5 </td>
   <td> 124.1 </td>
   <td> 57.7 </td>
   <td> 61.6 </td>
   <td> 64.6 </td>
  </tr> 
  <tr>
   <td> esProc SPL join </td>
   <td> </td>
   <td> </td>
   <td> 100.5 </td>
   <td> </td>
   <td> </td>
   <td> 49.5 </td>
  </tr> 
 </tbody> 
</table> 

For more complicated computations, such as funnel analysis, they are too complex to be tested using the ClickHouse. But esProc SPL can test them. Find related information HERE.
In a nutshell, **esProc SPL has complete and comprehensive performance advantages, but ClickHouse’s performance advantages only apply in a very narrow range of applications**.
 
Here is one real-world use case. A space-time collision problem involves about 25 billion rows of data. It seems that the SQL statement is not complicated:

```
WITH DT AS ( SELECT DISTINCT id, ROUND(tm/900)+1 as tn, loc FROM T WHERE tm<3*86400)
SELECT * FROM (
    SELECT B.id id, COUNT( DISINCT B.tn ) cnt
    FROM DT AS A JOIN DT AS B ON A.loc=B.loc AND A.tn=B.tn
    WHERE A.id=a AND B.id<>a
    GROUP BY id )
ORDER BY cnt DESC
LIMIT 20
```

Traditional databases run too slow and users turn to ClickHouse to solve the problem. ClickHouse runs over 30 minutes under a 5-node cluster environment and falls short of expectation. With the same amount of data, it takes SPL code one node and less than 6 minutes to complete the computation, which exceeds users’ expectations. Taking account of the gap between hardware resources, SPL is over 25 times faster than ClickHouse.

<table> 
 <thead> 
  <tr>
   <th> </th>
   <th> A </th>
  </tr> 
 </thead> 
 <tbody> 
  <tr>
   <td> 1 </td>
   <td> =now() </td>
  </tr> 
  <tr>
   <td> 2 </td>
   <td> &gt;NL=100000,NT=3*96 </td>
  </tr> 
  <tr>
   <td> 3 </td>
   <td> =file("T.ctx").open() </td>
  </tr> 
  <tr>
   <td> 4 </td>
   <td> =A3.cursor(tm,loc;id==a).fetch().align(NL*NT,(loc-1)*NT+tm\900+1) </td>
  </tr> 
  <tr>
   <td> 5 </td>
   <td> =A3.cursor@mv(;id!=a &amp;&amp; A4((loc-1)*NT+tm\900+1)) </td>
  </tr> 
  <tr>
   <td> 6 </td>
   <td> =A5.group@s(id;icount@o(tm\900):cnt).total(top(-20;cnt)) </td>
  </tr> 
  <tr>
   <td> 7 </td>
   <td> =interval@ms(A1,now()) </td>
  </tr> 
 </tbody> 
</table> 

(Unlike the code produced by other programming languages, SPL code is written in a grid. Find related information [HERE](http://c.raqsoft.com/article/1698110342313)).
The SQL DISTINCT computation involves HASH values and their comparison. It becomes computation-intensive when data volume is huge. The self-join and the further COUNT(DISTINCT) will also seriously slow down the performance. SPL can make the most use of ordered grouping and ordinal-number-based location that SQL does not have to effectively avoid the high complexity self-joins and DISTINCT operations. Though SPL does not have the advantage over ClickHouse in the aspect of storage efficiency and Java is a little slower than C++, it still boosts the performance by orders of magnitude.
 
Finally, get esProc SPL in https://github.com/SPLWare/esProc.