Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1079077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:48:57+00:00 2026-05-16T21:48:57+00:00

Each product a product_date_added which is a Date field contained the date it was

  • 0

Each product a product_date_added which is a Date field contained the date it was added.
They also have a product_views which is an int field containing how many times a product has been viewed.

To display products by popularity, I us an algorithm to calculate how many hits per day a product has.

SELECT 
    AVG(product_views / DATEDIFF(NOW(), product_date_added)) as avg_hits
    , product_table.* 
FROM product_table
WHERE product_available = "yes" 
GROUP BY product_id
ORDER BY avg_hits DESC

This works, but the boss is noticing a lot of older products showing up first. So he basically wants newer views to have more weight than older views.

His suggestion was that any views over a year old don’t count. I think I would have to keep a date of every view in order to do that, which I think would slow down performance.

What is the best way to create a popularity algorithm like what my boss is asking for?

Ideally I would want to be able to come up with something that doesn’t alter the table structure. If that is not possible, I would at least like to come up with a solution that can use the existing data so we are not starting from 0. If thats not possible either than anything that will work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:48:58+00:00Added an answer on May 16, 2026 at 9:48 pm

    You wouldn’t (as such) have to keep the date of every view. You could instead keep up to 366 rows per item in a table with columns: product_id, day_of_year, count. Each day run a task to zero all the counts from one year ago. If you don’t mind denormalised data, that task could also update the “count” field in the item itself for fast retrieval, so that your query does not need to change. product_views just becomes product_views_in_the_last_year. The time period of 1 day is arbitrary – I doubt you care that the popularity is based on a window of precisely 1 year, so I expect it could just as well be an hour, a week or a fortnight, depending how many buckets you’re willing to deal with.

    An alternative scheme might be to use exponential decay. Turn the count field into a decimal type. Once per day, reduce the count of each item by a fixed percentage (less than 1%, more than 0.1%), so that the more recent a hit is, the more “weight” it has. So old popularity never dies entirely, but hits from a year ago won’t contribute much. An equivalent to this scheme, by the way, is to leave the code as it is but ensure that your website as a whole becomes exponentially more popular over time 😉

    As for avoiding starting from zero – maybe reduce each item’s count immediately, as a one-off action, by a proportion which depends on the age of the item. On the whole you’d expect that older objects have older views, and hence are over-rated by the current scheme. That’s not fool-proof, since maybe some older items have recently gained a lot of hits. You might be able to identify these items by looking at recent web server logs, or by spending a week or a month counting hits before doing the one-off reduction. Even without doing that, if there’s a fundamental reason for their popularity (not just because they’re currently rating high in your rankings and hence receiving traffic from people looking at your “most popular” chart), then hopefully they’ll recover given time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a block of product images we received from a customer. Each product
I'm trying to generate a sales reports which lists each product + total sales
Each of my clients can have many todo items and every todo item has
We have to keep 2 product versions. Now, some changes in each version have
I need to store products for an e-commerce solution in a database. Each product
I have some products that belongs to the some category. Each category can have
We have several different optimization algorithms that produce a different result for each run.
A common (i assume?) type of query: I have two tables ('products', 'productsales'). Each
I have two MySQL tables (product and price history) that I would like to
I have two tables: one holding products and one holding related inventory moves. Product

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.