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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:37:45+00:00 2026-05-23T00:37:45+00:00

I know that I have to avoid to use IN clause, but I don’t

  • 0

I know that I have to avoid to use “IN” clause, but I don’t know how to do in this case. The scenario is this: from PHP I get a list of values and then I construct a query like this:

SELECT id FROM
    ( SELECT * FROM `tips` r 
      LEFT JOIN (SELECT tip FROM `tips_showed` WHERE user='8') AS a ON r.id=a.tip
      WHERE a.tip IS NULL ) AS t
WHERE t.id IN
    ('3','4','5','2','6','7','8','9','10','18',
     '11','12','13','14','15','16','17','20') LIMIT 1

This makes my site very slow (because it is executed each time a user visits it). How can make it faster?

  • 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-23T00:37:46+00:00Added an answer on May 23, 2026 at 12:37 am

    I believe you can simplify the query to:

    SELECT t.id
        FROM `tips` t
            LEFT JOIN `tips_showed` a
                ON t.id = a.tip
                    AND a.user = '8'
        WHERE a.tip IS NULL
            AND t.id IN ('3','4','5','2','6','7','8','9','10','18',
                         '11','12','13','14','15','16','17','20')
        LIMIT 1
    

    Using NOT EXISTS instead of the LEFT JOIN may also buy you some additional performace. Try each and compare.

     SELECT t.id
         FROM `tips` t
         WHERE t.id IN ('3','4','5','2','6','7','8','9','10','18',
                        '11','12','13','14','15','16','17','20')
            AND NOT EXISTS(SELECT NULL 
                               FROM `tips_showed` a 
                               WHERE a.tip = t.id
                                   AND a.user = '8')
        LIMIT 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The use case is that I have a remote (read slow) database from which
I have ten or more(i don't know) tables that have a column named foo
As a web developer I am using PHP and I know that I have
I know that most people have DirectX9-capable cards but is it really necessary to
I want to have a map of vectors, (but I don't want to use
This is a thing that puzzles me. I know that when you use Euler
I have seen the trend that many people don't like to use exceptions in
I know that cursors are frowned upon and I try to avoid their use
So I have this Java program that I use to munch through several terabytes
tl;dr-edition: I have a compilation I know that will fail, but want the subset

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.