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 6341365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:01:10+00:00 2026-05-24T20:01:10+00:00

Imagine this scenario: I have three tables in my database: Products , Users and

  • 0

Imagine this scenario:

I have three tables in my database: Products, Users and Likes, the latter representing a relation between a Product and a User. Now I have a query, which joins Products with the Likes table, counting how many Likes a Product got.

In fact, I need the information about the count much more often than the actual users and I want to use the query above as part of a greater query in a view. Is it possible to optimize the query or the view so that MySQL somehow caches the result of the count query above?

  • 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-24T20:01:10+00:00Added an answer on May 24, 2026 at 8:01 pm

    I don’t know of a way to do that with mysql (but I mainly use postgres, so it might be possible and I just don’t know). I would suggest two options:

    • If you need the cache to be as up-to-date as possible, add an likes_count column to your Products table, create AFTER INSERT and AFTER DELETE triggers on the Likes table (assuming you never UPDATE that table) that increases the like count of a product when a new row is inserted to the Likes table and decreases it when a row is deleted. Than, add a cronjob that executes UPDATE Products SET likes_count=(SELECT COUNT(1) FROM Likes WHERE product_id = Products.id) once in a while to make sure the values are really up-to-date – maintaining the counts with triggers is never 100% accurate.
    • Another option is create a view as CREATE VIEW Products_Likes_View AS SELECT product_id, COUNT(*) AS likes_count FROM Likes GROUP BY product_id;, and create a cache table, something like CREATE TABLE Products_Likes_Cache (product_id INTEGER PRIMARY KEY, likes_count INTEGER NOT NULL);. Than, add a cronjob that executes BEGIN; TRUNCATE Products_Likes_Cache; INSERT INTO Products_Likes_Cache SELECT * FROM Products_Likes_View; COMMIT; which’ll sync the cache table with the up-to-date information from the view. Than, if you need accurate results, you can get the data directly from the view. Otherwise, use the cache table.

    If you go with the first option, the triggers should look something like this (my MySQL skills are a bit rusty, I might be off with the exact syntax):

    CREATE TRIGGER product_increase_likes AFTER INSERT ON Likes
        FOR EACH ROW BEGIN
            UPDATE Products SET likes_count=likes_count+1 WHERE id=NEW.product_id
        END;
    
    CREATE TRIGGER product_decrease_likes AFTER DELETE ON Likes
        FOR EACH ROW BEGIN
            UPDATE Products SET likes_count=likes_count-1 WHERE id=OLD.product_id
        END;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine this scenario: You have a desktop and a laptop. The desktop has a
I imagine this has a rather simple answer <% for user in @users %>
Please imagine this small database... Diagram removed dead ImageShack link - volunteer database diagram
In this simplified scenario, I have a model called Post Each post has a
So imagine this scenario: 10.00: your app pushes a message Hello 10.01: your app
Hi I was wondering if this is possible, this is my scenario, I have
Imagine this scenario: var ass1 = Assembly.LoadFrom(@C:\Work\3.59\assembly1.dll); var ass2 = Assembly.LoadFrom(@C:\Work\3.60\assembly1.dll); Debug.Assert(ass1 != ass2);
Imagine this subroutine: sub test(&&) { my $cr1 = shift; my $cr2 = shift;
Imagine this simple form <form action=<?php echo $_SERVER['REQUEST_URI']; ?> method=post> <fieldset> <legend>Contact Me</legend> <label
Imagine this as the code from build.xml: <project name=test project> <target name=first> <echo>first</echo> </target>

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.