In my PHP Laravel application, I want to display special offers to users, depending on the quality of their house(s).
There are 4 Models: User
, House
, Offer
, OfferTargeting
.
- Each
User
can have manyHouses
. - Each
House
record has details likesize
,type
,city
,country
etc. - Each
Offer
records holds the details of the offer liketitle
,description
etc. - Each
OfferTargeting
defines the requirements that aHouse
has to have, in order to show the respective offer to the user.
Currently, I run a comparison each time a user visits the offers page, which takes a while since there are many targeting criteria that need to be considered. The problem is that it takes too long and uses too many resources.
What's the correct way to determine which user should see what offer? I already thought of a background job that runs the comparison hourly and stores the results in a new table, like:
id | user_id | offer_id |
---|---|---|
1 | 156 | 2 |
2 | 156 | 3 |
3 | 157 | 2 |
4 | 158 | 16 |
But I have ~20k users with ~30k houses and each comparison would take at least ~1.5 seconds, so it would be too huge.
What's the correct approach to this? How do companies like Facebook realize their targeting mechanism?