I'm trying to speed up a complex rails query that basically searches for a location on a venue. The location can be ether a name, city, county or country.
I tried adding an index, however I don't think i have done this correctly.
This is my schema for venues:
create_table "venues", force: :cascade do |t|
t.string "name"
t.string "address"
t.string "postcode"
t.string "city"
t.string "country"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "postplace"
t.string "map_address"
t.decimal "latitude"
t.decimal "longitude"
t.string "slug"
t.text "description"
t.string "website"
t.integer "import_id"
t.string "postal_address"
t.string "email"
t.string "county"
t.string "region"
t.string "country_name"
end
add_index "venues", ["country_name"], name: "index_venues_on_country_name", using: :btree
add_index "venues", ["import_id"], name: "index_venues_on_import_id", using: :btree
add_index "venues", ["name", "city", "county", "country"], name: "location", using: :btree
add_index "venues", ["slug"], name: "index_venues_on_slug", unique: true, using: :btree
And this is the relevant part of my query:
def filter_on_location
@events = events.
joins(:venue).where(
"venues.city ILIKE :location
OR venues.county ILIKE :location
OR venues.country ILIKE :location
OR venues.name = :location",
location: params[:location])
end
Any tips are appreciated!