I would like to identify and count the posts that have a specific number of messages and a post.location that is not nil.
I have approximately 100k rows and it is for a statistical use so I need fast queries (I might have to index post.location by the way).
How can I do it the easiest and quickest way?
Here is my schema :
create_table "posts", :force => true do |t|
t.string "ref"
t.string "title"
t.string "author"
t.datetime "created_at"
t.datetime "updated_at"
t.string "location"
t.float "lat"
t.float "long"
end
create_table "messages", :force => true do |t|
t.integer "post_id"
t.integer "status_id"
t.integer "user_id"
t.string "content"
t.datetime "created_at"
t.datetime "updated_at"
t.float "lat"
t.float "long"
end
add_index "messages", ["post_id"], :name => "index_messages_on_post_id"
add_index "messages", ["created_at"], :name => "index_messages_on_created_at"
The SQL Query you are looking for should look a lot like this:
You can then convert that to a ActiveRecord Query or just use
find_by_sql.(I wrote this from memory so you might have to tweak it in some places .. but the general idea should work).