Adding indexes and speeding up doorlog query

This commit is contained in:
Will Bradley 2014-11-20 12:56:28 -07:00
parent 98b9db5fe1
commit d1c5dba667
3 changed files with 11 additions and 2 deletions

View File

@ -87,7 +87,7 @@ class DoorLog < ActiveRecord::Base
end end
def self.show_status def self.show_status
door_logs = DoorLog.where(key: ["door_1_locked","door_2_locked"]).order('created_at DESC').take(2) door_logs = DoorLog.order('created_at DESC').where(key: ["door_1_locked","door_2_locked"]).limit(2)
door_1_locked = parse_locked_status(door_logs, "door_1_locked") door_1_locked = parse_locked_status(door_logs, "door_1_locked")
door_2_locked = parse_locked_status(door_logs, "door_2_locked") door_2_locked = parse_locked_status(door_logs, "door_2_locked")

View File

@ -0,0 +1,6 @@
class AddIndexesToDoorLog < ActiveRecord::Migration
def change
add_index :door_logs, :key
add_index :door_logs, :created_at
end
end

View File

@ -11,7 +11,7 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20140411024809) do ActiveRecord::Schema.define(:version => 20141120194112) do
create_table "cards", :force => true do |t| create_table "cards", :force => true do |t|
t.string "card_number" t.string "card_number"
@ -52,6 +52,9 @@ ActiveRecord::Schema.define(:version => 20140411024809) do
t.datetime "updated_at", :null => false t.datetime "updated_at", :null => false
end end
add_index "door_logs", ["created_at"], :name => "index_door_logs_on_created_at"
add_index "door_logs", ["key"], :name => "index_door_logs_on_key"
create_table "ipns", :force => true do |t| create_table "ipns", :force => true do |t|
t.integer "payment_id" t.integer "payment_id"
t.text "data" t.text "data"