You're right; it does seem counterintuitive that the official ClickHouse documentation for time-series use cases would showcase ORDER BY tuple() in their example:
CREATE TABLE tsdb (
date Date MATERIALIZED toDate(timestamp),
timestamp DateTime,
symbol String,
price Float64
) ENGINE = MergeTree()
ORDER BY (symbol, timestamp);
And then they insert data and query it like this:
sql
INSERT INTO tsdb (timestamp, symbol, price) VALUES
('2023-01-01 10:00:00', 'AAPL', 100.0),
('2023-01-01 10:01:00', 'AAPL', 101.0),
('2023-01-01 10:00:00', 'GOOG', 500.0),
('2023-01-01 10:01:00', 'GOOG', 501.0);
-- Notice ORDER BY tuple() here
SELECT * FROM tsdb ORDER BY tuple() LIMIT 4;
The "Gotcha": They're using ORDER BY tuple()specifically for demonstration purposes in the SELECT query, not as a recommendation for production time-series queries.
Here's the subtle context:
The CREATE TABLE statement does define a meaningful ORDER BY:
sql
ENGINE = MergeTree()
ORDER BY (symbol, timestamp);
This means the data is physically stored sorted first by symbol (stock ticker) and then by timestamp within each symbol group. This is perfect for time-series queries where you often filter by symbol and then need chronological order.
The SELECT query with ORDER BY tuple() is a trick for the docs:
They're deliberately avoiding the natural ORDER BY (symbol, timestamp) behavior (which would be applied implicitly due to the table's storage order) just to show the raw, unsorted output directly from storage.
By using ORDER BY tuple(), they're forcing ClickHouse to bypass the inherent storage order and return rows in whatever internal order is fastest (e.g., how data parts were merged, inserted, or read from disk).
This helps illustrate that the physical storage order (ORDER BY (symbol, timestamp)) isn't magically applied to SELECT * results unless you explicitly ask for it (via ORDER BY in the query).
Why do this in the docs?
Educational clarity: They want to demonstrate that:
The table's ORDER BY (symbol, timestamp) defines storage order, not query result order.
Without an explicit ORDER BY in the SELECT, you can't rely on any particular sequence.
Preventing misconceptions: If they had just done SELECT * FROM tsdb LIMIT 4, readers might (wrongly) assume: "Oh, ClickHouse automatically returns data in (symbol, timestamp) order because that's how it's stored." By jumbling the results with ORDER BY tuple(), they prove that's not the case.
Setting up the next step: Immediately after this example, they show queries like:
sql
SELECT * FROM tsdb WHERE symbol = 'AAPL' ORDER BY timestamp;
Here, the explicit ORDER BY timestamp gives the expected chronological results for a specific symbol. This drives home the point: for meaningful time-series analysis, you must use ORDER BY in your queries if you need a specific order.
In production, you'd never use ORDER BY tuple() for time-series analysis unless:
You're doing a quick sanity check/debug dump and order doesn't matter.
You're using it with LIMIT for a truly random sample (though ORDER BY rand() is better for that).
For actual time-series work:
Define a sensible ORDER BY in CREATE TABLE (e.g., (symbol, timestamp) or just timestamp if no additional dimensions).
Use explicit ORDER BY in your SELECT queries whenever order matters (which is almost always).
The docs aren't recommending ORDER BY tuple() for time-series queries; they're using it as a teaching tool to highlight the difference between storage order (CREATE TABLE ... ORDER BY) and result set order (SELECT ... ORDER BY).
TL;DR: The ClickHouse team is being pedagogically sneaky here. Don't let this example fool you into using ORDER BY tuple() in real time-series queries. Always specify the ORDER BY you need.