how to combine 2 separate queries in sql server (1 Viewer)

Galaxiom

Super Moderator
Staff member
Local time
Today, 21:26
Joined
Jan 20, 2009
Messages
12,851
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.
Code:
OPTION (RECOMPILE)
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.
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.
 

Isaac

Lifelong Learner
Local time
Today, 04:26
Joined
Mar 14, 2017
Messages
8,774
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.
Don't DBAs usually keep up with this every little while? I thought I remember them telling me at one of my jobs that a particular thing (which they ran about every half hour) had accomplishing this as a side effect.
 

Users who are viewing this thread

Top Bottom