简体   繁体   中英

Performance improvement in JPQL Query for update same table

I want to update a table using jpql. Table structure and condition and query is below.

-------------------------------------------------------------
ASSIGNMENT | BUSINESS_AREA | DC_CURRENCY | DC_AMOUNT | STATUS
-------------------------------------------------------------

In above table structure I want to update all the rows status as completed where sum of DC_AMOUNT is ZERO and DC_CURRENCY is same for all rows , and ASSIGNMENT is also same

My approach

  1. Get all assignment number by query in java layer
  2. Update status to completed for all assignment number which I got in previous result set

Query to get all assignment number satisfying condition

SELECT 
    q.assignment 
FROM 
    OnAccoutEntity q, OnAccoutEntity r 
WHERE 
    q.assignment = r.assignmet 
    AND q.dcCurrency = r.dcCurrency 
    AND q.businessArea = r.businessArea 
GROUP BY 
    q.assignmnt 
HAVING 
    SUM(q.dcAmount) = 0

UPDATE OnAccoutEntity p
SET p.status = 'COMPETED'
WHERE p.assignment IN ("Result of previous query' )

If I use single query to update it is giving exception.

java.sql.SQLException: You cant specify target 'ONACCCOUNT' for update in form clause.

Please suggest a better approach as this is time consuming.

It should do with an EXISTS ,

UPDATE
   OnAccoutEntity o
SET
   o.status = 'COMPLETED'
WHERE
   EXISTS (SELECT
              q.assignment
           FROM
              OnAccoutEntity q
           WHERE
              o.assignment = q.assignment
           AND
              q.dcCurrency = o.dcCurrency
           AND
              q.businessArea = r.businessArea
           GROUP BY
              q.assignment
           HAVING SUM(q.dcAmount) = 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM