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

The Archive Base Latest Questions

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

This query is running very slow; it used to be 3 separate queries but

  • 0

This query is running very slow; it used to be 3 separate queries but I’ve consolidated it into 1. I assume it’s slow because of the subselects it’s doing, but I assume there might be a better way to query for this info.

* Background:*

users is just user accounts.
posts is all the post content.
posts_int is just 2 fields, postid and intid. This stores interest ID’s for posts.
int_usr is also just 2 fields, userid and intid. This stores interest ID’s for users.

All of the “intid” field names are based on another table that simply has ids and names for each interest. This query returns all posts for this user that are relevant to their selected interests, stored in integers in int_usr.

The query:

SELECT DISTINCT 
users.first_name,
users.last_name,
posts.id,
posts.userid,
posts.comments,
posts.date,
posts.comment 
FROM posts 
LEFT JOIN users ON 
posts.userid = users.id 
LEFT JOIN posts_int ON 
posts.id = posts_int.postid 
WHERE 
posts_int.intid IN (
SELECT DISTINCT 
  intid 
FROM int_usr 
WHERE 
  intid IN (
      SELECT DISTINCT 
      intid 
       FROM int_usr 
       WHERE 
      userid = '1' 
  ) 
ORDER BY intid ASC 
)
AND posts.deleted = '0' 
AND users.deleted = '0' 
ORDER BY 
posts.id DESC 
LIMIT 0,12

Any help is appreciated!

Ryan

EDIT: Here is the explain you requested:

* EXPLAIN SQL: *

+----+--------------------+-----------+--------+----------------+---------+---------+---------------------------+------+----------------------------------------------+
| id | select_type        | table     | type   | possible_keys  | key     | key_len | ref                       | rows | Extra                                        |
+----+--------------------+-----------+--------+----------------+---------+---------+---------------------------+------+----------------------------------------------+
|  1 | PRIMARY            | posts_int | ALL    | postid         | NULL    | NULL    | NULL                      |   87 | Using where; Using temporary; Using filesort |
|  1 | PRIMARY            | posts     | eq_ref | PRIMARY,userid | PRIMARY | 4       | bunchify.posts_int.postid |    1 | Using where                                  |
|  1 | PRIMARY            | users     | eq_ref | PRIMARY        | PRIMARY | 4       | bunchify.posts.userid     |    1 | Using where                                  |
|  2 | DEPENDENT SUBQUERY | int_usr   | ALL    | NULL           | NULL    | NULL    | NULL                      |  288 | Using where; Using temporary                 |
|  3 | DEPENDENT SUBQUERY | int_usr   | ref    | userid         | userid  | 4       | const                     |   19 | Using where; Using temporary                 |
+----+--------------------+-----------+--------+----------------+---------+---------+---------------------------+------+----------------------------------------------+
5 rows in set (0.00 sec)
  • 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-25T13:57:06+00:00Added an answer on May 25, 2026 at 1:57 pm

    You can simplify things a bit by getting rid of the extra level of nesting in

    posts_int.intid IN (
    SELECT DISTINCT 
      intid 
    FROM int_usr 
    WHERE 
      intid IN (
          SELECT DISTINCT 
          intid 
           FROM int_usr 
           WHERE 
          userid = '1' 
      ) 
    ORDER BY intid ASC 
    )
    

    and then changing the left joins to inner joins because you’re checking the right tables in your where clause anyways.


    Something like

    SELECT DISTINCT <blah>
      FROM posts 
      JOIN users 
           ON posts.userid = users.id 
      JOIN posts_int 
           ON posts.id = posts_int.postid 
      JOIN int_usr 
           on posts_int.intid = int_usr.intid and int_usr.userid = '1'
     where posts.deleted = '0' 
       AND users.deleted = '0' 
    ORDER BY posts.id DESC 
    LIMIT 0,12
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My application keeps running into Timeout Expired SqlExceptions. The thing is that this query
I've a semi complex LINQ query that is running very slow. I do not
I'm trying to implement slow query logging into my database class, but I'm getting
When running a very simple query in SQL Server 2000. SELECT getDate() Most queries
I'm running this query: SELECT TOP 1 [DVD Copy].[Stock No] FROM [DVD Copy] WHERE
Im running this query on the same server as the web application, so SPQuery.ExpandRecurrence
I have problems running a dynamic LIKE statement in my project: this query works
I'm running Mysql 5.0.77 and I'm pretty sure this query should work? SELECT *
I got this error running a query that goes against 2 tables with combined
I have very simple query like this: SELECT * FROM `all_conversations` WHERE `deleted_1` !=

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.