Bug 301599 - Unnecessary Table Join in Native Many-to-Many Query generated from JPQL
Summary: Unnecessary Table Join in Native Many-to-Many Query generated from JPQL
Status: REOPENED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows Vista
: P3 enhancement with 2 votes (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: performance
Depends on:
Blocks:
 
Reported: 2010-02-02 16:05 EST by Bernard Missing name CLA
Modified: 2022-06-09 10:07 EDT (History)
2 users (show)

See Also:


Attachments
NetBeans project in zip file (13.45 KB, application/octet-stream)
2010-02-02 16:07 EST, Bernard Missing name CLA
no flags Details
patch to optimize out join (96.85 KB, patch)
2010-08-24 11:58 EDT, James Sutherland CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Bernard Missing name CLA 2010-02-02 16:05:36 EST
Build Identifier: version: Eclipse Persistence Services - 2.0.0.v20091127-r5931

A rather simple JPQL query produces an unnecessary table join.

The generated native SQL is:

SELECT
    t1.ID
    , t1.NAME
FROM
    PROJECT t0
    , EMPLOYEE_PROJECT t2
    , EMPLOYEE t1
WHERE (
    (t0.ID = ?)
    AND ((t2.Employee_ID = t1.ID)
    AND (t0.ID = t2.projects_ID))
)

The generated SQL should be like the following:

SELECT
    t1.ID
    , t1.NAME
FROM
#    PROJECT t0
    , EMPLOYEE_PROJECT t2
    , EMPLOYEE t1
WHERE (
    (t2.projects_ID = ?)
    AND ((t2.Employee_ID = t1.ID)
)


Reproducible: Always

Steps to Reproduce:
Please refer to the attached testcase (NetBeans project).
Comment 1 Bernard Missing name CLA 2010-02-02 16:07:10 EST
Created attachment 157965 [details]
NetBeans project in zip file
Comment 2 Tom Ware CLA 2010-02-08 10:49:46 EST
What does the actual SQL look like?  I assume it includes a join between the project table and the employee_project table?  Are you asking us to optimize that out?
Comment 3 Bernard Missing name CLA 2010-02-08 19:42:42 EST
Tom,

Yes, I am suggesting to remove the Poject table. It appears to be quite common that EclipseLink unnecessarily joins a table only to use its primary key in other joins.
Comment 4 Tom Ware CLA 2010-02-09 09:08:11 EST
Changing to enhancement.
Comment 5 James Sutherland CLA 2010-08-10 15:31:08 EDT
Fixing in trunk / 2.2

I asume the JPQL query was something like:

"Select e from Employee e join e.projects p where p.id = :id"

Please confirm?
Comment 6 James Sutherland CLA 2010-08-24 11:58:23 EDT
Created attachment 177337 [details]
patch to optimize out join
Comment 7 James Sutherland CLA 2010-08-24 13:41:01 EDT
Fixed in EclipseLink 2.2
Comment 8 James Sutherland CLA 2010-08-30 15:23:13 EDT
Hmm, this caused a test failure, and looking deeper into the issue, this is more difficult than I thought.  Reverting fix, and reopenning bug.

Issue is that the join to the join table is still required and must be normalized once, so querying on project.id and project.name would get very difficult to normalize.
Comment 9 Eclipse Webmaster CLA 2022-06-09 10:07:38 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink