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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:44:54+00:00 2026-06-04T16:44:54+00:00

I have the following sql which takes a long time to execute, are there

  • 0

I have the following sql which takes a long time to execute, are there any better way of writing this to improve speed.

Any help is highly appreciable.

Thanks

    SELECT a.KeyField
    FROM Details a,
         Master b
    WHERE a.ForeignKeyField = b.ForeignKeyField
      AND a.KeyField IS NOT NULL
      AND Date BETWEEN 
TO_DATE ('01-01-2011', 'dd-mm-yyyy') AND TO_DATE ('31-12-2011', 'dd-mm-yyyy')
      AND a.KeyField IN
        (SELECT p.KeyField
         FROM Details p,
              Master q
         WHERE q.ForeignKeyField = p.ForeignKeyField
           AND p.KeyField IS NOT NULL
           AND p.KeyField = a.KeyField
           AND q.FKField2 = b.FKField2
         GROUP BY p.KeyField,
                  q.Date HAVING COUNT (DISTINCT q.ForeignKeyField) > 1)
    GROUP BY b.Id,
             b.Name,
             b.ForeignKeyField,
             b.Date,
             a.ForeignKeyField,
             a.SomeOtherField,
             a.KeyField,
             b.EtcEtc
  • 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-04T16:44:55+00:00Added an answer on June 4, 2026 at 4:44 pm

    First, you need to learn proper join syntax. My guess is that Oracle is choosing a suboptimal query path because of the correlated subquery. Let’s change it to a join instead:

    SELECT a.KeyField
    FROM Details a join
     Master b
     on a.ForeignKeyField = b.ForeignKeyField join
     (SELECT p.KeyField, q.Date, q.FKField2
      FROM Details p join
           Master q
           on q.ForeignKeyField = p.ForeignKeyField
      WHERE p.KeyField IS NOT NULL AND
            q.FKField2 = b.FKField2
      GROUP BY p.KeyField, q.Date
      HAVING COUNT (DISTINCT q.ForeignKeyField) > 1
     ) t
     on a.KeyField = t.KeyField and
        b.FKField2 = t.FKField2
    WHERE a.KeyField IS NOT NULL AND
      Date BETWEEN TO_DATE ('01-01-2011', 'dd-mm-yyyy') AND TO_DATE ('31-12-2011', 'dd-mm-yyyy') AND
    GROUP BY b.Id, b.Name, b.ForeignKeyField, b.Date, a.ForeignKeyField, a.SomeOtherField, a.KeyField, 
         b.EtcEtc
    

    Writing it this way also makes me realize that there is no condition on the Date, although that is used in the subquery. Is this correct?

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

Sidebar

Related Questions

I have following query which takes almost 1 minute to execute. public static Func<Entities,
I have following somewhat complex sql query which has horrible performance, 'certainly' due to
I have the following piece of SQL: select DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)) which comes through as
I have the following which works in SQL Query Analyzer . select oh.* from
I have an sql table which has the following rows. (4081, 3, '', 'contrapreneurship.jpg',
I have the following SQL which creates a friends list with other info: SELECT
i have the following SQL Query which runs on SQL Server CE 4 SELECT
I have the following function which takes a tiff stored as blob from an
NHibernate is driving me 'Nuts'!!! I have the following mapping. Which returns this error
i have in a procedure which fills a table the following sql SELECT NVL(SUM(COL1),

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.