Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve pagination performance #878

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Oci8/Query/Grammars/OracleGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,18 @@ protected function compileTableExpression($sql, $constraint, $query)
return "select * from ({$sql}) where rownum {$constraint}";
}

Copy link
Owner

@yajra yajra Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the codes below seems to fix 904 issues from migration to actual app execution:

Suggested change
$orders = $query->orders;
foreach($orders as $key => $order) {
if (isset($order['sql'])) {
[$column, $direction] = explode(' ', $order['sql']);
if (Str::contains($column, '.')) {
[$table, $column] = explode('.', $column);
$orders[$key]['sql'] = 't1.'.$column.' '.$direction;
}
continue;
}
if (isset($order['column']) && Str::contains($order['column'], '.')) {
[,$column] = explode('.', $order['column']);
$orders[$key]['column'] = 't1.'.$column;
}
}
// Apply ROW_NUMBER() for pagination
$orderBy = $this->compileOrders($query, $orders);

return "select t2.* from ( select rownum AS \"rn\", t1.* from ({$sql}) t1 ) t2 where t2.\"rn\" {$constraint}";
// Apply ROW_NUMBER() for pagination
$orderBy = $this->compileOrders($query, $query->orders);

// If no ORDER BY is specified, use ROWID for ordering
if (empty($orderBy)) {
$orderBy = "order by ROWID";
}

return "select * from (
select t1.*, row_number() over ({$orderBy}) as row_num
from ({$sql}) t1
) where row_num {$constraint}";
}

/**
Expand Down
Loading