Oxon
Oxon

Reputation: 5041

Sql Server View with Inner Joins Performance

I am trying to understand how Sql Server handles views and joins inside the views.

I have a view something like.

CREATE VIEW 
        [dbo].[vw_Product]
AS
    SELECT
        Product.Id AS ProductId,
        Customer.Name AS CustomerName
    FROM
        Product
    INNER JOIN
        Customer ON Customer.Id = Product.CustomerId

What is the difference between the following two queries in terms of performance?

SELECT ProductId, CustomerName FROM vw_Product

SELECT ProductId FROM vw_Product

The reason for asking this question is that the first query selects CustomerName. In the view there is an Inner Join on the Customer table. An Inner Join execution has its extra cost on the performance.

Upvotes: 0

Views: 3635

Answers (2)

Bogdan Bogdanov
Bogdan Bogdanov

Reputation: 1723

Basically the second query will be a bit slower, because the data returned by the SQL Server contains additional column. In both cases INNER JOIN will take place.

Upvotes: 1

Madhivanan
Madhivanan

Reputation: 13700

It does not matter whether you select column from single table or multiple tables, the JOIN is performed always. So I would say the difference is negligible

Upvotes: 1

Related Questions