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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:06:25+00:00 2026-05-13T08:06:25+00:00

I am trying to use the following query on a table with ~200k records

  • 0

I am trying to use the following query on a table with ~200k records in it. There are all sorts of other fields that can be filtered by, but this is a base example.

SELECT b.isbn FROM books b 
WHERE 
b.price IS NOT NULL AND 
b.deleted = '' AND 
b.publication_date <= '2009-12-04' AND 
(
  b.subject1_id IN ('CAT1','CAT2','CAT3','CAT4','CAT5') OR
  b.subject2_id IN ('CAT1','CAT2','CAT3','CAT4','CAT5') OR
  b.subject3_id IN ('CAT1','CAT2','CAT3','CAT4','CAT5')
)

Currently, I have a separate index on all of these fields and this query takes ~4.5 seconds, which is way too long. EXPLAIN lists NULL under key.

I also tried creating one large index that included all of the fields in the above query, but EXPLAIN shows that this multi-field index is not used.

How can I index these fields to speed up my queries?

EDIT: Here are my current indices (none of which seem to be used by the query):

  • index(price)
  • index(deleted)
  • index(publication_date)
  • index(subject1_id)
  • index(subject2_id)
  • index(subject3_id)
  • index(price, deleted, publication_date, subject1_id, subject2_id, subject3_id)

EDIT2: Per ʞɔıu’s answer – after normalizing the tables and using basically his query, it does speed it up some (time is ~3.5 seconds now), but not as much as I’m looking for. I indexed the new table as PRIMARY KEY(isbn, subject_id) and this index is being used for the join.

EDIT3: I added an additional index on the second table (subject_id, isbn), which helps. The addition of the other index that ʞɔıu mentions below helps a small bit, but only gets used when I use “FORCE INDEX” on the query. It’s down to about 1.5 seconds now. Is there hope of getting it much lower?

  • 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-13T08:06:25+00:00Added an answer on May 13, 2026 at 8:06 am

    You need to normalize your schema before indexing will be of any help in this case.

    You can create another table that contains (subject, isbn), add indexes on book and subject, then join to that table, like:

    select b.isbn from books b
    inner join book_subject bs on bs.isbn=b.isbn
    where 
        b.price is not null and b.deleted != 'DELETED'
        AND b.publication_date <= '2009-12-04' 
        AND bs.subject in ('CAT1', 'CAT2'...)
    

    Rule #1 (literally) in schema normalization is: “no repeating groups“.
    Having the OR operation in the where clause across the 3 subject columns is going to prevent you from being able to take advantage of an index for that part of the query.

    (updated to reflect that isbn is the primary key)

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

Sidebar

Related Questions

I am trying to use the following query with my table: SELECT ARTICLE_NO, USERNAME,
I am trying to query a Azure Table Storage. For that I use the
I'm trying to use the following code and it still strips out all the
I'm trying to use the following code to get all trace data for the
I'm trying to use the following query syntax from a php file: $sql =
I am trying to create a products table running the following SQL query: CREATE
I am trying following query to show all of my user at user page...
im trying to update my table using the following query... $query = mysql_query(UPDATE `outgoings`
Trying to use the following demo example, using the 80px x 80px snap to
I am trying to use the following jcache-ehcache library to as a wrapper so

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.