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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:24:39+00:00 2026-06-16T16:24:39+00:00

My query is currently taking roughly 3 seconds, which I’m sure can be optimized.

  • 0

My query is currently taking roughly 3 seconds, which I’m sure can be optimized. I just can’t figure out how to optimize it.

My app has a reasonably big products table (roughly 500,000 records). Each product can be listed on one of 50 domains (listed in a domains table). The links between products and domains are stored in the domains_products table (which has approximately 1,400,000 records). The slow query is in my app’s admin section, where I need to be able to see products that are NOT listed on any domain.

Stripped to the bare bones with all unrelated joins removed, the query in question is:

SELECT    `products`.*
FROM      `products`
LEFT JOIN `domains_products`
ON        `domains_products`.`product_id` = `products`.`id`
WHERE     `products`.`deleted` = 'N'
AND       `domains_products`.`domain_id` IS NULL
ORDER BY  `products`.`id` ASC

In this form, the query takes more than 3 seconds and returns a little over 3,000 products (which is correct). If I remove either WHERE clause, the query takes 0.12 seconds (but obviously does not return the correct results).

Both tables use the InnoDB engine. The products table has a primary key on the id column and an index on the deleted column. The domains_products table only has a product_id and domain_id column, the primary key is on both these columns and they both have their own index. All relevant columns are NOT NULL columns.

EXPLAIN gives me this:

id select_type table            type possible_keys key        key_len ref         rows   Extra
1  SIMPLE      products         ref  deleted       deleted    1       const       188616 Using where
1  SIMPLE      domains_products ref  product_id    product_id 4       products.id 1      Using where; Using index; Not exists

Note that although MySQL has discovered the correct keys, it doesn’t actually seem to be using them.

The profiler says this:

Status               Time
Starting             62 µs
Checking Permissions 7 µs
Checking Permissions 5 µs
Opening Tables       38 µs
System Lock          13 µs
Init                 37 µs
Optimizing           17 µs
Statistics           1,3 ms
Preparing            25 µs
Executing            5 µs
Sorting Result       5 µs
Sending Data         3,3 s
End                  28 µs
Query End            8 µs
Closing Tables       25 µs
Freeing Items        297 µs
Logging Slow Query   4 µs
Cleaning Up          5 µs

Note that it seems to be hanging on Sending Data. I’ve tried replacing the join by a NOT IN:

SELECT `products`.*
FROM   `products`
WHERE  `products`.`deleted` = 'N'
AND    `product`.`id` NOT IN (
    SELECT `product_id`
    FROM   `domains_products`
)
ORDER BY `products`.`id` ASC

This query gives the exact same results, but takes 3.8 seconds.

Can anyone point me in the right direction to optimize this query?

  • 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-06-16T16:24:40+00:00Added an answer on June 16, 2026 at 4:24 pm

    It seems that the problem is with the “deleted” column. I’m guessing that almost all of the items in the products table is marked with “N”, making the index on the “deleted” column pretty useless in this case.

    One thing you can do is create another table, say deleted_domains_products that would store the product_id (and the domain_id if you want). Then you create a trigger so every time an entry was deleted from domains_products, it would insert an entry into that table. Then you’ll have a smaller set to query against. And when you’re done, you can truncate that table for the next time, so it should always be pretty quick.

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

Sidebar

Related Questions

I currently have the current MySQL query taking up to 10 seconds to run
I'm currently running a DELETE query that is taking a lot longer than expected
This query is taking 5 seconds: With cte as ( Select ROW_NUMBER() OVER (Order
We have a (currently InnoDB) table which contains roughly 500,000 rows. This represents a
I have a query that works, but it's taking at least 3 seconds to
I currently have quite a messy query, which joins data from multiple tables involving
I have to optimize this query can some help me fine tune it so
One of the query (given below) is taking 90+ seconds to execute. It returns
Given i have articles which are tagged, one article can have n tags. Currently
Looking to do a bit of refactoring... Using NHibernate I have this query currently

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.