Original Query: $\pi_A\left(\sigma_{B = 1 \wedge C < 3}(R)\right)$
Possible Implementations:
Lexical Sort: First sort by B, with C as a tiebreaker.
Which index to use (if several are available)?
SELECT COUNT(*) FROM R
SELECT SUM(A) FROM R
SELECT A, SUM(B) FROM R GROUP BY A
SELECT DISTINCT A FROM R
SELECT A FROM R GROUP BY A
TREE_ID | SPC_COMMON | BORONAME | TREE_DBH |
---|---|---|---|
180683 | 'red maple' | 'Queens' | 3 |
315986 | 'pin oak' | 'Queens' | 21 |
204026 | 'honeylocust' | 'Brooklyn' | 3 |
204337 | 'honeylocust' | 'Brooklyn' | 10 |
189565 | 'American linden' | 'Brooklyn' | 21 |
... and 683783 more |
SELECT COUNT(*) FROM TREES
TREE_ID | SPC_COMMON | BORONAME | TREE_DBH |
---|---|---|---|
COUNT = 0 | |||
180683 | 'red maple' | 'Queens' | 3 |
COUNT = 1 | |||
315986 | 'pin oak' | 'Queens' | 21 |
COUNT = 2 | |||
204026 | 'honeylocust' | 'Brooklyn' | 3 |
COUNT = 3 | |||
204337 | 'honeylocust' | 'Brooklyn' | 10 |
COUNT = 4 | |||
189565 | 'American linden' | 'Brooklyn' | 21 |
COUNT = 5 | |||
... and 683783 more | |||
COUNT = 683788 |
SELECT SUM(TREE_DBH) FROM TREES
TREE_ID | SPC_COMMON | BORONAME | TREE_DBH |
---|---|---|---|
SUM = 0 | |||
180683 | 'red maple' | 'Queens' | 3 |
SUM = 3 | |||
315986 | 'pin oak' | 'Queens' | 21 |
SUM = 24 | |||
204026 | 'honeylocust' | 'Brooklyn' | 3 |
SUM = 27 | |||
204337 | 'honeylocust' | 'Brooklyn' | 10 |
SUM = 37 | |||
189565 | 'American linden' | 'Brooklyn' | 21 |
SUM = 58 | |||
... and 683783 more |
This is also sometimes called a "fold"
Grey et. al. "Data Cube: A Relational Aggregation Operator Generalizing Group-By, Cross-Tab, and Sub-Totals
SELECT SPC_COMMON, COUNT(*) FROM TREES GROUP BY SPC_COMMON
Naive Idea: Keep a separate accumulator for each group
TREE_ID | SPC_COMMON | BORONAME | TREE_DBH |
---|---|---|---|
{} | |||
180683 | 'red maple' | 'Queens' | 3 |
{ 'red maple' = 1 } | |||
204337 | 'honeylocust' | 'Brooklyn' | 10 |
{ 'red maple' = 1, 'honeylocust' = 1 } | |||
315986 | 'pin oak' | 'Queens' | 21 |
{ 'red maple' = 1, 'honeylocust' = 1, 'pin oak' = 1 } | |||
204026 | 'honeylocust' | 'Brooklyn' | 3 |
{ 'red maple' = 1, 'honeylocust' = 2, 'pin oak' = 1 } |
What could go wrong?
TREE_ID | SPC_COMMON | BORONAME | TREE_DBH |
---|---|---|---|
{} | |||
204337 | 'honeylocust' | 'Brooklyn' | 10 |
{ 'honeylocust' = 1 } | |||
204026 | 'honeylocust' | 'Brooklyn' | 3 |
{ 'honeylocust' = 2 } | |||
... and more | |||
315986 | 'pin oak' | 'Queens' | 21 |
{ 'honeylocust' = 3206, 'pin oak' = 1 } | |||
... and more | |||
180683 | 'red maple' | 'Queens' | 3 |
{ 'pin oak' = 53814, 'red maple' = 1 } |