DBAL-93: [ORACLE] Speed improvement of mechanism for limiting query results #2172

Closed
doctrinebot opened this Issue Feb 16, 2011 · 2 comments

2 participants

@doctrinebot

Jira issue originally created by user cicovec:

Currently the mechanism for limiting query results in Oracle is suboptimal.

Doctrine uses the following pseudo SQL statement to limit query results:

SELECT b.* FROM (
   SELECT a.*, ROWNUM AS doctrine_rownum FROM (
      $query
  ) a
) b
WHERE doctrine_rownum BETWEEN $min AND $max

It's much faster to use the following:

SELECT * FROM (
  SELECT a.*, rownum AS doctrine_rownum FROM {
    $query
  ) a WHERE rownum <= $max
) WHERE doctrine_rownum >= $min

The updated code in DBAL/Platforms/OraclePlatform.php would look like this:

public function modifyLimitQuery($query, $limit, $offset = null)
{
    $limit = (int) $limit;
    $offset = (int) $offset;
    if (preg_match('/^\s*SELECT/i', $query)) {
        if ( ! preg_match('/\sFROM\s/i', $query)) {
            $query .= " FROM dual";
        }
        if ($limit > 0) {
            $max = $offset <ins> $limit;
            $column = '*';
            if ($offset > 0) {
                $min = $offset </ins> 1;
                $query = 'SELECT * FROM (SELECT a.' . $column . ', rownum AS doctrine_rownum FROM (' . 
                         $query . 
                         ') a WHERE rownum <= ' . $max . ') WHERE doctrine_rownum >= ' . $min;
            } else {
                $query = 'SELECT a.'.$column.' FROM (' . $query .') a WHERE ROWNUM <= ' . $max;
            }
        }
    }
    return $query;
}

With the current implementation the request for fairly complicated query selecting data from multiple tables one of which reaches 2 million rows takes 5.98 seconds comparing to 1.64 with the proposed one.

In addition It would be nice to have the min and max values as SQL query parameters so that Oracle's internal caching mechanisms could take part, but I'm aware of the fact that it would not be so easy to implement.

@doctrinebot

Comment created by @beberlei:

Fixed

@doctrinebot

Issue was closed with resolution "Fixed"

@beberlei beberlei was assigned by doctrinebot Dec 7, 2015
@doctrinebot doctrinebot added this to the 2.1 milestone Dec 7, 2015
@doctrinebot doctrinebot closed this Dec 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment