Project: ClickHouse (App Definition and Development / Database)
File: star-schema.md

slug: /en/getting-started/example-datasets/star-schema sidebar_label: Star Schema Benchmark description: "Dataset based on the TPC-H dbgen source. The coding style and architecture follows the TPCH dbgen." Compiling dbgen: ``` bash $ git clone git@github.com:vadimtk/ssb-dbgen.git $ cd ssb-dbgen $ make ``` Generating data: :::note With `-s 100` dbgen generates 600 million rows (67 GB), while while `-s 1000` it generates 6 billion rows (which takes a lot of time) ::: ``` bash $ ./dbgen -s 1000 -T c $ ./dbgen -s 1000 -T l $ ./dbgen -s 1000 -T p $ ./dbgen -s 1000 -T s ``` Creating tables in ClickHouse: ``` sql CREATE TABLE customer ( C_CUSTKEY UInt32, C_NAME String, C_ADDRESS String, C_CITY LowCardinality(String), C_NATION LowCardinality(String), C_REGION LowCardinality(String), C_PHONE String, C_MKTSEGMENT LowCardinality(String) ) ENGINE = MergeTree ORDER BY (C_CUSTKEY); CREATE TABLE lineorder ( LO_ORDERKEY UInt32, LO_LINENUMBER UInt8, LO_CUSTKEY UInt32, LO_PARTKEY UInt32, LO_SUPPKEY UInt32, LO_ORDERDATE Date, LO_ORDERPRIORITY LowCardinality(String), LO_SHIPPRIORITY UInt8, LO_QUANTITY UInt8, LO_EXTENDEDPRICE UInt32, LO_ORDTOTALPRICE UInt32, LO_DISCOUNT UInt8, LO_REVENUE UInt32, LO_SUPPLYCOST UInt32, LO_TAX UInt8, LO_COMMITDATE Date, LO_SHIPMODE LowCardinality(String) ) ENGINE = MergeTree PARTITION BY toYear(LOORDERDATE) ORDER BY (LOORDERDATE, LO_ORDERKEY); CREATE TABLE part ( P_PARTKEY UInt32, P_NAME String, P_MFGR LowCardinality(String), P_CATEGORY LowCardinality(String), P_BRAND LowCardinality(String), P_COLOR LowCardinality(String), P_TYPE LowCardinality(String), P_SIZE UInt8, P_CONTAINER LowCardinality(String) ) ENGINE = MergeTree ORDER BY P_PARTKEY; CREATE TABLE supplier ( S_SUPPKEY UInt32, S_NAME String, S_ADDRESS String, S_CITY LowCardinality(String), S_NATION LowCardinality(String), S_REGION LowCardinality(String), S_PHONE String ) ENGINE = MergeTree ORDER BY S_SUPPKEY; ``` Inserting data: ``` bash $ clickhouse-client --query "INSERT INTO customer FORMAT CSV" < customer.tbl $ clickhouse-client --query "INSERT INTO part FORMAT CSV" < part.tbl $ clickhouse-client --query "INSERT INTO supplier FORMAT CSV" < supplier.tbl $ clickhouse-client --query "INSERT INTO lineorder FORMAT CSV" < lineorder.tbl ``` Converting star schema to denormalized flat schema: ``` sql SET maxmemoryusage = 20000000000; CREATE TABLE lineorder_flat ENGINE = MergeTree ORDER BY (LOORDERDATE, LOORDERKEY) AS SELECT l.LOORDERKEY AS LOORDERKEY, l.LOLINENUMBER AS LOLINENUMBER, l.LOCUSTKEY AS LOCUSTKEY, l.LOPARTKEY AS LOPARTKEY, l.LOSUPPKEY AS LOSUPPKEY, l.LOORDERDATE AS LOORDERDATE, l.LOORDERPRIORITY AS LOORDERPRIORITY, l.LOSHIPPRIORITY AS LOSHIPPRIORITY, l.LOQUANTITY AS LOQUANTITY, l.LOEXTENDEDPRICE AS LOEXTENDEDPRICE, l.LOORDTOTALPRICE AS LOORDTOTALPRICE, l.LODISCOUNT AS LODISCOUNT, l.LOREVENUE AS LOREVENUE, l.LOSUPPLYCOST AS LOSUPPLYCOST, l.LOTAX AS LOTAX, l.LOCOMMITDATE AS LOCOMMITDATE, l.LOSHIPMODE AS LOSHIPMODE, c.CNAME AS CNAME, c.CADDRESS AS CADDRESS, c.CCITY AS CCITY, c.CNATION AS CNATION, c.CREGION AS CREGION, c.CPHONE AS CPHONE, c.CMKTSEGMENT AS CMKTSEGMENT, s.SNAME AS SNAME, s.SADDRESS AS SADDRESS, s.SCITY AS SCITY, s.SNATION AS SNATION, s.SREGION AS SREGION, s.SPHONE AS SPHONE, p.PNAME AS PNAME, p.PMFGR AS PMFGR, p.PCATEGORY AS PCATEGORY, p.PBRAND AS PBRAND, p.PCOLOR AS PCOLOR, p.PTYPE AS PTYPE, p.PSIZE AS PSIZE, p.PCONTAINER AS PCONTAINER FROM lineorder AS l INNER JOIN customer AS c ON c.CCUSTKEY = l.LOCUSTKEY INNER JOIN supplier AS s ON s.SSUPPKEY = l.LOSUPPKEY INNER JOIN part AS p ON p.PPARTKEY = l.LOPARTKEY; ``` Running the queries: Q1.1 ``` sql SELECT sum(LOEXTENDEDPRICE * LODISCOUNT) AS revenue FROM lineorder_flat WHERE toYear(LOORDERDATE) = 1993 AND LODISCOUNT BETWEEN 1 AND 3 AND LO_QUANTITY < 25; ```

``` sql SELECT sum(LOEXTENDEDPRICE * LODISCOUNT) AS revenue FROM lineorder_flat WHERE toYYYYMM(LOORDERDATE) = 199401 AND LODISCOUNT BETWEEN 4 AND 6 AND LO_QUANTITY BETWEEN 26 AND 35; ``` Q1.3 ``` sql SELECT sum(LOEXTENDEDPRICE * LODISCOUNT) AS revenue FROM lineorder_flat WHERE toISOWeek(LOORDERDATE) = 6 AND toYear(LOORDERDATE) = 1994 AND LODISCOUNT BETWEEN 5 AND 7 AND LOQUANTITY BETWEEN 26 AND 35; ``` Q2.1 ``` sql SELECT sum(LO_REVENUE), toYear(LO_ORDERDATE) AS year, P_BRAND FROM lineorder_flat WHERE PCATEGORY = 'MFGR#12' AND SREGION = 'AMERICA' GROUP BY year, P_BRAND ORDER BY year, P_BRAND; ``` Q2.2 ``` sql SELECT sum(LO_REVENUE), toYear(LO_ORDERDATE) AS year, P_BRAND FROM lineorder_flat WHERE PBRAND >= 'MFGR#2221' AND PBRAND <= 'MFGR#2228' AND S_REGION = 'ASIA' GROUP BY year, P_BRAND ORDER BY year, P_BRAND; ``` Q2.3 ``` sql SELECT sum(LO_REVENUE), toYear(LO_ORDERDATE) AS year, P_BRAND FROM lineorder_flat WHERE PBRAND = 'MFGR#2239' AND SREGION = 'EUROPE' GROUP BY year, P_BRAND ORDER BY year, P_BRAND; ``` Q3.1 ``` sql SELECT C_NATION, S_NATION, toYear(LO_ORDERDATE) AS year, sum(LO_REVENUE) AS revenue FROM lineorder_flat WHERE CREGION = 'ASIA' AND SREGION = 'ASIA' AND year >= 1992 AND year <= 1997 GROUP BY C_NATION, S_NATION, year ORDER BY year ASC, revenue DESC; ``` Q3.2 ``` sql SELECT C_CITY, S_CITY, toYear(LO_ORDERDATE) AS year, sum(LO_REVENUE) AS revenue FROM lineorder_flat WHERE CNATION = 'UNITED STATES' AND SNATION = 'UNITED STATES' AND year >= 1992 AND year <= 1997 GROUP BY C_CITY, S_CITY, year ORDER BY year ASC, revenue DESC; ``` Q3.3 ``` sql SELECT C_CITY, S_CITY, toYear(LO_ORDERDATE) AS year, sum(LO_REVENUE) AS revenue FROM lineorder_flat WHERE (CCITY = 'UNITED KI1' OR CCITY = 'UNITED KI5') AND (SCITY = 'UNITED KI1' OR SCITY = 'UNITED KI5') AND year >= 1992 AND year <= 1997 GROUP BY C_CITY, S_CITY, year ORDER BY year ASC, revenue DESC; ``` Q3.4 ``` sql SELECT C_CITY, S_CITY, toYear(LO_ORDERDATE) AS year, sum(LO_REVENUE) AS revenue FROM lineorder_flat WHERE (CCITY = 'UNITED KI1' OR CCITY = 'UNITED KI5') AND (SCITY = 'UNITED KI1' OR SCITY = 'UNITED KI5') AND toYYYYMM(LO_ORDERDATE) = 199712 GROUP BY C_CITY, S_CITY, year ORDER BY year ASC, revenue DESC; ``` Q4.1 ``` sql SELECT toYear(LO_ORDERDATE) AS year, C_NATION, sum(LOREVENUE - LOSUPPLYCOST) AS profit FROM lineorder_flat WHERE CREGION = 'AMERICA' AND SREGION = 'AMERICA' AND (PMFGR = 'MFGR#1' OR PMFGR = 'MFGR#2') GROUP BY year, C_NATION ORDER BY year ASC, C_NATION ASC; ``` Q4.2 ``` sql SELECT toYear(LO_ORDERDATE) AS year, S_NATION, P_CATEGORY, sum(LOREVENUE - LOSUPPLYCOST) AS profit FROM lineorder_flat WHERE CREGION = 'AMERICA' AND SREGION = 'AMERICA' AND (year = 1997 OR year = 1998) AND (PMFGR = 'MFGR#1' OR PMFGR = 'MFGR#2') GROUP BY year, S_NATION, P_CATEGORY ORDER BY year ASC, S_NATION ASC, P_CATEGORY ASC; ``` Q4.3 ``` sql SELECT toYear(LO_ORDERDATE) AS year, S_CITY, P_BRAND, sum(LOREVENUE - LOSUPPLYCOST) AS profit FROM lineorder_flat WHERE SNATION = 'UNITED STATES' AND (year = 1997 OR year = 1998) AND PCATEGORY = 'MFGR#14' GROUP BY year, S_CITY, P_BRAND ORDER BY year ASC, S_CITY ASC, P_BRAND ASC; ```