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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:41:52+00:00 2026-05-18T12:41:52+00:00

I have this rather simple SQL query, but it takes almost a minute to

  • 0

I have this rather simple SQL query, but it takes almost a minute to execute:

SELECT
 i.id,
 ...,
 a.id AS albums_id,
 ...,
 u.id AS users_id,
 ...
FROM
 images i
  LEFT JOIN albums a ON i.albums_id = a.id
  LEFT JOIN users u ON a.users_id = u.id
WHERE
 a.access = 'public'
 AND i.num_of_views > 0
ORDER BY
 i.num_of_views DESC
LIMIT
 0, 60

Result of EXPLAIN for the above query:

http://i.min.us/ibeAnM.png

Tables involved:

images (~4,822,000 rows), albums (~149,000 rows), users (~43,000 rows)

Relevant indexes:

albums: access(access,num_of_images,album_time), access_2(access,num_of_images,num_of_all_comments,album_time), users_id(users_id,album_time)

images: browser_2(num_of_views), albums_id(albums_id,image_order)

All tabels are InnoDB, running on MySql v5.1.47

So how do I bring this down to under a second?

Please leave a comment if you need any additional info.

edit: users table can be joined either with albums or images does not matter to me.

edit2: moving a.access = 'public' from WHERE to JOIN does indeed solve my problem, but the results returned are not correct (I get images from albums that are not public), putting the a.access ... in both WHERE and JOIN slows the query down even more than before.

  • 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-18T12:41:53+00:00Added an answer on May 18, 2026 at 12:41 pm

    I believe there’s a little confusion going on here w/r/t the impact that a filter can have on a LEFT JOIN vs an INNER JOIN.

    Jan, if what you are trying to ask in your query is “Get all images for all albums that are public, and get the users of those albums as well” then you do not want a left join, you want an inner join. A left join will return all images for all albums, but it will also return all images that have no matching album. You can add “and a.id IS NOT NULL” but that’s the same as an INNER JOIN.

    I believe what you want is the following:

    SELECT
     i.id,
     ...,
     a.id AS albums_id,
     ...,
     u.id AS users_id,
     ...
    FROM images i
      INNER JOIN albums a ON i.albums_id = a.id AND a.access = 'public'
      INNER JOIN users u ON a.users_id = u.id
    WHERE i.num_of_views > 0
    ORDER BY i.num_of_views DESC
    LIMIT 0, 60
    

    If you left join albums to users you could return all albums that don’t have users. Not sure which one you want.

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

Sidebar

Related Questions

Using SQL Server 2008R2, I have here is a simple query: SELECT * FROM
I have a rather simple PL/SQL query (...) DECODE(upper(Table.Column), 'SOMETHING', '---', TABLE2.COLUMN2) as VALUE
I have a rather simple web-service which exposes data from sql server. It will
I will try to keep this as simple as possible. I have a rather
I'm rather new to WPF, so maybe this is a simple question. I have
This may be rather noobish but I'm gonna ask anyhow. I have a class
I know I could solve this with a header but I'd rather not have
This may be a silly question, but right now I have a rather large
I know LINQ doesn't have the SQL IN clause but rather uses contains. Can
I have a very simple SQL query that I made which retrieves some user

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.