Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cassandra - using "date" vs "text" types for a partition date key

Tags:

cassandra

We have a schema where the partition key will be a date (yyyy-MM-dd) and we are thinking about choosing the data type between text and date for this partition key.

Does one data type provide benefits over the other and how would they differ in querying/storage?

Here is an example schema.

CREATE TABLE test.user_sessions (
    sess_date date (or text),
    sess_starttime timestamp,
    event_type text,
    total_req int,
    ended_at timestamp
    PRIMARY KEY (sess_date, sess_starttime)
);
like image 822
maulik13 Avatar asked Mar 07 '17 08:03

maulik13


2 Answers

Cassandra Date Type :

Value is a date with no corresponding time value; Cassandra encodes date as a 32-bit integer representing days since epoch (January 1, 1970)

Cassandra Text Type :

UTF-8 encoded string; 16 bit for each character

If you store date (yyyy-MM-dd) as date data type each entry will only take 32-bit. On the other hand if you store the date as text it will take 10*16 = 160 bit storage.

like image 189
Ashraful Islam Avatar answered Nov 01 '22 20:11

Ashraful Islam


As per your comments, if you need maximum portability simply store the information as a timestamp (that is a 64-bit number) corresponding to the something like yyyy-MM-dd 00:00:00 (a truncated time stamp). You can't go wrong with an "universal" number...

like image 36
xmas79 Avatar answered Nov 01 '22 22:11

xmas79