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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:53:58+00:00 2026-05-16T04:53:58+00:00

I have a data model like the following: username | product1 | product2 ——————————-

  • 0

I have a data model like the following:

username | product1 | product2
-------------------------------
 harold     abc        qrs
 harold     abc        def   
 harold     def        abc
 kim        abc        def
 kim        lmn        qrs    
 ...

username | friend_username
---------------------------
 john       harold
 john       kim
 ...

I want to build a histogram of the most frequent product1 to product2 records there are, restricted to a given product1 id, and restricted only to friends of john. So something like:

What do friends of john link to for product1, when product1=’abc’:
Select all of john’s friends from the friends table. For each friend, count and group the number of records where product1 = ‘abc’, sort results in desc order:

Results:
abc -> def (2 instances)
abc -> qrs (1 instance)

I know we can do the following in a relational database, but there will be some threshold where this kind of query will start utilizing a lot of resources. Users might have a large number of friend records (500+). If this query is running 5 times every time a user loads a page, I’m worried I’ll run out of resources quickly.

Is there some other table I can introduce to my model to relieve the overhead of doing the above query everytime users want to see the histogram break down? All I can think of is to precompute the histograms when possible so that reads optimized.

Thanks for any ideas

  • 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-16T04:53:59+00:00Added an answer on May 16, 2026 at 4:53 am

    Here’s your query:

      SELECT p.product2,
             COUNT(p.product2) AS num_product
        FROM PRODUCTS p
        JOIN FRIENDS f ON f.friend_username = p.username
                      AND f.username = 'john'
       WHERE p.product1 = 'abc'
    GROUP BY p.product2
    ORDER BY num_product DESC
    

    To handle 5 products, use:

      SELECT p.product1,
             p.product2,
             COUNT(p.product2) AS num_product
        FROM PRODUCTS p
        JOIN FRIENDS f ON f.friend_username = p.username
                      AND f.username = 'john'
       WHERE p.product1 IN ('abc', 'def', 'ghi', 'jkl', 'mno')
    GROUP BY p.product1, p.product2
    ORDER BY num_product DESC
    

    It’s pretty simple, and the more you can filter the records down, the faster it will run because of being a smaller dataset.

    If this query is running 5 times every time a user loads a page, I’m worried I’ll run out of resources quickly.

    My first question is why you’d run this query more than once per page. If it’s to cover more than one friend, the query I posted can be updated to expose counts for products on a per friend or user basis.

    After that, I’d wonder if the query can be cached at all. How fresh do you really need the data to be – is 2 hours acceptable? How about 6 or 12… We’d all like the data to be instantaneous, but you need to weigh that against performance and make a decision.

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

Sidebar

Related Questions

I have a Core Data model structure like the following: Product <-->> OrderProduct where
I have a data model that includes common columns like addedBy, editedby (user), addedDate,
I have a relationship in a Core Data model that feels like it wants
I have a to-many relationship in my data model, and I'd like to get
I have some configuration data that I'd like to model in code as so:
I have a data model with a many-to-many relationship like EntityA <-->> EntityB <<-->
Let's say I have the following data model, for keeping track of the stats
In a webapp, i have a data-model (let's call it Item). The data of
I have a simple data model of two tables, email and recipients, email can
I have an Entity Data Model that I have created, and its pulling in

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.