Giter Site home page Giter Site logo

ClickHouse table schema about klogs HOT 4 CLOSED

kobsio avatar kobsio commented on May 30, 2024
ClickHouse table schema

from klogs.

Comments (4)

ricoberger avatar ricoberger commented on May 30, 2024

Hi @UnamedRus thanks for your help and suggestions. They are very welcome πŸ™‚.

The current schema is our first try and works quite nice. Since we are not that experienced with ClickHouse we are definitely open for improvements. Could you imagine to create a PR for the schema adjustments?

Regarding your questions:

  1. Yes host is the hostname of the k8s node. We were not sure if we should include it in the order by clause or not, because searching through the logs of a specific host is a rare use case for us. What do you think about?
  2. We are trying to reverse the timestamp order because most of the queries are using ORDER BY timestamp DESC. Does this makes sense? Currently the logs are retrieved as follows:

The user can provide a query (e.g. namespace='kobs' _and_ app='kobs' _and_ container_name='kobs') via the kobs ui, which is comparable with Kibana for Elasticsearch.

Based on the user input we are running the following query to create a list of buckets in the selected time range:

SELECT
  toStartOfInterval(timestamp, INTERVAL 30 second) AS interval_data,
  count(*) AS count_data
FROM
  logs.logs
WHERE
  timestamp >= FROM_UNIXTIME(1641923841)
  AND timestamp <= FROM_UNIXTIME(1641924741)
  AND namespace='kobs'
  AND app='kobs'
  AND container_name='kobs'
GROUP BY
  interval_data
ORDER BY
  interval_data
WITH FILL
  FROM toStartOfInterval(FROM_UNIXTIME(1641923841), INTERVAL 30 second)
  TO toStartOfInterval(FROM_UNIXTIME(1641924741), INTERVAL 30 second)
STEP
  30

The returned data is then used to render the buckets chart in the UI and looks as follows:

[{"interval":1641923820,"count":0},{"interval":1641923850,"count":0},{"interval":1641923880,"count":0},{"interval":1641923910,"count":0},{"interval":1641923940,"count":0},{"interval":1641923970,"count":0},{"interval":1641924000,"count":0},{"interval":1641924030,"count":0},{"interval":1641924060,"count":4},{"interval":1641924090,"count":0},{"interval":1641924120,"count":0},{"interval":1641924150,"count":0},{"interval":1641924180,"count":4},{"interval":1641924210,"count":1},{"interval":1641924240,"count":0},{"interval":1641924270,"count":0},{"interval":1641924300,"count":0},{"interval":1641924330,"count":0},{"interval":1641924360,"count":0},{"interval":1641924390,"count":0},{"interval":1641924420,"count":0},{"interval":1641924450,"count":0},{"interval":1641924480,"count":0},{"interval":1641924510,"count":0},{"interval":1641924540,"count":0},{"interval":1641924570,"count":0},{"interval":1641924600,"count":0},{"interval":1641924630,"count":0},{"interval":1641924660,"count":0},{"interval":1641924690,"count":0}]

We are then using this list to create the query to get the logs from ClickHouse. For that we are only looking into the intervals were the count is larger then 0, which was a good optimization for large time intervals with a small number of logs (<1000).

SELECT
  timestamp,
  cluster,
  namespace,
  app,
  pod_name,
  container_name,
  host,
  fields_string.key,
  fields_string.value,
  fields_number.key,
  fields_number.value,
  log
FROM
  logs.logs
WHERE
  (
    (timestamp >= FROM_UNIXTIME(1641924210) AND timestamp <= FROM_UNIXTIME(1641924240))
    OR (timestamp >= FROM_UNIXTIME(1641924180) AND timestamp <= FROM_UNIXTIME(1641924210))
    OR (timestamp >= FROM_UNIXTIME(1641924060) AND timestamp <= FROM_UNIXTIME(1641924090))
  )
  AND namespace='kobs'
  AND app='kobs'
  AND container_name='kobs'
ORDER BY
  timestamp DESC
LIMIT
  1000
  1. Your provided query returns the following results for one of the ClickHouse nodes (the results on the other nodes are looking very similar):
β”Œβ”€database─┬─table──────┬─column──────────────────────────┬─type───────────────────┬───────rows─┬─compressed_bytes─┬─compressed──┬─uncompressed─┬──────────────ratio─┬─codec────────────────────┐
β”‚ logs     β”‚ logs_local β”‚ fields_string.key               β”‚ Array(String)          β”‚ 2329771851 β”‚     358178446573 β”‚ 333.58 GiB  β”‚ 2.86 TiB     β”‚  8.784910317881737 β”‚ CODEC(ZSTD(1))           β”‚
β”‚ logs     β”‚ logs_local β”‚ fields_string.value             β”‚ Array(String)          β”‚ 2329771851 β”‚     350285240753 β”‚ 326.23 GiB  β”‚ 2.35 TiB     β”‚  7.366783332109033 β”‚ CODEC(ZSTD(1))           β”‚
β”‚ logs     β”‚ logs_local β”‚ log                             β”‚ String                 β”‚ 2329771851 β”‚     303909067614 β”‚ 283.04 GiB  β”‚ 2.23 TiB     β”‚   8.05202209102257 β”‚ CODEC(ZSTD(1))           β”‚
β”‚ logs     β”‚ logs_local β”‚ fields_number.key               β”‚ Array(String)          β”‚ 2329771851 β”‚      16199072105 β”‚ 15.09 GiB   β”‚ 176.53 GiB   β”‚ 11.701033451940425 β”‚ CODEC(ZSTD(1))           β”‚
β”‚ logs     β”‚ logs_local β”‚ fields_number.value             β”‚ Array(Float64)         β”‚ 2329771851 β”‚      12967873005 β”‚ 12.08 GiB   β”‚ 60.11 GiB    β”‚  4.977477356781071 β”‚ CODEC(ZSTD(1))           β”‚
β”‚ logs     β”‚ logs_local β”‚ content.response_code           β”‚ Float64                β”‚ 2329771851 β”‚       2621257346 β”‚ 2.44 GiB    β”‚ 17.36 GiB    β”‚  7.110369504330232 β”‚                          β”‚
β”‚ logs     β”‚ logs_local β”‚ content.loggerName              β”‚ String                 β”‚ 2329771851 β”‚       1211619167 β”‚ 1.13 GiB    β”‚ 17.37 GiB    β”‚  15.39396583018895 β”‚                          β”‚
β”‚ logs     β”‚ logs_local β”‚ timestamp                       β”‚ DateTime64(3)          β”‚ 2329771851 β”‚        678452055 β”‚ 647.02 MiB  β”‚ 17.36 GiB    β”‚  27.47151867054187 β”‚ CODEC(Delta(8), ZSTD(1)) β”‚
β”‚ logs     β”‚ logs_local β”‚ content.contextMap.branch.appID β”‚ String                 β”‚ 2329771851 β”‚        582468072 β”‚ 555.48 MiB  β”‚ 3.84 GiB     β”‚  7.083428064706008 β”‚                          β”‚
β”‚ logs     β”‚ logs_local β”‚ content.level                   β”‚ String                 β”‚ 2329771851 β”‚        245835895 β”‚ 234.45 MiB  β”‚ 3.69 GiB     β”‚ 16.101778314350717 β”‚                          β”‚
β”‚ logs     β”‚ logs_local β”‚ host                            β”‚ String                 β”‚ 2329771851 β”‚         67291481 β”‚ 64.17 MiB   β”‚ 82.48 GiB    β”‚  1316.036386478104 β”‚ CODEC(ZSTD(1))           β”‚
β”‚ logs     β”‚ logs_local β”‚ pod_name                        β”‚ String                 β”‚ 2329771851 β”‚         57316257 β”‚ 54.66 MiB   β”‚ 66.39 GiB    β”‚  1243.760170958128 β”‚ CODEC(ZSTD(1))           β”‚
β”‚ logs     β”‚ logs_local β”‚ app                             β”‚ String                 β”‚ 2329771851 β”‚         21185633 β”‚ 20.20 MiB   β”‚ 32.99 GiB    β”‚ 1671.7998898121193 β”‚ CODEC(ZSTD(1))           β”‚
β”‚ logs     β”‚ logs_local β”‚ container_name                  β”‚ String                 β”‚ 2329771851 β”‚         16967547 β”‚ 16.18 MiB   β”‚ 27.96 GiB    β”‚ 1769.4741842176716 β”‚ CODEC(ZSTD(1))           β”‚
β”‚ logs     β”‚ logs_local β”‚ namespace                       β”‚ LowCardinality(String) β”‚ 2329771851 β”‚          9309111 β”‚ 8.88 MiB    β”‚ 2.19 GiB     β”‚  252.3147725921412 β”‚ CODEC(ZSTD(1))           β”‚
β”‚ logs     β”‚ logs_local β”‚ cluster                         β”‚ LowCardinality(String) β”‚ 2329771851 β”‚          9181883 β”‚ 8.76 MiB    β”‚ 2.19 GiB     β”‚  255.7960722217872 β”‚ CODEC(ZSTD(1))           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

from klogs.

UnamedRus avatar UnamedRus commented on May 30, 2024

Can you also show result of this query:

SELECT uniqCombinedArray(fields_string.key), uniqCombinedArray(fields_string.value), uniqCombinedArray(fields_number.key), count() FROM logs.logs_local WHERE timestamp  > '2022-01-01';

We are trying to reverse the timestamp order because most of the queries are using ORDER BY timestamp DESC. Does this makes sense? Currently the logs are retrieved as follows:

In general, it's only required for 2 things:

  1. Place similar data near by, (improve compression) but for this case it really doesn't matter in which exact order you do this.
  2. read_in_order optimizations.

https://clickhouse.com/docs/en/sql-reference/statements/select/order-by/#optimize_read_in_order
https://clickhouse.com/docs/en/sql-reference/statements/select/group-by/#aggregation-in-order

But for your case (when you do simple ORDER BY timestamp DESC) they doesn't work( for now :) )

ClickHouse/ClickHouse#7102
ClickHouse/ClickHouse#32748

Does this makes sense?

But if we will assume that they work for you now.
There were some cases, when reading in reverse order worked worse than in direct order. So it's better to test.
ClickHouse/ClickHouse#16250

from klogs.

ricoberger avatar ricoberger commented on May 30, 2024

Sure, here are the results:

β”Œβ”€uniqCombinedArray(fields_string.key)─┬─uniqCombinedArray(fields_string.value)─┬─uniqCombinedArray(fields_number.key)─┬───count()─┐
β”‚                                35392 β”‚                             1665223720 β”‚                                 6418 β”‚ 680701758 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

1 rows in set. Elapsed: 767.066 sec. Processed 680.70 million rows, 2.37 TB (887.41 thousand rows/s., 3.09 GB/s.)

from klogs.

ricoberger avatar ricoberger commented on May 30, 2024

Hi @UnamedRus, thanks again for all your suggestions πŸ™‚.

We updated the table schema accordingly, so that I would close the issue for now.

If you have further suggestions feel free to open a new issue.

from klogs.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.