Link to home
Start Free TrialLog in
Avatar of Jacque Scott
Jacque ScottFlag for United States of America

asked on

Optimizing a query

I have the below query and it runs very slow.  How can I rewrite it so it will run faster?  Here is some background:

 I have a table called Events.  There are 2,931,695 records.  We store data for documents in the Events table.

We have documents that we want some of our clients to be able to view and download at anytime.  The client will access a website to view these documents.  If there is a document that a user can view then we INSERT data from the EVents table into the tblCP_PublishedDocument table on a different server.  The website will access the tblCP_PublishedDocument  instead of the Events table.  If a document is deleted from the Events table we need to delete it from the tblCP_PublishedDocument table.  I am writing a stored procedure that will run on a scheduled basis to check if there are entries in the tblCP_PublishedDocument table but not in the Events table.  If there are then delete it.

DELETE FROM [webserver2.MyCompany.com].[MyDatabase].[dbo].tblCP_PublishedDocument WHERE PublishedDocument NOT IN (SELECT Events FROM Events);

Open in new window

Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

What Indexes do you currently have?
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jacque Scott

ASKER

Thank you very much.  I went with the tempTable version and it is so quick.  Only about 10 seconds compared to over 1 min.