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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:17:43+00:00 2026-06-17T12:17:43+00:00

I have a decently long (10 tables, about 60k records each) query, all of

  • 0

I have a decently long (10 tables, about 60k records each) query, all of those tables are joined using a left join since they could contain null values (all of them).

I’m seeing a huge performance hit and I tracked it down to this bit of code.

SELECT * 
FROM MAIN_TABLE d 
LEFT JOIN INSURANCES i on i.IMREDEM_CODE = d.IMREDEM_CODE 
                      and i.INS_TERMINATIONDATE IS NULL 
                      and INS_RANK = 0
                      and i.IMREINS_CODE = (SELECT TOP 1 IMREINS_CODE 
                                              from INSURANCES i2
                                             WHERE i2.IMREDEM_CODE = i.IMREDEM_CODE 
                                               and i2.INS_TERMINATIONDATE IS NULL 
                                               and i2.INS_RANK = 0
                                          ORDER BY TAG_SYSTEMDATE DESC)

Basically what I am needing to do is that the insurance table could contain 0 or many records because when they update the insurance it performs an insert not an update for audit purposes. So I have to join the table twice, on a left join. Further I need to do this query twice for primary and secondary insurance (primary is rank = 0 and secondary is rank =1. The IMREDEM_CODE is a PK for the D table and a FK for the i table.

The answer is here:

left join INSURANCES i on i.IMREDEM_CODE = d.IMREDEM_CODE 
and i.IMREINS_CODE = (SELECT max(imreins_code) FROM INSURANCES i2  WHERE i2.IMREDEM_CODE = i.IMREDEM_CODE and i2.INS_TERMINATIONDATE IS NULL and i2.INS_RANK = 0)
  • 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-17T12:17:44+00:00Added an answer on June 17, 2026 at 12:17 pm

    While I’m sure you may be able to optimize this even further, I have taken a quick stab at it. Your execution plan will do 3 Table Scans. You can try this which will reduce that to 2:

    SELECT *
    FROM Main_Table d
        LEFT JOIN 
            (SELECT TOP 1 *
             FROM Insurances 
             WHERE INS_TERMINATIONDATE IS NULL 
                 AND INS_RANK = 0
             ORDER BY TAG_SYSTEMDATE DESC) i on i.IMREDEM_CODE = d.IMREDEM_CODE;
    

    There is probably a better approach than TOP 1 and Order By, but I’m getting tired and can’t think of it at the moment. Either way, this is definitely more efficient.

    Here is the SQL Fiddle with both sets of queries. You can see the execution plan in each.

    Good luck.

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

Sidebar

Related Questions

I have about 45 decently large images (about 680x1000) that need to be loaded
Have 2 tables in Access 2007, both lists of certain tasks to be accomplished.
I have a rails application on a shared server that also has a decently
I still have a question about Enumerations. Here's a quick sketch of the situation.
I have a website, which decently performs in FF and other browsers. but when
I have innoDB table using numerous foreign keys, but we just want to look
I've just about finished coding a decently sized disease transmission model in C#. However,
so essentially I have the task to learn matlab decently well in the next
For decently sized projects I've been told that when you have classes extending JPanels
I've inherited an MVC2 project using a pretty standard and decently well kept DDD

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.