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

  • Home
  • SEARCH
  • 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 6645593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:18:24+00:00 2026-05-26T00:18:24+00:00

Ok, I have a query that I just can’t seem to get optimized. Maybe

  • 0

Ok, I have a query that I just can’t seem to get optimized. Maybe I am doing too much in MySQL and should delegate more to PHP. The query as it is takes about a minute, but I really need it to be much faster than that. Below is a hypothetical structure that should give you an idea of what I am trying to accomplish.

You will notice that I am supplying the query with a PHP variable, $bookList, which contains a list of book_id’s owned by the current user in a comma delimited format.

For this example, assume that the user has 70 book_id’s assigned to his library ($bookList) and he wants to find out which stores share the most books in common with his library. Each store has 70 books, but there are 200,000+ stores. Stores with less than 50% in common will be filtered out.

Table 1: my_books

id: mediumint
book_id: smallint

Table 2: store_books

store_id: mediumint
book_id: smallint
index: store_id
index: book_id

Table 3; stores

id: mediumint
name: varchar(50)
primary: id

Query:

SELECT count(s.book_id) AS commonBooks, s.id
                 FROM store_books AS sb
                 INNER JOIN stores AS s ON s.id = sb.id
                 WHERE sb.book_id IN ($bookList) 
                 GROUP BY sb.store_id 
                 HAVING commonBooks > 35
                 ORDER BY commonBooks

Thanks in advance!

  • 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-26T00:18:25+00:00Added an answer on May 26, 2026 at 12:18 am

    Borrowing from @Joe Stefanelli’s answer, use the temp table.
    Make sure you have the primary index on bl.book_id

    BTW: You seem to have an error in your query:

    SELECT count(*) AS commonBooks, s.id
    FROM store_books AS sb
    INNER JOIN stores AS s ON s.id = sb.store_id  -- <<sb.store_id, not sb.id
    WHERE sb.book_id IN (SELECT bl.book_id FROM tempBookList)
    GROUP BY sb.store_id 
    HAVING commonBooks > 35
    ORDER BY commonBooks DESC
    

    It’s a little known fact that MySQL can only use one index per (sub)select.
    Make sure you have a composite index in table store_book
    Also count(*) is (sometimes) faster than count(afield) (and never slower)

    Table 2: store_books
    
    store_id: mediumint
    book_id: smallint
    index: (store_id, book_id)  <<-- composite primary index.
    index: (book_id)
    

    This query will run faster in InnoDB than MyISAM because InnoDB can use covering indexes to resolve this query and will not need to read the data in the actual tables.

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

Sidebar

Related Questions

I have a query, or batch of queries, that I just can't get to
I have written a SQL query that works just fine, but am having a
I have a Linq to SQL query that was working just fine with SQL
I have a feeling that this query is pretty easy to construct, I just
I am using in C# MYsql .I have query that works if I run
Let's say I have a query that is run all 15min. I just need
I have a MySQL query that I'd like help optimizing. Its rather simple in
I have a query that JOINs some tables to get a list of products
below I have a query that will get the most common user agents for
I have a MySQL query that produces this output ordered like this: Instead I'd

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.