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

The Archive Base Latest Questions

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

SELECT b.categoryid, SUM(viewcount) AS cnt, categoryname FROM bookvisit AS bv INNER JOIN book AS

  • 0
SELECT
    b.categoryid,
    SUM(viewcount) AS cnt,
    categoryname
FROM
    bookvisit AS bv
    INNER JOIN book AS b ON b.isbn = bv.isbn
    LEFT JOIN category AS c ON b.categoryid = c.categoryid
WHERE
    b.categoryid IS NOT NULL AND
    b.categoryid <> 0 
GROUP BY
    b.categoryid 
ORDER BY
    cnt DESC,
    bv.isbn
LIMIT 0, 4

I have three tables –
book (contains books information)
bookvisit (book visit info)
category (category master )

What I need is popular categories, above query is fine with two eq_ref but it has a
Using temporary; Using filesort also

any help ?

  • 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:05:54+00:00Added an answer on May 16, 2026 at 9:05 pm

    As @dj_segfault said, you cannot have an index on an aggregate column in MySQL and you will have to write a service that would cache the SUMs in a shapshot table (which you can index).

    Here’s how you can do it and still have the accurate statistics:

    1. Create a snapshot table:

      category  cnt
      

      with a PRIMARY KEY on category.

    2. Create a single-field, single-record table called snapshot_time:

      taken
      
    3. On a timely basis, fill this table with the query:

      UPDATE  snapshot_time
      SET     taken = NOW()
      
      INSERT  
      INTO    snapshot
      SELECT  b.category, COUNT(*) AS new_cnt,
              (
              SELECT  taken
              FROM    snapshot_time
              ) AS new_taken
      FROM    bookvisit bv
      JOIN    book b
      ON      b.isbn = bv.isbn
      WHERE   bv.visit_time <=
              (
              SELECT  taken
              FROM    snapshot_time
              )
      ON DUPLICATE KEY UPDATE
      SET     cnt = new_cnt,
              snapshot_taken = new_taken
      
    4. Create the following indexes:

      snapshot (cnt)
      bookvisit (visit_time)
      book (category)
      
    5. Run this query:

      SELECT  category,
              cnt +
              (
              SELECT  COUNT(*)
              FROM    bookvisit bv
              JOIN    book b
              ON      b.isbn = bv.isbn
              WHERE   bv.visit_time > 
                      (
                      SELECT  taken
                      FROM    shapshot_time
                      )
                      AND b.category = s.category
              ) AS total
      FROM    snapshot
      WHERE   cnt >=
              (
              SELECT  cnt
              FROM    snapshot
              ORDER BY
                      cnt DESC
              LIMIT 4
              )
              -
              (
              SELECT  COUNT(*)
              FROM    bookvisit
              WHERE   bv.visit_time > 
                      (
                      SELECT  taken
                      FROM    shapshot_time
                      )
              )
      ORDER BY
              total DESC
      LIMIT 4
      

    The query will return you accurate visit count.

    The main idea is that you need to scan only the records in bookvisit that were collected after the statistics were cached.

    More than that: you don’t even have to scan all records in the cached statistics. Since the number of visits only grows, you can only scan the results that can possibly get into the first four.

    If the 4th record has 1,000,000 page views in the snapshot, and 1,000 page views happened after you took the snapshot, you can only select the records from the snapshot with cnt >= 999,000. The other records could not theoretically hit this limit, since it would take more than 1K page views.

    The only problem is that you can delete the books or change their categories. In this case you would just need to recalculate the statistics or fall back to your original method.

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

Sidebar

Related Questions

SQL query: SELECT ArticleCategories.Title AS Category, Articles.Title, Articles.[Content], Articles.Date FROM ArticleCategories INNER JOIN Articles
I have this SELECT * FROM categoryTable WHERE categoryId NOT IN ( SELECT categoryId
My code contain SELECT * FROM newchap WHERE company LIKE '%$company%' OR Category LIKE
What is a scalable way to select latest 10 items from each category. I
I have a following HQL query: SELECT s.id FROM stack s WHERE s.category is
I have a query as follows. select strftime('%Y-%m',A.traDate) as Month,sum(A.TraAmt) as Total,C.GroupType from TransactionTbl
This is my SQL code SELECT p.plant_name, sum(data.value_1) totalvalue_1 FROM (SELECT DISTINCT date_format(date, '%Y-%m')
Using LINQ to SQL db.Products.Where(c => c.ID == 1).Skip(1).Take(1).ToList(); executes SELECT [t1].[ID], [t1].[CategoryID], [t1].[Name],
I have a select input: f.select :category_id, nested_set_options(Category, @categories) {|i| #{'-' * i.level} #{i.name}
SELECT PLD_LINK.ID, PLD_LINK.TITLE, PLD_LINK.URL, PLD_CATEGORY.TITLE, TLL_SORT_STATUS.status, PLD_LINK_COMMENT.DATE_ADDED FROM PLD_LINK, PLD_CATEGORY, TLL_SORT_STATUS, PLD_LINK_COMMENT WHERE PLD_LINK.CATEGORY_ID

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.