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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:39:34+00:00 2026-06-05T11:39:34+00:00

Table1 has columns Id int, Date smalldatetime. View1 has, among many other columns, column

  • 0

Table1 has columns

Id int, Date smalldatetime.

View1 has, among many other columns, column Id int.

View1 has a maximum of 2000 rows, but there are some rather complex computation to determine the values of all the columns.

What is the most efficient way to return all Table1.Id that are not in View1.Id for Table1.Date between '2012-05-30' and '2012-05-31' ?

The filtered selection from Table1 typically returns about 200 unique Table1.Id.

When I do a SELECT * FROM View1, the total data is returned always in under one second. When I do a SELECT Id from Table1 WHERE Date BETWEEN ‘2012-05-30’ AND ‘2012-05-31’, the result is always instanteous.

The moment I tried SELECT Table1.Id from Table1 T1 WHERE Date BETWEEN .. AND .. AND NOT EXISTS (SELECT Id from View1 WHERE ViewId=T1.Id), it takes ages (almost 20s).

I tried using a CTE also, WITH V1 as (SELECT Id from View1) SELECT T1.Id FROM Table1 T1 WHERE Date BETWEEN … and … AND NOT EXISTS (SELECT Id from V1 WHERE V1.Id=T1.Id), and it also took ages.

Thanks.

  • 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-05T11:39:36+00:00Added an answer on June 5, 2026 at 11:39 am

    Try something like this:

    SELECT t.Id, t.[Date]
    FROM dbo.Table1 AS t
    LEFT HASH JOIN dbo.View1 AS v ON v.Id = t.Id
    WHERE t.[Date] >= '20120530' AND t.[Date] < '20120531'
    AND v.Id IS NULL
    

    The HASH hint forces the SQL Server query optimizer to evaluate the view only once.

    Another way would be to use a table variable to store the result of the view:

    DECLARE @ViewResult TABLE (Id int PRIMARY KEY)
    INSERT INTO @ViewResult 
    SELECT Id FROM dbo.View1
    
    SELECT Id, [Date]
    FROM dbo.Table1
    WHERE [Date] >= '20120530' AND [Date] < '20120531'
    AND Id NOT IN (SELECT Id FROM @ViewResult)
    

    Razvan

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

Sidebar

Related Questions

PROFESSIONAL Table has many columns FIRST_NAME,MIDDLE_NAME, LAST_NAME, TITLE_CODE, COMMUNICATION_TYPE_CODE, COMMUNICATION_VALUE PROFESSIONAL Table COMMUNICATION_TYPE_CODE column
I have a table with around 6 million rows and has many columns with
i have a single table called journal. Journal has 4 columns: Date (date) Account(int)
I have a table that has 5 columns: AcctId (int), Address1 (varchar), Address2 (varchar),
I have a table which has the below columns. Ticket_id (Primary key, Int) Attachment1
I have a table PGM_MASTER, which has three columns PGM_ID | int(11) PGM_ENV_ID |
I have a table, tblDocs that has a few columns: DocName varchar(50) DocLocation int
I have two tables - table1 and table2 table1 has 14 columns and table2
I have two tables: table1, table2. Table1 has 10 columns, table2 has 2 columns.
I have a table say table1 which has 3 columns column1, column2 and column3

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.