Skip to content

Commit

Permalink
Remove the unnecessary ORDER BY on $this->sort_column in
Browse files Browse the repository at this point in the history
get_position(), and instead apply an ORDER BY on `id` in the 2nd query
so that we have stability among the equal elements.  This should
result in cheaper (and more sensible) queries.
  • Loading branch information
bharat committed Jul 27, 2009
1 parent b3fe70e commit 7efb4b4
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions modules/gallery/models/item.php
Expand Up @@ -399,10 +399,10 @@ public function get_position($child_id) {
$db = Database::instance();
$position = $db->query("
SELECT COUNT(*) AS position FROM {items}
WHERE parent_id = {$this->id}
WHERE `parent_id` = {$this->id}
AND `{$this->sort_column}` $comp (SELECT `{$this->sort_column}`
FROM {items} WHERE id = $child_id)
ORDER BY `{$this->sort_column}` {$this->sort_order}")->current()->position;
FROM {items} WHERE `id` = $child_id)")
->current()->position;

// We stopped short of our target value in the sort (notice that we're using a < comparator
// above) because it's possible that we have duplicate values in the sort column. An
Expand All @@ -414,9 +414,10 @@ public function get_position($child_id) {
// our base value.
$result = $db->query("
SELECT id FROM {items}
WHERE parent_id = {$this->id}
WHERE `parent_id` = {$this->id}
AND `{$this->sort_column}` = (SELECT `{$this->sort_column}`
FROM {items} WHERE id = $child_id)");
FROM {items} WHERE `id` = $child_id)
ORDER BY `id` ASC");
foreach ($result as $row) {
$position++;
if ($row->id == $child_id) {
Expand Down

0 comments on commit 7efb4b4

Please sign in to comment.