Skip to content

Instantly share code, notes, and snippets.

@tzach
Last active February 18, 2019 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzach/05b39cfb22cfcf7b06885be07df70f19 to your computer and use it in GitHub Desktop.
Save tzach/05b39cfb22cfcf7b06885be07df70f19 to your computer and use it in GitHub Desktop.
### First Try
CREATE KEYSPACE IF NOT EXISTS mykeyspace WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
USE mykeyspace;
CREATE TABLE heartrate (
pet_chip_id uuid,
time timestamp,
heart_rate int,
PRIMARY KEY (pet_chip_id, time));
INSERT INTO heartrate(pet_chip_id, time, heart_rate) VALUES (123e4567-e89b-12d3-a456-426655440b23, '2019-03-04 07:01:00', 100);
INSERT INTO heartrate(pet_chip_id, time, heart_rate) VALUES (123e4567-e89b-12d3-a456-426655440b23, '2019-03-04 07:02:00', 103);
INSERT INTO heartrate(pet_chip_id, time, heart_rate) VALUES (123e4567-e89b-12d3-a456-426655440b23, '2019-03-04 07:03:00', 130);
INSERT INTO heartrate(pet_chip_id, time, heart_rate) VALUES (123e4567-e89b-12d3-a456-426655440b23, '2019-03-04 07:04:00', 131);
INSERT INTO heartrate(pet_chip_id, time, heart_rate) VALUES (123e4567-e89b-12d3-a456-426655440b23, '2019-03-04 07:05:00', 111);
INSERT INTO heartrate(pet_chip_id, time, heart_rate) VALUES (123e4567-e89b-12d3-a456-426655440b23, '2019-03-04 07:06:00', 99);
INSERT INTO heartrate(pet_chip_id, time, heart_rate) VALUES (123e4567-e89b-12d3-a456-426655440b23, '2019-03-05 07:04:00', 100);
INSERT INTO heartrate(pet_chip_id, time, heart_rate) VALUES (123e4567-e89b-12d3-a456-426655440b23, '2019-03-05 07:05:00', 99);
INSERT INTO heartrate(pet_chip_id, time, heart_rate) VALUES (123e4567-e89b-12d3-a456-426655440b23, '2019-03-05 07:06:00', 100);
SELECT * from heartrate WHERE pet_chip_id = 123e4567-e89b-12d3-a456-426655440b23;
SELECT * from heartrate WHERE pet_chip_id = 123e4567-e89b-12d3-a456-426655440b23 AND time>='2019-03-04 07:03:00' AND time <= '2019-03-05 07:05:00';
### Second
CREATE TABLE heartrate2 (
pet_chip_id uuid,
date text,
time timestamp,
heart_rate int,
PRIMARY KEY ((pet_chip_id, date), time));
INSERT INTO heartrate2(pet_chip_id, date, time, heart_rate) VALUES (123e4567-e89b-12d3-a456-426655440b23, '2019-03-04', '2019-03-04 07:01:00', 100);
INSERT INTO heartrate2(pet_chip_id, date, time, heart_rate) VALUES (123e4567-e89b-12d3-a456-426655440b23, '2019-03-04', '2019-03-04 07:02:00', 103);
INSERT INTO heartrate2(pet_chip_id, date, time, heart_rate) VALUES (123e4567-e89b-12d3-a456-426655440b23, '2019-03-04', '2019-03-04 07:03:00', 130);
INSERT INTO heartrate2(pet_chip_id, date, time, heart_rate) VALUES (123e4567-e89b-12d3-a456-426655440b23, '2019-03-04', '2019-03-04 07:04:00', 131);
INSERT INTO heartrate2(pet_chip_id, date, time, heart_rate) VALUES (123e4567-e89b-12d3-a456-426655440b23, '2019-03-04', '2019-03-04 07:05:00', 111);
INSERT INTO heartrate2(pet_chip_id, date, time, heart_rate) VALUES (123e4567-e89b-12d3-a456-426655440b23, '2019-03-04', '2019-03-04 07:06:00', 99);
INSERT INTO heartrate2(pet_chip_id, date, time, heart_rate) VALUES (123e4567-e89b-12d3-a456-426655440b23, '2019-03-05', '2019-03-05 07:04:00', 100);
INSERT INTO heartrate2(pet_chip_id, date, time, heart_rate) VALUES (123e4567-e89b-12d3-a456-426655440b23, '2019-03-05', '2019-03-05 07:05:00', 99);
INSERT INTO heartrate2(pet_chip_id, date, time, heart_rate) VALUES (123e4567-e89b-12d3-a456-426655440b23, '2019-03-05', '2019-03-05 07:06:00', 100);
SELECT * from heartrate2 WHERE pet_chip_id = 123e4567-e89b-12d3-a456-426655440b23 AND time>='2019-03-04 07:03:00' AND time <= '2019-03-05 07:05:00';
> InvalidRequest: Error from server: code=2200 [Invalid query] message="Cannot execute this query as it might involve data filtering and thus may have unpredictable performance. If you want to execute this query despite the performance unpredictability, use ALLOW FILTERING"
* Full Scan
SELECT * from heartrate2 WHERE pet_chip_id = 123e4567-e89b-12d3-a456-426655440b23 AND time>='2019-03-04 07:03:00' AND time <= '2019-03-05 07:05:00' ALLOW FILTERING;
* WHERE on Date
SELECT * from heartrate2 WHERE pet_chip_id = 123e4567-e89b-12d3-a456-426655440b23 AND date = '2019-03-04' AND time>='2019-03-04 07:03:00' AND time <= '2019-03-05 07:05:00';
SELECT * from heartrate2 WHERE pet_chip_id = 123e4567-e89b-12d3-a456-426655440b23 AND date = '2019-03-05' AND time>='2019-03-04 07:03:00' AND time <= '2019-03-05 07:05:00';
* IN
SELECT * from heartrate2 WHERE pet_chip_id = 123e4567-e89b-12d3-a456-426655440b23 AND date IN('2019-03-05', '2019-03-04') AND time>='2019-03-04 07:03:00' AND time <= '2019-03-05 07:05:00';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment