Skip to content

Instantly share code, notes, and snippets.

@pmcfadin
Created October 30, 2014 04:46
Show Gist options
  • Save pmcfadin/55917688012bde9a5bba to your computer and use it in GitHub Desktop.
Save pmcfadin/55917688012bde9a5bba to your computer and use it in GitHub Desktop.
Data model for storing active orders
CREATE TABLE event (
id uuid,
active text,
event_time timestamp,
event text,
PRIMARY KEY ((id), active, event_time)
);
SELECT id FROM event WHERE id=<uuid> AND acrive='Y';
CREATE TABLE active_orders (
day text, //Date to the day. If high volume go to hour or minute for better partitioning
order_time timestamp,
id uuid,
event text,
PRIMARY KEY ((day), order_time)
) WITH CLUSTERING ORDER BY (order_time DESC);
SELECT * from active_orders WHERE day='20141030' AND order_time < '201410301200';
You'll want to reverse this to avoid any issues with tombstones and just make queries more efficient.
As a variant on the same index:
CREATE TABLE active_orders2 (
day text, //Date to the day. If high volume go to hour or minute for better partitioning
product text, // If looking to track all active orders for a specific product
order_time timestamp,
id uuid,
event text,
PRIMARY KEY ((day, product), order_time)
) WITH CLUSTERING ORDER BY (order_time DESC);
// All active orders for October 30
SELECT * FROM active_orders2 WHERE day='20141030' AND product = '12345';
// All active orders for October 30 between 9-11AM
SELECT * FROM active_orders2 WHERE day='20141030' AND product = '12345' AND order_time >= '201410300900' AND order_time <= '201410301100';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment