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

  • Home
  • SEARCH
  • 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 5938411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:40:54+00:00 2026-05-22T15:40:54+00:00

In this sql: SELECT s.*, u.id, u.name FROM shops s LEFT JOIN users u

  • 0

In this sql:

    SELECT s.*,
           u.id,
           u.name 
      FROM shops s 
 LEFT JOIN users u ON u.id = s.user_id 
                   OR u.id = s.owner_user_id 
     WHERE s.status = 1

For some reason this query takes an amazing time. although id is the primary key. it seems especially after I added this part OR u.id=s.owner_user_id the query became slow. owner_user_id often is 0 only handful of times. But why would it take so long apparently scanning the whole table? The database table users is very long and big. I didn’t design it. this is for a client who subsequent programmers added too many fields. the table is 22k rows and dozens of fields.

*the names of the fields for demonstration only. actual names are different, so don’t ask me why I’m looking for owner_user_id (; I did solve the slowness by remove the “OR …” part and instead searching for the id in the loop if it is not 0. but I would like to know why this is happening and how to speedup that query as is.

  • 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-22T15:40:55+00:00Added an answer on May 22, 2026 at 3:40 pm

    You may be able to speed it up by using IN instead of the OR but that is minor.

    SELECT u.id,
               u.name 
          FROM shops s
     LEFT JOIN users u ON u.id IN ( s.user_id, s.owner_user_id )
    WHERE s.status = 1
    

    Firstly, are there any indexes on this table? Mainly one on the user.id field or the s.user_id or s.owner_user_id?

    However, I must ask why you need to use a LEFT JOIN instead of a regular join. The LEFT JOIN causes the matching of every row with every other one. And since I’m assuming the value / id should either be in the user_id or the owner_user_id field, and that there will always be a match, if that is the case then the use of a JOIN should speed the query up a bit.

    And as Mitch said, 22k rows is tiny.

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

Sidebar

Related Questions

this subquery works in SQL Server: select systemUsers.name, (select count(id) from userIncidences where idUser
I have this SQL query: SELECT * FROM IMAGES WHERE IMAGENAME in ('IMG1', 'IMG2',
I seem to have a problem with this SQL query: SELECT * FROM appts
i have sql statement like this SELECT DISTINCT results_sp_08.material_number FROM results_sp_08 INNER JOIN courses
I am trying to execute this SQL command: SELECT page.page_namespace, pagelinks.pl_namespace, COUNT(*) FROM page,
The following SQL SELECT * FROM customers converted to this in LINQ var customers
I want to do something like this: SQL.Text := Format('select foo from bar where
I have this query in sql server 2000: select pwdencrypt('AAAA') which outputs an encrypted
In SQL server 2005 this query select len(cast('the quick brown fox jumped over the
Hi guys i need to replicate this SQL query in Linq to Entity select

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.