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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:47:08+00:00 2026-05-26T14:47:08+00:00

I have a question about best approach. I am not sure which approach is

  • 0

I have a question about best approach. I am not sure which approach is best when data is considered variable in size.

Consider the following 3 TABLES:

EMPLOYEE

EMPLOYEE_ID, EMP_NAME

PROJECT

PROJECT_ID, PROJ_NAME

EMP_PROJ (many to many of above two tables)

EMPLOYEE_ID, PROJECT_ID

Problem: Given an EmployeeID, find ALL the employees of ALL Projects that this Employee is associated with.

I have tried this in two way.. both approaches differ only by few milliseconds no matter what size of data is used.

SELECT EMP_NAME FROM EMPLOYEE
WHERE EMPLOYEE_ID IN (
    SELECT EMPLOYEE_ID FROM EMP_PROJ    
    WHERE PROJECT_ID IN (
        SELECT PROJECT_ID FROM EMP_PROJ p, EMPLOYEE e
        WHERE p.EMPLOYEE_ID = E.EMPLOYEE_ID 
        AND  E.EMPLOYEE_ID = 123)

go

select c.EMP_NAME FROM
(SELECT PROJECT_ID FROM EMP_PROJ 
  WHERE EMPLOYEE_ID = 123) a
JOIN 
EMP_PROJ b
ON a.PROJECT_ID = b.PROJECT_ID
JOIN 
EMPLOYEE c
ON b.EMPLOYEE_ID = c.EMPLOYEE_ID

As of now, I expect around 5000 Employees and Projects each.. but have no idea about what kinda many-many relationship exists.
Which approach would u recommend?
thanks!

EDIT:
Execution Plan of Approach 1

"Hash Join  (cost=86.55..106.11 rows=200 width=98)"
"  Hash Cond: (employee.employee_id = emp_proj.employee_id)"
"  ->  Seq Scan on employee  (cost=0.00..16.10 rows=610 width=102)"
"  ->  Hash  (cost=85.07..85.07 rows=118 width=4)"
"        ->  HashAggregate  (cost=83.89..85.07 rows=118 width=4)"
"              ->  Hash Semi Join  (cost=45.27..83.60 rows=118 width=4)"
"                    Hash Cond: (emp_proj.project_id = p.project_id)"
"                    ->  Seq Scan on emp_proj  (cost=0.00..31.40 rows=2140 width=8)"
"                    ->  Hash  (cost=45.13..45.13 rows=11 width=4)"
"                          ->  Nested Loop  (cost=0.00..45.13 rows=11 width=4)"
"                                ->  Index Scan using employee_pkey on employee e  (cost=0.00..8.27 rows=1 width=4)"
"                                      Index Cond: (employee_id = 123)"
"                                ->  Seq Scan on emp_proj p  (cost=0.00..36.75 rows=11 width=8)"
"                                      Filter: (p.employee_id = 123)"

Execution Plan of Approach 2:

"Nested Loop  (cost=60.61..112.29 rows=118 width=98)"
"  ->  Index Scan using employee_pkey on employee e  (cost=0.00..8.27 rows=1 width=4)"
"        Index Cond: (employee_id = 123)"
"  ->  Hash Join  (cost=60.61..102.84 rows=118 width=102)"
"        Hash Cond: (b.employee_id = c.employee_id)"
"        ->  Hash Join  (cost=36.89..77.49 rows=118 width=8)"
"              Hash Cond: (b.project_id = p.project_id)"
"              ->  Seq Scan on emp_proj b  (cost=0.00..31.40 rows=2140 width=8)"
"              ->  Hash  (cost=36.75..36.75 rows=11 width=8)"
"                    ->  Seq Scan on emp_proj p  (cost=0.00..36.75 rows=11 width=8)"
"                          Filter: (employee_id = 123)"
"        ->  Hash  (cost=16.10..16.10 rows=610 width=102)"
"              ->  Seq Scan on employee c  (cost=0.00..16.10 rows=610 width=102)"

So looks like the Execution plan of Approach 2 is slightly better, because the ‘cost’ is 60 as opposed to 85 of approach 1. Is that the right way to analyze this?

How does one know it will hold true even for all sorts of many-many combinations?

  • 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-26T14:47:09+00:00Added an answer on May 26, 2026 at 2:47 pm

    Both approaches may cause sub-optimum performance as they both use subqueries which are optimized efficiently or not depending on the DBMS.

    I would suggest to avoid subqueries and use JOIN for what is is meant to be used, like:

    Edit:

    I corrected and simplyfied my example:

    select coll.EMP_NAME
    from EMP_PROJ ep1
    inner join EMP_PROJ ep2 on ep1.PROJECT_ID = ep2.PROJECT_ID
    inner join EMPLOYEE coll on ep2.EMPLOYEE_ID = coll.EMPLOYEE_ID
    where ep1.EMPLOYEE_ID = 123
    

    I think one should note that is is perfectly legal to refer to the same table more than once with different aliases in one query. To the query this logically “looks” like two separate tables which happen to have the same structure and data.

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

Sidebar

Related Questions

I have a question about best practices in Java. I'm writing a class, which
I have a question about jQuery load best practice. Imagine the following situation: $(#div).on('click',
I have a question about best practices regarding how one should approach storing complex
I have a dilemma and I'm not sure about the best way to start
I have a question about the proper, best way to manage the model. I
I have posted a question about multilanguage database design here, [] What are best
I have question about interpreting strings as packed binary data in C++. In python,
i have question about YAJLiOS parser... I have next json data : {{ body
I have a question about storing site configuration data. We have a platform for
I'm developing a site and I have some doubts about the best approach to

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.