I have an object called listings, and a listing can have a number of amenities (compare this to post, tags model). It's a sort of has/doesn't have relationship.
So I have an amenities
table which stores what amenities there can be in the system, there is the listings
table that stores listings, listing_amenities
which stores amenities.
The problem is searching listings on the basis of amenities
I can search listing on the basis of any of the parameters of listing(rent, location, etc), but not amenities since that requires a 2 table join. I haven't really been able to come up with an efficient solution to do this. So, I'd like a little help on how to search on this basis.
One solution I could think of was to inject an extra field with group concat of amenities
SELECT *
FROM(SELECT listings.*, GROUP_CONCAT(CONCAT("(",Concat(amenity_id,")"))) as amenities
FROM `listings`,`listing_amenities`
WHERE listings.id=listing_amenities.listing_id
GROUP BY listings.id) as rs
WHERE amenities like "%(1)%"
i.e concatenate and add amenity ids wrapped in () and search on them
which gets me to thinking: amenities are only used in context of listing and for searching so why not store them in a similar format (1), (2) to indicate available amenities in a seperate column in the listings table and save on a table and extra query cost? What are the disadvantages of this approach, if any?
For an easier understanding of table
Listing Table
--------
id title description rent
Amenities
-------------
id name description
Listing_Amenities
------------------
id listing_id amenity_id
I am thinking I could delete the third table
and modify listing
Listing Table
--------
id title description rent amenties(the amenities available in the appartment wrapped in bracket)
like
1 "House in Downtown Discworld" "Nice house, running water, teleporter" 2000 "(1)(5)(7)"
This kind of allows me to search by amenities without needing a join. I can run the same query where I select rent
and search with the where clause: where amenities like "%(1)%" AND amenities like "%(2)%"
The only problem I see in this approach is a deletion anomaly i.e if I delete an amenity from system, it won't delete amenity from listings automatically
but I see this as an unlikely occurence since amenities are decided by the admin and will be pretty much frozen before site launch and secondly, while we might add new amenties, its unlikely we'll delete any.
Are there any other disadvantages. Can you please highlight specific scenarios. I see this as more efficient than a join. And frankly easier to wrap my head around. I could be wrong