Sometimes the stored query plan can become inappropriate. Forcing a new plan to be generated can help.
One easy way to do this is to add the following line to the end of the query.
Remove it or comment it out after running it as you don't want the plan rebuilt every time.
Another possibility is the statistics on the tables get stale after records have been added. This especially so if the proportion of records with certain values in one of the criteria fields changes radically. Fix this by updating the statistics by running the following command.
BTW The performance of Union queries can be very sensitive to the order of the subqueries. Sometimes putting them the other way around makes a huge difference. The one with the most data returned should be first.
One easy way to do this is to add the following line to the end of the query.
Code:
OPTION (RECOMPILE)
Another possibility is the statistics on the tables get stale after records have been added. This especially so if the proportion of records with certain values in one of the criteria fields changes radically. Fix this by updating the statistics by running the following command.
Code:
UPDATE STATISTICS tablename
BTW The performance of Union queries can be very sensitive to the order of the subqueries. Sometimes putting them the other way around makes a huge difference. The one with the most data returned should be first.