Let’s start with the scenario defined in my previous question.
Now I want to create a query that generates the list of Foos F1 and the count of Foos F2 that are distinct than F1 but are nevertheless associated to the same Bar or Baz F1 is associated to:
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
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 F1.Foo_ID. This is clearly not efficient.
An alternative is that does not run into this problem is…
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
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 F1.Foo_ID. Thus, GROUP BY F1.Foo_ID should be sufficient to produce the desired result. But SQL still forces me to explicitly GROUP BY every field not affected by an aggregate function. The result? Inefficiency.
A third alternative that does not run into any of the two previous problems is…
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
But this has the disadvantage of requiring the instantiation of three copies of Foo in memory, not just two.
How should I structure my query to produce the desired result in the most efficient way possible?
I agree with the comments stating that you can only find out by trying. In your other post you say that you have no test data available. So my guess is you don’t know how to generate test data. I’ll show you.
I assume the following tables exist:
Now populate Bar with test data:
Populate Baz:
and put some data in Foo
Before you run the queries and test their execution speed, make sure you have some indexes created:
Now you can test your queries. I suggest you try this one too:
In my old version of SQL Query Analyzer there is an option to ‘Display the generated execution plan’. Your version will have that option too probably. It shows that the above query will run faster than the 3 queries you suggested. But that is theory! So fill your tables with as much data as you think it will have in the production system and try.