Jacque Scott
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.
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);
What Indexes do you currently have?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.