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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:12:02+00:00 2026-06-13T02:12:02+00:00

Let’s head straight to the example: Query 1: Select * Into #temp_v1 From View1

  • 0

Let’s head straight to the example:

Query 1:

Select * 
Into #temp_v1 
From View1

Select * 
Into #temp_v2
From View2

select *
From #temp_v1 v1
where not exists (
    Select * From #temp_v2 where key = v1.key
)

This is much faster than

Query 2:

select *
From View1 v1
where not exists (
    Select * From View2 where key = v1.key
)

Now, obviously, I have simplified the example. View1 is a view of views and and more comparisons are needed that make it difficult to use a join.

My question is really not how I should rather write my SQL, but how come SQL Server can execute query 1 in 3 seconds, and query 2 in 10 minutes.

And more importantly is there a magic option that I can give the optimizer to create an execution plan like query 1.

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

    If your view is complicated, then accessing the view will be the slowest part of the operation by far, and query 1 minimizes that.

    Suppose each view has 1,000,000 rows. In query 1, you are only retrieving a row from a view 2,000,000 times, while in query 2 you will likely retrieve a row from a view 500,000,000,000 times or more. (In a best-case scenario, every v1.key will exist in view 2. But you still have to check roughly half the rows in view 2 each time to verify that).

    I don’t know about execution plans in SQL server, but writing your query something like this should also be more efficient:

    with keys as (
       select key from View2
    )
    select *
    From View1 v1
    where not exists (
        Select * From keys where key = keys.key
    )
    

    By getting all the keys from view 2 ahead of time, you essentially have the same efficiency of your query 1, without the temporary table step.

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

Sidebar

Related Questions

Let me try to explain by example. Say website is hosted at example.com (NOT
Let's have an example like below: package xliiv.sandbox; import android.app.Activity; import android.os.Bundle; import android.util.Log;
Let me explain best with an example. Say you have node class that can
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
let's say I have a select list as follows: <select name='languages'> <option value='german'>German</option> <option
Let say I've this URL: http://example.com/image-title/987654/ I want to insert download to the part
Let me explain what I am asking for by an example. Imagine I have
Let's say I want to write an FTP client from scratch. In the command
Let's have an example: id (auto_increment) user 1 John 2 Mary 10 Christopher How
Let me give an example and try to explain what I want to ask:

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.