Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to set up a sql view so that any joins are only performed when required?
    primarykey
    data
    text
    <p>I'm having some trouble with view performance. Specifically some expensive joins being performed when they are not required to be. I've managed to reduce the problem to the simplest case, and have used the AdventureWorks sample database to create the following view, which pulls together a persons name and email address.</p> <pre><code>create view PersonDetails_View as select P.FirstName, P.LastName, A.EmailAddress from Person.Person P left outer join Person.EmailAddress A ON P.BusinessEntityID = A.BusinessEntityID </code></pre> <p>If I run the following query against this view:</p> <pre><code>select FirstName from PersonDetails_View </code></pre> <p>The query that is generated is equal to that of:</p> <pre><code>select P.FirstName from Person.Person P left outer join Person.EmailAddress A ON P.BusinessEntityID = A.BusinessEntityID </code></pre> <p>In this query there is an unnecessary join being performed. I know why it is happening - If there are multiple <code>EmailAddress</code> entries per <code>Person</code> row, the results will be different. However in this particular case the mapping is 1-1, and always will be. Is there a way to prevent this join happening, so that the generated query is equal to the following?</p> <pre><code>select P.FirstName from Person.Person P </code></pre> <p>(This simple case does look like micro optimization, but I have properly profiled and identified unnecessary joins in a view as a performance problem in my situation - I promise!)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload