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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:34:12+00:00 2026-06-12T00:34:12+00:00

Here is my query, SELECT ID As Col1, ( SELECT VID FROM TABLE2 t

  • 0

Here is my query,

   SELECT ID As Col1,
   (
         SELECT VID FROM TABLE2 t 
         WHERE (a.ID=t.ID or a.ID=t.ID2) 
         AND t.STARTDTE =
         (
               SELECT MAX(tt.STARTDTE) 
               FROM TABLE2 tt 
               WHERE (a.ID=tt.ID or a.ID=tt.ID2)  AND tt.STARTDTE < SYSDATE
         )
   ) As Col2
   FROM TABLE1 a

Table1 has 48850 records and Table2 has 15944098 records.

I have separate indexes in TABLE2 on ID,ID & STARTDTE, STARTDTE, ID, ID2 & STARTDTE.

The query is still too slow. How can this be improved? Please help.

  • 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-12T00:34:13+00:00Added an answer on June 12, 2026 at 12:34 am

    I’m guessing that the OR in inner queries is messing up with the optimizer’s ability to use indexes. Also I wouldn’t recommend a solution that would scan all of TABLE2 given its size.

    This is why in this case I would suggest using a function that will efficiently retrieve the information you are looking for (2 index scan per call):

    CREATE OR REPLACE FUNCTION getvid(p_id table1.id%TYPE) 
       RETURN table2.vid%TYPE IS
       l_result table2.vid%TYPE;
    BEGIN
       SELECT vid
         INTO l_result
         FROM (SELECT vid, startdte
                 FROM (SELECT vid, startdte
                         FROM table2 t
                        WHERE t.id = p_id
                          AND t.startdte < SYSDATE
                        ORDER BY t.startdte DESC)
                WHERE rownum = 1
               UNION ALL
               SELECT vid, startdte
                 FROM (SELECT vid, startdte
                         FROM table2 t
                        WHERE t.id2 = p_id
                          AND t.startdte < SYSDATE
                        ORDER BY t.startdte DESC)
                WHERE rownum = 1
                ORDER BY startdte DESC)
        WHERE rownum = 1;
       RETURN l_result;
    END;
    

    Your SQL would become:

    SELECT ID As Col1,
           getvid(a.id) vid
      FROM TABLE1 a
    

    Make sure you have indexes on both table2(id, startdte DESC) and table2(id2, startdte DESC). The order of the index is very important.

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

Sidebar

Related Questions

Here is my query select * from table1 inner join table2 on table1.typeId=table2.typeId; This
I have an oracle sql query select distinct tab1.col1, tab2.col1 from table1 tab1 join
Here's my query SELECT * FROM wp_postmeta WHERE meta_value LIKE '.$_POST['username'].' AND meta_value LIKE
Here's the query: SELECT COUNT(*) AS c, MAX(`followers_count`) AS max_fc, MIN(`followers_count`) AS min_fc, MAX(`following_count`)
Here is my query Select Gender from Table The result pane shows 1 1
Here is my Query : select EmployeeName, EmployeeSalary from Employee2 for xml path('EmployeeDetails') returns
Here is my query select t1.*, t2.name as customer, t2.name as vendor from order
here is the query SELECT * FROM customers WHERE NOT EXISTS ( SELECT 1
Here is my sample query: SELECT userid,count(*) FROM hits GROUP BY userid My table
So i have a query that looks like this: SELECT col1, col2, col3 ...

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.