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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:38:53+00:00 2026-06-07T16:38:53+00:00

PID UID Req Res 1 101 C to 2 101 A from 3 101

  • 0
PID UID Req Res

1   101 C   to
2   101 A   from
3   101 B   to
4   102 A   from
5   102 B   from

I want to write a query to get all the records from table which satisfies the below condition.

I want to get UID of all the records where Res=’to’. For these particular UID I want to get records with Req=’A’. I wrote a sample query and it worked also. Please find the query below.

SELECT Res,
       UID,
       Req
FROM   TABLE
WHERE  Req = 'A'
       AND UID = (SELECT DISTINCT( UID )
                  FROM   TABLE
                  WHERE  Res = 'to') 

The problem with the above query is that while running in SQL server 2008, it is running for more than one hour for fetching just 15-20 records. Total number of records is big as this is a production table. I wish to know why the query is long running as well as whether I can get an optimized query for the same.

Thanks,
Prabhath

  • 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-07T16:38:56+00:00Added an answer on June 7, 2026 at 4:38 pm

    The reason your query is so slow is that your engine is probably running that subquery for every single row it considers, and that query may be an implicitly aggregating (since it uses DISTINCT). That’s a lot of work to do for each row, and as it gets later into the result set, it is likely doing plenty of swapping to make it happen.

    This query joins the table to itself on the condition that the joined row has the same UID and has Res = ‘to’. Rows are filtered based on two factors: having at least one matching row with same UID and Res=’to’, and having Req=’A’.

    This will be a very fast solution, and here’s why: Query engines are optimized to eliminate rows from the result set as soon as they can. For rows PID=1, 3, &5, the engine will immediately discard them from consideration (not even perform the join) since Req != ‘A’. For row PID=2, the join will match row 1 and then usually not even continue on to join 3 (since it knows via the DISTINCT keyword (and the fact that no columns from the join are being selected) that matching additional rows in the join would not change the result set). And for PID=4, it won’t match any.

    SELECT DISTINCT main_row.Res,
           main_row.UID,
           main_row.Req
    FROM   TABLE AS main_row
    JOIN   TABLE AS res_join 
           ON main_row.UID = res_join.UID 
           AND res_join.Res = 'to'
    WHERE  main_row.Req = 'A'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

$pid = $fb->api(array( 'method' => 'fql.query', 'query' => 'SELECT first_name from user where uid='.$session['uid'])
I have a table (TABLE1) which has 4 columns: UID | PID | VALUE1
SO_PEERCRED is simple way to get pid/uid/gid of connected AF_UNIX stream socket, SCM_CREDENTIALS is
I like to get window pid (only firefox) from wmctrl, i tried wmctrl -lp
I need some table like this one: uid:integer pid:integer predesposition:integer primary_key(uid,pid) foreign_key(uid, user(id)) foreign_key(pid,
i am having table structure like this table1 table2 pid pname pid uid cat
Given a pid, I want to find the owner of the process (as uid).
i have three tables, i want to select all data from one of the
for instance, in bash when i type ps -wef I will get: UID PID
I have these tables: comments uid id pid pages pid user users id rank

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.