How do views work? Does it join at the time of query execution, or do
views contain the data like tables? (Something like a virtual table.)
View
contain no data at all, it's just a saved piece of code similar to stored procedure
but "joinable".
When you use a view
in your query, the view definition is used, the name of view is substituted with view definition and execution plan
is built for this query with substitution, you won't see any view mention in the plan
, only based tables.
Is there any limitation on the view, like maximum number of joins, or
maximum number of records?
Maximum number of tables
for SELECT
statement depends on your server version
, you can check it here: Maximum Capacity Specifications for SQL Server, for example, it was equal to 256 in 2008
, in SQL Server 2017
it's limited only by available resources.
A view can have a maximum of 1,024 columns
as described here CREATE VIEW (Transact-SQL)
There is no limit on number of rows returned.
Instead of a view, if I create a new table, can I retrieve the records
faster?
It depends. You should examine the execution plan
for your query.
Note that if you just copy the data returned by the query from your view definition to a new table, it will not automatically update when the data in based tables changes.
To make your "copy of data" automatically updatable you can create clustered index on your view. This is called Indexed view and has some limitations.
In any case you should start from your query actual execution plan
, in order to find where server waste time and to understand if it's wrong cardinality estimation or just a lack of indexes
on base tables.