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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:44:37+00:00 2026-05-15T13:44:37+00:00

I am going through some pl/sql code with no comments. Trying to make sense

  • 0

I am going through some pl/sql code with no comments. Trying to make sense of it and optimizing it. Here is the sample:

INSERT INTO gtt1 --75711 rows
(USER_ID, role_id, participant_code, status_id )
SELECT
 r.user_id, r.role_id, r.participant_code, MAX(status_id)
FROM
  user_role r,
  cmp_role c
WHERE
  r.role_id = c.role_id
  AND r.participant_code IS NOT NULL
  AND c.group_id = 3
  GROUP BY
  r.user_id, r.role_id, r.participant_code;

Then

DELETE gtt1
WHERE ROWID IN (SELECT ROWID FROM gtt1
                MINUS
                SELECT a.ROWID FROM gtt1 a, UIV_CMP_USER_ROLE b
                WHERE a.status_id = b.status_id
                AND (b.ACTIVE = 1 OR ( b.ACTIVE IN ( 0,3 ) 
                      AND SYSDATE BETWEEN b.effective_from_date AND b.effective_to_date
                     )
                )
                );

finally (this takes longest)

OPEN cv_1 FOR

SELECT c.role_id,
       c.subgroup,
       c.subgroup_description,
       COUNT(a.USER_ID) user_count
FROM   
    (SELECT b.user_id, b.role_id FROM gtt1 b, pt_user e
    --pt_user table has 73000 rows
        WHERE  e.user_id = RTRIM(b.user_id)
       ) a
RIGHT OUTER JOIN CMP_ROLE c ON a.role_id = c.role_id
WHERE c.group_id = v_group_id
GROUP BY c.role_id,c.subgroup,c.subgroup_description
ORDER BY c.subgroup;

Is there a way I can avoid the deletion from gtt1 and initially just get rows we want?

Running explain plan I notice some full table scans on this query:

SELECT 
   r.user_id, r.role_id, r.participant_code, MAX(status_id) 
  FROM 
    user_role r, 
    cmp_role c 
  WHERE 
    r.role_id = c.role_id 
    AND r.participant_code IS NOT NULL 
    AND c.group_id = 3 
    GROUP BY 
    r.user_id, r.role_id, r.participant_code 
    HAVING MAX(status_id) IN (SELECT b.status_id FROM UIV_CMP_USER_ROLE b 
                              WHERE (b.ACTIVE = 1 OR ( b.ACTIVE IN ( 0,3 )  
                                     AND SYSDATE BETWEEN b.effective_from_date AND b.effective_to_date 
                                    )) 
                             ) 

user_role = 803507 rows

cmp_role = 27 rows

user_role has 5 indexes:

idx 1 = role_id

idx 2 = last_updt_user_id

idx 3 = actv_id, participant_code, effective_from_Date, effective_to_date

idx 4 = user_id, role_id, effective_from_Date, effective_to_date

idx 5 = participant_code, user_id, roke_id, actv_cd

  • 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-15T13:44:37+00:00Added an answer on May 15, 2026 at 1:44 pm

    The INSERT and DELETE appear to me to be equivalent to this:

    INSERT INTO gtt1 --75711 rows
    (USER_ID, role_id, participant_code, status_id )
    SELECT
     r.user_id, r.role_id, r.participant_code, MAX(status_id)
    FROM
      user_role r,
      cmp_role c
    WHERE
      r.role_id = c.role_id
      AND r.participant_code IS NOT NULL
      AND c.group_id = 3
      GROUP BY
      r.user_id, r.role_id, r.participant_code
      HAVING MAX(status_id) IN (SELECT b.status_id FROM UIV_CMP_USER_ROLE b
                                WHERE (b.ACTIVE = 1 OR ( b.ACTIVE IN ( 0,3 ) 
                                       AND SYSDATE BETWEEN b.effective_from_date
                                                   AND b.effective_to_date
                                      ))
                               );
    

    Whether that is more efficient I have no idea – I’d need to know a lot more about the tables, indexes and data.

    You could then go further and turn the GTT into a subquery, for example:

    WITH gtt1 AS 
      (SELECT
       r.user_id, r.role_id, r.participant_code, MAX(status_id)
      FROM
        user_role r,
        cmp_role c
      WHERE
        r.role_id = c.role_id
        AND r.participant_code IS NOT NULL
        AND c.group_id = 3
        GROUP BY
        r.user_id, r.role_id, r.participant_code
        HAVING MAX(status_id) IN (SELECT b.status_id FROM UIV_CMP_USER_ROLE b
                                  WHERE (b.ACTIVE = 1 OR ( b.ACTIVE IN ( 0,3 ) 
                                         AND SYSDATE BETWEEN b.effective_from_date AND b.effective_to_date
                                        ))
                                 )
      )
    SELECT c.role_id,
           c.subgroup,
           c.subgroup_description,
           COUNT(a.USER_ID) user_count
    FROM   
        (SELECT b.user_id, b.role_id FROM gtt1 b, pt_user e
        --pt_user table has 73000 rows
            WHERE  e.user_id = RTRIM(b.user_id)
           ) a
    RIGHT OUTER JOIN CMP_ROLE c ON a.role_id = c.role_id
    WHERE c.group_id = v_group_id
    GROUP BY c.role_id,c.subgroup,c.subgroup_description
    ORDER BY c.subgroup;
    

    Again, I have no idea whether this is more or less efficient than the current code.

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

Sidebar

Related Questions

Possible Duplicate: SQL: What does =* mean? I am going through some legacy code
I am going through some code and at the beginning of the script we
I was going through some code that I downloaded off the internet ( Got
I am going through some example assembly code for 16-bit real mode. I've come
I'm going through some old C#.NET code in an ASP.NET application making sure that
I'm trying to learn SQL Service Broker (SSB), and I'm starting by going through
Im new to SQL SERVER , I was going through some of the examples
Going through some of my older Delphi projects and upgrading them to D2009, as
Going through some documentation on modifying CGImageRef data, I came across a strange example
While going through some tutorials, I have encountered lines such as this: ((IDisposable)foo).Dispose(); Ignore

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.