Reputation: 17
I'm setting up a new service that manage some payment transactions. I have four use cases:
To reach this goal I created three tables:
CREATE TABLE test.transaction_by_device (
uid uuid ,
device text,
time timestamp,
amount double,
currency text,
cardNumber text,
PRIMARY KEY ((device),time)
) WITH CLUSTERING ORDER BY (time DESC);
CREATE TABLE test.transaction_by_cardNumber (
uid uuid ,
device text,
time timestamp,
amount double,
currency text,
cardNumber text,
PRIMARY KEY ((cardNumber),time)
) WITH CLUSTERING ORDER BY (time DESC);
CREATE TABLE test.transaction_by_time (
uid uuid ,
device text,
year text,
month text,
day text,
time timestamp,
amount double,
currency text,
cardNumber text,
PRIMARY KEY ((year,month,day),time)
) WITH CLUSTERING ORDER BY (time DESC);
The queries that I used are:
const select_last_10_transactions_by_time =
'SELECT * FROM test.transaction_by_time LIMIT 10';
const select_last_10_transactions_of_the_day_by_time =
"SELECT * FROM test.transaction_by_time WHERE YEAR='2019' AND MONTH='2' AND DAY='22'";
const select_last_10_transactions_by_cardNumber =
"SELECT * FROM test.transaction_by_cardNumber where cardNumber='4242800217402773' LIMIT 10";
const select_last_10_transactions_by_device =
"SELECT * FROM test.transaction_by_device where device='device01' ";
Everything is working very well, and select time for 1 million row is about 0.5s
The problem is that every data are duplicated in every table. What I'm doing wrong with the data-modeling in this use cases?
Is there a better way to optimize these queries/tables?
I read something about the materialized view but I read also that aren't ready for production.
Thanks.
Upvotes: 1
Views: 80
Reputation: 16400
The problem is that every data are duplicated in every table. What I'm doing wrong with the data-modeling in this use cases?
You are not doing it wrong, you are doing it right. Disk space is usually far cheaper than trying to do distributed joins. Especially tiny data like this. If anythings wrong I would worry about your partitions for a single card or device growing too large since its essentially unbounded. What if a customer sets up thing to make a transaction from a device every second? After a month or year that will get a bit difficult to work with.
Upvotes: 1