I’m working on a web application written on php. I have some objects (represented as rows) in mysql table. And I need to show them randomly during a day.
How can I limit the show count of a particular object, e.g not more than 10 times for an hour?
By the show count I mean how many times the object was rendered.
For example, there are 100 images and with each pageview random 5 are shown. I need to normalize the image shows distribution, by limiting images’ show count for an hour, for preventing 1000 shows for one image and 3 to another.
Hope its useful explanation.
Probably the simplest way to do it would be to add a field called
last_shownto your table and then exclude it from the candidate list if it’s been shown within the hour. eg something along these lines:SELECT id FROM my_objects WHERE last_shown < DATE_SUB(NOW(), INTERVAL 1 HOUR) ORDER BY RAND() LIMIT 1Then when you display that actual object, timestamp the column, ie:
UPDATE my_objects SET last_shown = NOW() WHERE id = <the_id_you_displayed>This approach is simpler, but just as effective. If you reduced the timeframe to once every 6 minutes, it would effectively be similar logic to ’10 times within the hour’, and not require an entire new reference table.