How can i sort Cassandra-cql Results by timestamp? - ruby-on-rails

Hi how can i sort my entries by the timestamp of the columns?
def self.get_users
##db.execute("SELECT * FROM users")
end

Do you mean timestamp of the values, or is your column key some kind of timestamp?
CQL doesn't support sort - it returns data in the order it is stored on disk. You tell Cassandra how you want you columns sorted by specifying the comparator_type on your Column Family. In this case, you might consider using a composite column keys of [timestamp, column name].

Related

How do I optimise an ActiveRecord PostgreSQL query and index based on the absence of a jsonb attribute key?

I have a complex SQL query within my model that returns results based on a jsonb attribute key called user_removed. Specifically, I need all records if this is either does not exist, or if its set to false.
Currently in the WHERE part of my query I have the following:
deleted_at IS NULL AND
document->>'user_removed' IS NULL OR
document->>'user_removed' = 'false'",
The corresponding indexes I've created are:
"CREATE INDEX user_removed_false_index ON db.table USING BTREE ((document->>'user_removed')) WHERE document->>'user_removed' = 'false';"
"CREATE INDEX user_removed_is_null_index ON db.table USING BTREE ((document->>'user_removed')) WHERE document->>'user_removed' IS NULL;"
Some research has led me to think that a GIN index would be preferred for the presence of a top level key (https://www.postgresql.org/docs/13/functions-json.html) via:
'{"a":1, "b":2}'::jsonb ? 'b' → t
However, my question is would this work in my use case where I'm specifically looking for the inverse of this (i.e. the absence of a key)? I may be misunderstanding indexes (and please correct me if I'm wrong), but will the creation of the presence of a key index in this case be useful, or do I need to be explicit with my indexes?

JSONB Querying array

I have the following a column in a PostgreSQL table that has a JSONB field with data like:
[{"id":33,"url":"","name":"test"}, {"id":45,"url":"","name":"test"}]
I'm trying to query it and return the element that matches the id. I have the following query in Rails, but it doesn't seem to be matching:
Book.where('data #> ?', '[{"id": 33}]')
Since you only need some elements in the jsonb array, you will need to transform those into records and cross join them with their parents:
Book.joins(", jsonb_array_elements(books.data) obj")
.where("obj->>'id' = '33'")
.select("obj")

Return activerecord results for jsonb column array count greater than given value

In my rails 5 application with a Postgresql database, there is a class Listing with a jsonb column media.
I want to return the records that have array length greater than 10 in the media column.
How would I construct that query in ActiveRecord?
Turns out it is very straightforward
Listing.where('JSONB_ARRAY_LENGTH(media) > 10')

Are indexed columns with NULL values ignored by the cost-based optimizer?

I have a voter table with ~640M rows that has a DateOfDeath DATE column which is indexed.
~410M of the rows have NULL values in DateOfDeath column because these voters are still alive.
If I do a query to select all rows with DateOfDeath between "01012015" and "12312015", will the optimizer bypass all rows that have NULL values in DateOfDeath, without having to do a full table scan?
It will depend on the query, but the query would probably use the index on DateOfDeath and would certainly not need to scan the range of the index where the DateOfDeath is NULL in order to answer that query. You'd need to be sure you've run UPDATE STATISTICS recently if your version of Informix is older than about 11.50.

Faster Active Record Query for counting a group?

is there a more optimized way to count my records that have been grouped by a column? I just want to count all the records; i dont know why it appears i need to add a select statement before my group method.
User.select('username').group('username').to_a.count
The following will return a hash with username as the key and the count as the value:
User.group('username').count

Resources