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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:38:40+00:00 2026-05-15T07:38:40+00:00

I have a lookup table ( ##lookup ). I know it’s bad design because

  • 0

I have a lookup table (##lookup). I know it’s bad design because I’m duplicating data, but it speeds up my queries tremendously. I have a query that populates this table

insert into ##lookup select distinct col1,col2,... from table1...join...etc...

I would like to simulate this behavior:

delete from ##lookup
insert into ##lookup select distinct col1,col2,... from table1...join...etc...

This would clearly update the table correctly. But this is a lot of inserting and deleting. It messes with my indexes and locks up the table for selecting from.

This table could also be updated by something like:

delete from ##lookup where not in (select distinct col1,col2,... from table1...join...etc...)
insert into ##lookup (select distinct col1,col2,... from table1...join...etc...) except if it is already in the table

The second way may take longer, but I can say “with no lock” and I will be able to select from the table.

Any ideas on how to write the query the second way?

  • 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-05-15T07:38:41+00:00Added an answer on May 15, 2026 at 7:38 am
    DELETE LU
    FROM ##lookup LU
    LEFT OUTER JOIN Table1 T1 ON T1.my_pk = LU.my_pk
    WHERE T1.my_pk IS NULL
    
    INSERT INTO ##lookup (my_pk, col1, col2...)
    SELECT T1.my_pk, T1.col1, T1.col2...
    FROM Table1 T1
    LEFT OUTER JOIN ##lookup LU ON LU.my_pk = T1.my_pk
    WHERE LU.my_pk IS NULL
    

    You could also use WHERE NOT EXISTS instead of the LEFT JOINs above to look for non-existence of rows.

    You might also want to look into the MERGE statement if you’re on SQL 2008. Otherwise, you aren’t keeping the tables in sync – you’re only keeping the PKs in sync. If one of the column changes in one table but not the other that won’t be reflected above.

    Either way, it sounds like you might want to consider optimizing queries. While duplicating the data may seem like a nice fix for your performance issues, as you can see it can carry a lot of headaches with it (and this is just one). You’re better off finding the underlying cause of the poor performance and fixing that rather than putting on this ugly bandaid.

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

Sidebar

Related Questions

I have a lookup table called metadata_types in my database, which lists all the
Let's say I have a lookup table which I'd like to make accessible to
I am trying to code a global lookup table of sorts. I have game
I have several 'lookup' fields in my models where I don't want nulls, but
I have a table containing hierarchical data. There are currently ~8 levels in this
I have a reference/lookup table whose main purpose is to provide the user with
I currently have a a simple lookup table. When I display an agency I
For example I have a tag lookup table joining tags to 3 different types
Let's say I have a type of lookup table which I can build for
So I've read enough to know that having one lookup table isn't the best

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.