Clickhouse: How to more table join?

Created on 28 Feb 2017  Β·  6Comments  Β·  Source: ClickHouse/ClickHouse

:] desc t1

DESCRIBE TABLE t1

β”Œβ”€name─┬─type───┬─default_type─┬─default_expression─┐
β”‚ x    β”‚ UInt32 β”‚              β”‚                    β”‚
β”‚ y    β”‚ String β”‚              β”‚                    β”‚
β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β†— Progress: 2.00 rows, 86.00 B (1.14 thousand rows/s., 49.14 KB/s.) 
2 rows in set. Elapsed: 0.002 sec. 

:] desc t2

DESCRIBE TABLE t2

β”Œβ”€name─┬─type───┬─default_type─┬─default_expression─┐
β”‚ x    β”‚ UInt32 β”‚              β”‚                    β”‚
β”‚ y    β”‚ String β”‚              β”‚                    β”‚
β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β†’ Progress: 2.00 rows, 86.00 B (1.17 thousand rows/s., 50.42 KB/s.) 
2 rows in set. Elapsed: 0.002 sec. 

:] desc t3

DESCRIBE TABLE t3

β”Œβ”€name─┬─type───┬─default_type─┬─default_expression─┐
β”‚ x    β”‚ UInt32 β”‚              β”‚                    β”‚
β”‚ y    β”‚ String β”‚              β”‚                    β”‚
β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β†˜ Progress: 2.00 rows, 86.00 B (1.21 thousand rows/s., 52.21 KB/s.) 
2 rows in set. Elapsed: 0.002 sec. 

:] 

I can't use this SQL
select x,y from t1 any inner join t2 any inner join t3 using x;
so,how to join more tables?

question

Most helpful comment

You have to join tables pairwise. Also, this means you have to manually choose join order:

:) SELECT x, y1, y2, y3 FROM (SELECT x, y AS y3 FROM t3) ALL INNER JOIN (SELECT x, y1, y2 FROM (SELECT x, y AS y1 FROM t1) ALL INNER JOIN (SELECT x, y AS y2 FROM t2) USING (x)) USING (x)

SELECT
    x,
    y1,
    y2,
    y3
FROM
(
    SELECT
        x,
        y AS y3
    FROM t3
)
ALL INNER JOIN
(
    SELECT
        x,
        y1,
        y2
    FROM
    (
        SELECT
            x,
            y AS y1
        FROM t1
    )
    ALL INNER JOIN
    (
        SELECT
            x,
            y AS y2
        FROM t2
    ) USING (x)
) USING (x)

β”Œβ”€x─┬─y1─┬─y2─┬─y3─┐
β”‚ 1 β”‚ t1 β”‚ t2 β”‚ t3 β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”˜

1 rows in set. Elapsed: 0.003 sec.

All 6 comments

You have to join tables pairwise. Also, this means you have to manually choose join order:

:) SELECT x, y1, y2, y3 FROM (SELECT x, y AS y3 FROM t3) ALL INNER JOIN (SELECT x, y1, y2 FROM (SELECT x, y AS y1 FROM t1) ALL INNER JOIN (SELECT x, y AS y2 FROM t2) USING (x)) USING (x)

SELECT
    x,
    y1,
    y2,
    y3
FROM
(
    SELECT
        x,
        y AS y3
    FROM t3
)
ALL INNER JOIN
(
    SELECT
        x,
        y1,
        y2
    FROM
    (
        SELECT
            x,
            y AS y1
        FROM t1
    )
    ALL INNER JOIN
    (
        SELECT
            x,
            y AS y2
        FROM t2
    ) USING (x)
) USING (x)

β”Œβ”€x─┬─y1─┬─y2─┬─y3─┐
β”‚ 1 β”‚ t1 β”‚ t2 β”‚ t3 β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”˜

1 rows in set. Elapsed: 0.003 sec.

so,I will join table A,B,C,D,E.
select ... (select column from C any inner join (select columns from A any inner join B) using (ID) ) using (ID) ......

my god ,If i need A join B and A join C and A join D,
B Join C and B join D...
I can't write this SQL. is very trouble .

Indeed, joining many tables is currently not very convenient but there are plans to improve the join syntax.

Also note that if many joins are necessary because your schema is some variant of the star schema and you need to join dimension tables to the fact table, then in ClickHouse you should use the external dictionaries feature instead.

@ztlpn
how to delete table ,this release is not support DELETE ?

To drop table, run DROP TABLE db.table.
There is no possibility to delete individual records from table.
Except the possibility to delete whole partition, see ALTER TABLE ... DROP PARTITION.

I mean,How to delete some record
Table A is 10 rows, I just delete 1 row,keep 9 rows in ClickHouse.

Was this page helpful?
0 / 5 - 0 ratings