I have a rails application that talks to Oracle.
In average, I have a request that has the following completion times: (extracted from rails production.log)
Completed 200 OK in 85ms (Views: 8.4ms | ActiveRecord: 17.1ms)
Basically, I have a Speed table with:
- gps_id
- speed
- timestamp
=> it stores the speed of a given gps device at a given timestamp.
My ActiveRecord request is something like:
from = DateTime.strptime(params[:from], "%Y-%m-%dT%H:%M:%S%Z")
to = DateTime.strptime(params[:to], "%Y-%m-%dT%H:%M:%S%Z")
@speeds = Speed.where('gps_id = ? and timestamp >= ? and timestamp <= ?', gps.id, from, to).order('id desc')
=> it retrieves all the speed information between 2 timestamps
As the requests was really slow, I have added 2 indexes on the Speed table:
- one index on the gps_id column
- one index on the timestamp column
I guess this is the first basic approach, but how can this be optimized ?
You can add a multi-column index on gps_id and timestamp. That may improve things. But if you are inserting data more often than you query it, it may have negative consequences elsewhere.