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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:27:15+00:00 2026-06-12T11:27:15+00:00

The following query is taking more than 20 seconds to execute on a table

  • 0

The following query is taking more than 20 seconds to execute on a table with around half million rows:

SELECT images.id, images.user_id, images_locale.filename, extension, size, width, height, views, batch, source, status, images.created_at, images.category_id, title, short_description, long_description, alternate, slugs.name as slug, images_locale.slug_id, path_cache AS category_path, full_name, users.username
FROM images
JOIN images_locale ON images_locale.image_id = images.id JOIN slugs ON images_locale.slug_id = slugs.id JOIN categories_locale ON images.category_id = categories_locale.category_id JOIN users ON users.id = images.user_id
WHERE slugs.name = 'THE_SLUG_HERE' AND images.status = '1' AND images_locale.locale_id = 1 AND categories_locale.locale_id = 1
LIMIT 1

Now when I remove slugs.name = 'THE_SLUG_HERE' AND I get the result in a few milliseconds.

This is my slug table:

CREATE TABLE `slugs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(250) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '',
  `type` tinyint(4) NOT NULL,
  `locale_id` smallint(6) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=3611900 DEFAULT CHARSET=utf8;

I tried to CREATE INDEX test_speed ON slugs(name) but it didn’t speed up things.

Please help.

EDIT:

Here are the result of EXPLAIN:

result of <code>EXPLAIN</code>

  • 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-12T11:27:16+00:00Added an answer on June 12, 2026 at 11:27 am

    Move all the conditions you can into the ON clause of the joins:

    SELECT ...
    FROM images
    JOIN images_locale ON images_locale.image_id = images.id
        AND images_locale.locale_id = 1
    JOIN slugs ON images_locale.slug_id = slugs.id
        AND slugs.name = 'THE_SLUG_HERE'
    JOIN categories_locale ON images.category_id = categories_locale.category_id
        AND categories_locale.locale_id = 1
    JOIN users ON users.id = images.user_id
    WHERE images.status = '1' 
    LIMIT 1
    

    The reason this works is that WHERE clauses filter the results of all possible joins, but if you move the conditions into the ON clauses, you avoid joining all following tables for rows you already know are not wanted.

    This can avoid doing millions of unnecessary joins!

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

Sidebar

Related Questions

The following query is taking more than 25 seconds to complete: SELECT c.* ,
Following query is taking really long to execute: SELECT HISTORY.VERSION_ID , HISTORY.ACTION , HISTORY.STATUS
I'm executing the following query SELECT COUNT(*) FROM table WHERE field1='value' AND (field2 >=
By taking reference from here I had made the following query: SELECT INSTITUTE.ID INST_ID,
The following query: select unnest(Table2.L) as X, unnest(Table1.O) from Table1, Table2 where Table1.code =
The following query: SELECT DISTINCT ClassName FROM SiteTree ORDER BY ClassName is returning things
I have the following query: SELECT M.Col7, M.Col8, M.Col9, M.Col10 FROM [MyTable] M WHERE
I ran a query to copy 18 million records from one table to another.
I'm returning results from the following query which is taking too long when running.
I'm using VB.NET. I am performing a select query that returns approximately 2500 rows,

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.