TapCub is live — analytics, insights and live chat, free on one platform
Analytics

Clickstream database: how to store and query event data

A clickstream database is an append-only events table read by scanning a few columns. Which storage fits, a schema that works, what a row costs in bytes for a wide and a narrow design, and when you do not need your own.

Xu An Published Nov 22, 2026 14 min read Analytics
Sample data: assumed string lengths (UUID 36, URL 60, referrer 30) and BigQuery logical sizes. The narrow design saves 46.7%, or 17.1 GB per 30 days at 5 million events a day.Sample data

A clickstream database is an append-only events table with one row per interaction, read by scanning a few columns across many rows. That access pattern favours columnar storage partitioned by date. In the sample here, replacing long strings with integer ids cuts a row from 244 to 130 bytes, and a two-column query reads 9% of the table.

What you will learn

  • Events are written once and scanned by a few columns, so columnar storage with daily partitions usually fits
  • Sample row: 244 bytes wide vs 130 bytes with integer ids, 36.6 GB vs 19.5 GB per 30 days at 5 million events a day
  • A column scan costs the bytes of the columns read: 3.3 GB instead of 36.6 GB for events per day by name

What is a clickstream database?

A clickstream database is any store whose main table has one row per recorded interaction: a page view, a click, a form submission, a purchase. Each row says who did what, where and when, and the table only grows. The job of the schema is to keep that table cheap to write, cheap to scan and safe to delete from.

That job is different from a normal application database. An orders table gets updated and looked up by key. An events table is written once, never updated, and read by scanning a few columns across millions of rows ("how many checkout_start events per day, last 30 days"). Almost every sensible choice below follows from those two access patterns. For the definition of the data itself, see clickstream data; for how events travel from the browser to storage, see data pipeline.

Which kind of storage fits event data?

Pick by how the table will be read, not by which product is popular. There are four common homes:

StorageGood atWeak atFits when
Row-oriented relational databaseSingle-row lookups, updates, transactionsScanning wide tables; storage grows fastUnder a few million events in total, or a live "last 50 events for user X" view
Columnar warehouseAggregations over a few columns of billions of rowsRow-by-row updates; very small queriesMost analysis: funnels, retention, daily counts
Columnar files on object storageCheap long-term storage, many engines can read itYou manage partitions, schema changes and compactionArchive, or a lake you query occasionally
Specialised event or time-series storesFast ingestion and time-range queriesJoins across large tablesHigh write rates with mostly time-bounded questions

The columnar choices work because of how the data is laid out. The Apache Parquet format splits a table into row groups, and within each row group stores one chunk per column, with the metadata kept at the end of the file so a reader can fetch only the column chunks a query needs. If your query reads event_name and event_time, the other eight columns are never read from disk. That is the whole reason a warehouse beats a row store on clickstream scans.

If you are not sure, start with a columnar warehouse and daily partitions. You can move to files later; going the other way is harder.

StorageGood atWeak atFits when
Row-oriented relational databaseSingle-row lookups, updatesScanning wide tablesUnder a few million events, or live per-user views
Columnar warehouseAggregations over a few columns of billions of rowsRow-by-row updatesMost analysis: funnels, retention, daily counts
Columnar files on object storageCheap long-term storageYou manage partitions and compactionArchive or an occasional-query lake
Event or time-series storeFast ingestion, time-range queriesJoins across large tablesHigh write rates, time-bounded questions

Pick by how the table will be read, not by product popularity.

What does a good events table look like?

Start with one wide table with a small, fixed set of columns, plus one flexible column for extras. A workable core:

ColumnTypeWhy it is there
event_idstring or integerDe-duplication when the same event arrives twice
event_timetimestamp (UTC)The partition key; store UTC, convert at read time
visitor_idstringAnonymous identity per browser or device
user_idstring, nullableFilled after login; keep the anonymous ID too
session_idstringSession grouping if your collector assigns one
event_namestringpage_view, click, purchase...
page_url or page_idstring or integerWhere it happened
referrer, utm_*stringsWhere the visitor came from
device, countrystringsSegments you filter on every day
propsJSON or repeated key/valueEverything specific to one event type

Google's documentation of the Analytics BigQuery export is a useful reference for a mature design: it writes one daily table per day named events_YYYYMMDD, models event_params and user_properties as repeated key/value records, and says daily tables can receive late-arriving events for up to three days after the event date. Copy the ideas (daily tables, key/value extras, tolerance for late data), not the exact column names.

Two rules prevent most schema regrets. First, never rename or reuse an event_name; add a new one and map the old one in a view. Second, keep the common filter columns (device, country, utm_source) as real columns. Hiding them inside props makes every query parse text.

How much does a row cost, and what does a narrower design save?

Work out the bytes per row before you pick a size for the machine. BigQuery's documentation of logical data sizes gives a convenient yardstick: an INT64 or TIMESTAMP value is 8 logical bytes, a STRING is 2 bytes plus its UTF-8 length, and a NULL is zero bytes. Applying that rule to the schema above, with sample lengths, gives two designs:

Sample data (typical string lengths assumed, not measured):

ColumnWide designBytesNarrow designBytes
event_idUUID string (36 chars)38INT64 sequence8
event_timeTIMESTAMP8TIMESTAMP8
visitor_idstring (16 chars)18string (16 chars)18
session_idstring (16 chars)18string (16 chars)18
event_namestring (12 chars)14INT64 dictionary id8
page_urlstring (60 chars)62INT64 page id8
referrerstring (30 chars)32INT64 referrer id8
devicestring (6 chars)8INT64 device id8
countrystring (2 chars)4string (2 chars)4
propsstring (40 chars)42string (40 chars)42
Total per event244130

The narrow design is 130 ÷ 244 = 53% of the size, a 46.7% saving, because repeated long strings (URLs, referrers) are replaced by 8-byte ids pointing at small lookup tables. At 5 million events per day:

  • Wide: 244 × 5,000,000 = 1.22 GB per day, 36.6 GB per 30 days, about 0.45 TB per year.
  • Narrow: 130 × 5,000,000 = 0.65 GB per day, 19.5 GB per 30 days, about 0.24 TB per year.
  • The lookup tables stay tiny: 20,000 distinct pages × (8 + 62) = 1.4 MB, and 2,000 referrers × (8 + 32) = 0.08 MB.

The saving is real, but it is not free. The narrow design needs joins or views to read, and every new URL needs a dictionary row written first. For a small site, the wide table is the simpler choice and the storage difference is a few gigabytes. Use the narrow design when you reach hundreds of millions of rows, or when you pay per byte scanned.

app.tapcub.com

Bytes per event by design

logical bytes
Wide (strings)244
Narrow (integer ids)130
Sample data
Sample data: assumed string lengths (UUID 36, URL 60, referrer 30) and BigQuery logical sizes. The narrow design saves 46.7%, or 17.1 GB per 30 days at 5 million events a day.

How should you partition and order the table?

Partition by event date and, inside each day, cluster or sort by the column you filter on most. Nearly every clickstream query has a time window ("last 7 days", "March"), so a date partition lets the engine skip whole days it does not need. Then sorting by visitor_id or session_id within a day makes session queries read nearby rows together.

Two practical details:

  1. Partition on event time, not arrival time, and expect late events. The GA4 export notes late arrivals for up to three days, so a "yesterday" report run at 01:00 can be missing rows. Re-run the last three days, or wait before publishing.
  2. Keep partitions at a sensible size. Very many tiny partitions add overhead, and hourly partitions at 5 million events per day (about 208,000 events per hour) are unnecessary. Daily is the usual grain until a single day is too large to scan comfortably.

Should extra properties be JSON, key/value pairs or columns?

Use columns for the properties you filter every day, key/value or JSON for the long tail, and promote a property to a column the day it appears in ten queries. The trade-off:

ApproachQuery costFlexibilityUse for
Real columnsLowest; columns are read only if usedSchema change needed for each new propertydevice, country, utm_source, revenue
JSON string columnParsed on every read; no column pruningAny shape, no migrationRarely used properties, early prototypes
Repeated key/value recordsNeeds an unnest or lookup per queryTyped values, any keyStable, mixed event types

Typed key/value records, like the string_value / int_value / double_value fields in the GA4 export, avoid the "everything is a string" trap: you can sum a revenue property without casting it. The price is longer queries.

How do you turn events into sessions and funnels?

Derive sessions at query time from the gap between events, so the rule can change without rewriting history. A common rule starts a new session after 30 minutes of inactivity. In standard SQL:

WITH gaps AS (
  SELECT visitor_id, event_time,
         TIMESTAMP_DIFF(event_time,
           LAG(event_time) OVER (PARTITION BY visitor_id ORDER BY event_time),
           MINUTE) AS gap_min
  FROM events
  WHERE event_time >= TIMESTAMP '2026-03-01'
)
SELECT visitor_id, event_time,
       SUM(CASE WHEN gap_min IS NULL OR gap_min > 30 THEN 1 ELSE 0 END)
         OVER (PARTITION BY visitor_id ORDER BY event_time) AS session_no
FROM gaps

A funnel is the same idea with conditional counts: count distinct sessions that reached each event_name in order. Keep the query on columns that exist in the table; asking a funnel to parse props is the most common reason it is slow.

What does a query actually cost?

On a columnar store, a query costs the bytes in the columns it reads, not the size of the table. Take the 30-day table from above (150 million rows) and the question "events per day by name", which reads event_name and event_time only:

  • Wide design: (14 + 8) = 22 bytes per row × 150,000,000 = 3.3 GB read, which is 9.0% of the 36.6 GB table.
  • Narrow design: (8 + 8) = 16 bytes per row × 150,000,000 = 2.4 GB read, which is 12.3% of its 19.5 GB.
  • On a row store, the same scan would read the full 36.6 GB, 11 times as much.

That is why adding a column costs almost nothing for queries that ignore it, and why SELECT * is the most expensive habit on an events table. The same arithmetic tells you whether to precompute: if a dashboard runs this query 200 times a day, build a daily summary table once and have the dashboard read that instead.

How long should you keep raw events?

Keep raw events as long as you will actually re-ask questions of them, and keep daily summaries much longer. One pattern is to keep raw events for 13 months, so year-on-year comparisons work, with daily aggregates (counts by day, source and page) kept indefinitely because they contain no personal data. Deleting a date partition is a metadata operation on most engines, while deleting one person's rows from a billion-row table is slow and expensive, so design partitions for the retention you want and have a separate process for erasure requests.

When you do not need your own clickstream database

If the questions are standard (traffic, sources, funnels, retention) and the data comes from your own site, you probably do not need one. Building and operating ingestion, de-duplication, partition management, bot filtering and privacy handling is a real job. Build your own store when you need to join events with data that cannot leave your systems, or when you have custom models to train on raw rows.

How to query event data in TapCub without running a database

If your question is "what happens to events after they leave the browser", TapCub already does the storing, and the table you would have designed has an equivalent in the product.

  1. Install the script from Web analytics › Configuration › SDK code. Page views, outbound clicks and file downloads are collected by default; for anything else, send named events from code or from the server through the HTTP interface.
  2. In All-platform analytics › Data › Events & properties and Event inbox, new event names land in an inbox and are claimed into the formal event list. That is the "never reuse an event name" rule, enforced as a workflow.
  3. Define your own metrics in All-platform analytics › Behavior › Metrics & SQL with a formula or an event plus an aggregation, or write SQL in the query console to run the session and funnel queries above against your project's events.
  4. If you do need raw rows in your own warehouse, use All-platform analytics › Data › Data export, which makes an asynchronous export of raw detail; the console itself shows summary data, and detail past the retention period has already been deleted.

TapCub is an analytics product, not a storage service you query directly from other systems for arbitrary joins; for that, export and load the data where you control the schema.

FAQ

What is a clickstream database?

It is a database whose main table records one row per user interaction, such as a page view or click, with who, what, where and when. It is append-only and read by scanning a few columns over many rows, so columnar, date-partitioned storage usually fits better than a row-oriented database built for transactions.

What is the best database for clickstream data?

It depends on volume and how you read it. For up to a few million events or live per-user lookups, a relational database works. For analysis over hundreds of millions of rows, a columnar warehouse with daily partitions is the usual fit. For archives, columnar files on object storage are cheapest.

How much storage does clickstream data need?

Multiply bytes per row by events per day. In the sample here, 244 bytes per event at 5 million events a day is 1.22 GB a day, or 36.6 GB per 30 days; replacing long strings with integer ids brings it to 130 bytes and 19.5 GB. Measure your own average string lengths first.

Should I store clickstream data in JSON?

Use JSON only for rarely queried extras. Every query on a JSON column parses text and cannot skip unused data. Keep properties you filter daily, such as device, country and campaign source, as real columns, and promote a JSON property to a column once it appears in many queries.

How long should I keep raw clickstream events?

Keep raw rows as long as you will re-ask questions of them, for example 13 months so you can compare year over year, and keep daily aggregates longer because they hold no personal data. Partition by date so that deleting old data is cheap.

Sources

Filed underAnalytics

Was this article helpful?

We use your answers to fix and expand this page.

Xu An

Customer Experience Consultant

Writes about live chat, visitor conversations, and self-service, sharing practical ways to make customer communication clearer and more effective.

Query your events without running a database

Define metrics, run SQL on your events and export raw detail when you need it, from one project.

See your data clearly. Find your growth.

Every click, backed by data. Install one line of code and see your first numbers in a minute.

No credit card · Free plans for analytics and chat · Cookieless analytics