I need to output several rows from a table and process them in two ways:
- aggregated by key
- line by line sorted by the same key
The table looks something like this:
table (
key,
string_data,
numeric_data
)
So, I am considering two approaches to the function that I am writing.
The first one will pull out aggregated data with one query, and then query again inside the loop for each data set row by row (the following pseudo-type PHP type):
$rows = query(
"SELECT key,SUM(numeric_data)
FROM table
GROUP BY key"
);
foreach ($rows as $row) {
<process aggregate data in $row>
$key = $row['key'];
$row_by_row_data = handle_individual_rows($key);
}
function handle_individual_rows($key)
{
$rows = query(
"SELECT string_data
FROM table WHERE key=?",
$key
);
<process $rows one row at a time>
return $processed_data;
}
Or, I could make one big request and let the code do all the work:
$rows = query(
"SELECT key, string_data, numeric_data
FROM table"
);
foreach ($rows as $row) {
<process rows individually and calculate aggregates as I go>
}
Performance is not a practical issue in this application; I just want to write reasonable and convenient code.
, - , . , , ?