I need to often run a query that aggregates data from different tables, something like
select
Name, Code, Date From TableA
union
select
Surname, TheCode, TheDate From TableB
union
[...] -- this stands for some more select statements
union
select
NickName, MyCode, getdate() from tableC
(the example is simplicistic, but it is like, this: I cannot refactor the database to put al the data I need in a single table!).
THis query can return 100.000 records, even if typically it will be 400-500 because of the WHERE conditions.
I am considering using a view to simplify the queries, but does this make sense? Is querying the view faster because the view is pre-calculated (I am not sure about this) or the view is executed as it is queried? I mean: if 10 users reuqest the same data running 10 queries against tables with joins ot running 10 queries against a view is it the same or is it better to have a view? (I mean if the view is executed only once it is better, if not it is exactly the same).
Moreover I cannot use an indexed view since I am using UNION STATEMENTS.
The advantage of a view would be that I can also easily do a select count (*) against the view, while to do this with tables I should write almost 2 different queries: one (the one I wrote above) for getting the records, and another one (modifying the one above) for the count.