Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL optimization question
    primarykey
    data
    text
    <p>Let's start with the scenario defined in <a href="https://stackoverflow.com/questions/6764830/efficiency-of-using-case-when-is-not-null-vs-isnull-coalesce">my previous question</a>.</p> <p>Now I want to create a query that generates the list of <code>Foo</code>s <code>F1</code> and the count of <code>Foo</code>s <code>F2</code> that are distinct than <code>F1</code> but are nevertheless associated to the same <code>Bar</code> or <code>Baz</code> <code>F1</code> is associated to:</p> <pre><code>SELECT F1.*, CASE WHEN F1.Bar_ID IS NOT NULL THEN ISNULL(Bar.LotNumber + '-', '') + Bar.ItemNumber WHEN F2.Baz_ID IS NOT NULL THEN ISNULL(Baz.Color + ' ', '') + Baz.Type END AS 'Ba?Description', (SELECT COUNT(*) FROM Foo F2 WHERE F2.Bar_ID = F1.Bar_ID OR F2.Baz_ID = F1.Baz_ID) - 1 AS FooCount FROM Foo F1 LEFT JOIN Bar ON Bar.Bar_ID = F1.Bar_ID LEFT JOIN Baz ON Baz.Baz_ID = F1.Baz_ID </code></pre> <p>What worries me is efficiency. I must admit I know nothing regarding how SQL Server generates execution plans from SQL sentences, but common sense tells me that the subquery would be executed once for each row in the main query, i.e., once for each value of <code>F1.Foo_ID</code>. This is clearly not efficient.</p> <p>An alternative is that does not run into this problem is...</p> <pre><code>SELECT F1.*, CASE WHEN F1.Bar_ID IS NOT NULL THEN ISNULL(Bar.LotNumber + '-', '') + Bar.ItemNumber WHEN F2.Baz_ID IS NOT NULL THEN ISNULL(Baz.Color + ' ', '') + Baz.Type END AS 'Ba?Description', COUNT(*) - 1 AS FooCount FROM Foo F1 LEFT JOIN Bar ON Bar.Bar_ID = F1.Bar_ID LEFT JOIN Baz ON Baz.Baz_ID = F1.Baz_ID LEFT JOIN Foo F2 ON F2 .Bar_ID = F1.Bar_ID OR F2 .Baz_ID = F1.Baz_ID GROUP BY F1.Foo_ID, F1.SomeFooField, F1.SomeOtherField, ..., CASE WHEN F1.Bar_ID IS NOT NULL THEN ISNULL(Bar.LotNumber + '-', '') + Bar.ItemNumber WHEN F2.Baz_ID IS NOT NULL THEN ISNULL(Baz.Color + ' ', '') + Baz.Type END </code></pre> <p>But this is even worse, since it runs into a bigger problem that is related to the fact that SQL databases are not true relational databases. If SQL databases were truly relational, then SQL engines would be able to infer that the value of every field that is not affected by an aggregate function is uniquely determined by <code>F1.Foo_ID</code>. Thus, <code>GROUP BY F1.Foo_ID</code> should be sufficient to produce the desired result. But SQL still forces me to explicitly <code>GROUP BY</code> every field not affected by an aggregate function. The result? Inefficiency.</p> <p>A third alternative that does not run into any of the two previous problems is...</p> <pre><code>SELECT Foo.*, CASE WHEN Foo.Bar_ID IS NOT NULL THEN ISNULL(Bar.LotNumber + '-', '') + Bar.ItemNumber WHEN Foo.Baz_ID IS NOT NULL THEN ISNULL(Baz.Color + ' ', '') + Baz.Type END AS 'Ba?Description', ISNULL(Temp.FooCount, 0) AS FooCount FROM Foo LEFT JOIN Bar ON Bar.Bar_ID = Foo.Bar_ID LEFT JOIN Baz ON Baz.Baz_ID = Foo.Baz_ID LEFT JOIN (SELECT F1.Foo_ID, COUNT(*) - 1 AS FooCount FROM Foo F1 JOIN Foo F2 ON F2.Bar_ID = F1.Bar_ID OR F2.Baz_ID = F1.Baz_ID GROUP BY F1.Foo_ID) Temp ON Temp.Foo_ID = Foo.Foo_ID </code></pre> <p>But this has the disadvantage of requiring the instantiation of <em>three</em> copies of <code>Foo</code> in memory, not just two.</p> <p>How should I structure my query to produce the desired result in the most efficient way possible?</p>
    singulars
    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.
 

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