github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/xform/rules/limit.opt (about)

     1  # =============================================================================
     2  # limit.opt contains exploration rules for the Limit and Offset operators.
     3  # =============================================================================
     4  
     5  # GenerateLimitedScans generates a set of limited Scan operators, one for each
     6  # matching index on the scanned table. If the secondary index cannot provide all
     7  # the output columns, an IndexJoin is introduced to supply them. Pushing a limit
     8  # into Scan operators can substantially reduce the cost of execution, as rows
     9  # are never fetched to begin with, rather than fetched only to be discarded by
    10  # a Limit operator.
    11  [GenerateLimitedScans, Explore]
    12  (Limit
    13      (Scan $scanPrivate:* & (IsCanonicalScan $scanPrivate))
    14      (Const $limit:* & (IsPositiveInt $limit))
    15      $ordering:*
    16  )
    17  =>
    18  (GenerateLimitedScans $scanPrivate $limit $ordering)
    19  
    20  # PushLimitIntoConstrainedScan constructs a new Scan operator that adds a hard
    21  # row limit to an existing Scan operator that already has a constraint
    22  # associated with it (added by the ConstrainScans rule). The Scan operator
    23  # always applies the limit after any constraint.
    24  [PushLimitIntoConstrainedScan, Explore]
    25  (Limit
    26      (Scan $scanPrivate:*)
    27      (Const $limit:* & (IsPositiveInt $limit))
    28      $ordering:* &
    29          (CanLimitConstrainedScan $scanPrivate $ordering)
    30  )
    31  =>
    32  (Scan (LimitScanPrivate $scanPrivate $limit $ordering))
    33  
    34  # PushLimitIntoIndexJoin pushes a limit through an index join and constructs a
    35  # new Scan operator that incorporates it. Since index lookup can be expensive,
    36  # it's always better to discard rows beforehand.
    37  #
    38  # TODO(radu): we can similarly push Offset too.
    39  [PushLimitIntoIndexJoin, Explore]
    40  (Limit
    41      (IndexJoin (Scan $scanPrivate:*) $indexJoinPrivate:*)
    42      (Const $limit:* & (IsPositiveInt $limit))
    43      $ordering:* &
    44          (CanLimitConstrainedScan $scanPrivate $ordering)
    45  )
    46  =>
    47  (IndexJoin
    48      (Scan (LimitScanPrivate $scanPrivate $limit $ordering))
    49      $indexJoinPrivate
    50  )