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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:22:07+00:00 2026-06-15T15:22:07+00:00

I have a long query that is zapping resources and need to rewrite it.

  • 0

I have a long query that is zapping resources and need to rewrite it.
The obvious problem with it is the use of “not in” in the where clause.
My initial thought is to rewrite it doing away with all of the self joins and doing a “not exists” on a subquery. Any thoughts on doing that, or a maybe a more efficient idea than that one?

Here’s the query:

SELECT a.referenceid,
       a.memberid AS d1,
       b.memberid AS d2,
       c.memberid AS d3,
       d.memberid AS d4,
       e.memberid AS d5,
       f.memberid AS d6
FROM   jos_comprofiler_members AS a FORCE INDEX (aprm)
       LEFT JOIN jos_comprofiler_members AS b FORCE INDEX (aprm)
              ON a.memberid = b.referenceid
                 AND b.accepted = 1
                 AND b.pending = 0
       LEFT JOIN jos_comprofiler_members AS c FORCE INDEX (aprm)
              ON b.memberid = c.referenceid
                 AND c.accepted = 1
                 AND c.pending = 0
       LEFT JOIN jos_comprofiler_members AS d FORCE INDEX (pamr)
              ON c.memberid = d.referenceid
                 AND d.accepted = 1
                 AND d.pending = 0
       LEFT JOIN jos_comprofiler_members AS e FORCE INDEX (pamr)
              ON d.memberid = e.referenceid
                 AND e.accepted = 1
                 AND e.pending = 0
       LEFT JOIN jos_comprofiler_members AS f FORCE INDEX (pamr)
              ON e.memberid = f.referenceid
                 AND f.accepted = 1
                 AND f.pending = 0
WHERE  a.referenceid = 1593
       AND a.accepted = 1
       AND a.pending = 0
       AND f.memberid = 1593
       AND b.memberid NOT IN ( 1593, a.memberid )
       AND c.memberid NOT IN ( 1593, a.memberid, b.memberid )
       AND d.memberid NOT IN ( 1593, a.memberid, b.memberid, c.memberid )
       AND e.memberid NOT IN (
           1593, a.memberid, b.memberid, c.memberid, d.memberid )
       AND f.memberid NOT IN ( 1593, a.memberid, b.memberid, c.memberid,
                               d.memberid, e.memberid )
LIMIT  0, 1  
  • 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-15T15:22:09+00:00Added an answer on June 15, 2026 at 3:22 pm

    Your query is looking for all members within a distance 6 of 1593. Rhetorical question: Is this related to six degrees of separation?

    Normally, finding such lists in SQL requires recursive queries or some non-SELECT construct (loops, cursors). I am not sure how MySQL handles the not in, when the list consists of variable members. However, I suspect that it is doing some sort of nested loop join for each one.

    Suggestion: move the not in statements to the on clause. For example:

       LEFT JOIN jos_comprofiler_members AS c FORCE INDEX (aprm)
              ON b.memberid = c.referenceid
                 AND c.accepted = 1
                 AND c.pending = 0
                 AND (c.memberId <> b.MemberId an c.MemberId <> a.MemberId and c.MemberId <> 1593)
    

    This could improve things. Or make them worse. But it is an idea.

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

Sidebar

Related Questions

I have a pretty long SQL query that I use to, for all of
I have a reasonably long SQL query that I need to run in a
I have a long and very repetitive query that I'm going to need to
I have a query that takes too long to execute. I need to know
I have a long-running SQL Server 2005 query that I have been hoping to
I have a long LinqtoSQl query in which several parameters I'm not forcing the
I have a query that takes too long to respond when the search parameter
I have a MongoDB query that's taking an unreasonably long time to run, but
I have this quite long query that should give me some information about shipments,
So I have a big long query string that can either be ... //url=z&surl=y&time=z&codec=a264&acodec=mp3&width=400x100

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.