Okay I have table:
ID Name Description Picture
1 Alex Alex desc.. 2
2 Maria NULL 3
3 John John desc.. NULL
table picture has ID and varbinary image.
I need to select: If description exists, then description, else picture
I do this:
select Id,
Name,
Case when Description is null then pic.Image else Description
from person per join picture pic on per.Picture = pic.Id
So, looks like unnecessary join if description is not null.
Anyways. any suggestions on improving this simple query? also, What are good easy to use tools for performance comparison between two version of queries?
As far as I know there is no way to conditionally join within a single query. I would probably rewrite what you have as:
I’m not sure how the performance compares, but it seems a little cleaner.
As far as comparing queries goes, I would recommend looking at the execution plan for both and seeing if either are doing any scans. Here is some info on using execution plans for performance tuning:
You can also use the Database Tuning Advisor as a quick way to check to see if you have the necessary indexes.
Be careful to not spend too much time over optimizing either. While it’s good to keep an eye on both current performance and performance as your database grows, it’s easy to spend a lot of time on this without getting too much in return. If you have a good database structure and well written queries, then future optimizations shouldn’t be too much of a pain.