|
|
|
|
|
by code_biologist
637 days ago
|
|
Instead of joining tables and using distinct or similar to filter rows, consiser using subquery "columns", ie in SELECT list. What does this mean? Running SELECT
column1,
(
SELECT column2, column3, ...
FROM table_b
WHERE table_a.id = table_b.a_id
)
FROM table_a
Results in "subquery must return only one column" as I expected. You mean returning the multiple columns as a record / composite type?Keep in mind GROUP BY clause usually dictates index use. The reason for this wasn't immediately apparent to me. For those who were curious, this blog post walks through it step by step: https://www.brentozar.com/archive/2015/06/indexing-for-group... |
|
> The reason for this wasn't immediately apparent to me.
The key thing to remember is that grouping is essentially a sorting operation, and it happens before your other sorts (that last part isn't necessarily as obvious).