I am looking for a good design template or best practice to achieve a situation of “either this request or another request”, with maximum performance and lowest cost.
The business logic / demans program says “all elements with Foo” unless it returns less than three elements and then “all elements”. I am refactoring the current code and cannot find a good way to achieve this logic.
Current Pseudocode (Drupal / PHP):
<?php
$result = db_query(
'SELECT n.uid FROM og_ancestry oga ' .
'INNER JOIN node n on n.nid = oga.nid ' .
'WHERE oga.group_nid = %d AND n.created > %d GROUP BY n.uid ' .
'ORDER BY cnt DESC LIMIT %d', $group_nid, $since, $limit);
while ($row = db_fetch_array($result)) {
$uids[] = $row['uid'];
}
if (count($uids) < 3) {
$result = db_query(
'SELECT n.uid FROM og_ancestry oga ' .
'INNER JOIN node n on n.nid = oga.nid ' .
'WHERE oga.group_nid = %d GROUP BY n.uid ' .
'ORDER BY cnt DESC LIMIT %d', $group_nid, $limit);
while ($row = db_fetch_array($result)) {
$uids[] = $row['uid'];
}
}
?>
This code seems "wrong", primarily because of DRY: it contains the same request with one minor difference. I can change this by building smarter queries.
, ( ), , db.
?