Documentation Home
MySQL Internals Manual Implementing the records_in_range Method

The [custom-engine.html#custom-engine-api-reference-records_in_range records_in_range()] method is called by the optimizer to assist in choosing which index on a table to use for a query or join. It is defined as follows:

ha_rows ha_foo::records_in_range(uint inx, key_range *min_key, key_range *max_key)

The inx parameter is the index to be checked. The *min_key and *max_key parameters are key_range structures that indicate the low and high ends of the range. The key_range structure looks like this:

typedef struct st_key_range
  const byte *key;
  uint length;
  key_part_map keypart_map;
  enum ha_rkey_function flag;
} key_range;

key_range members are used as follows:

  • key is a pointer to the key buffer.

  • length is the key length.

  • keypart_map is a bitmap that indicates which key parts are passed in key (as described in Parsing Key Information).

  • flag indicates whether to include the key in the range. Its value differs for min_key and max_key, as described following.

min_key.flag can have one of the following values:

  • HA_READ_KEY_EXACT - Include the key in the range

  • HA_READ_AFTER_KEY - Don't include key in range

max_key.flag can have one of the following values:

  • HA_READ_BEFORE_KEY - Don't include key in range

  • HA_READ_AFTER_KEY - Include all 'end_key' values in the range

The following return values are allowed:

  • 0 - There are no matching keys in the given range

  • number > 0 - There are approximately number matching rows in the range

  • HA_POS_ERROR - Something is wrong with the index tree

When calculating values, speed is more important than accuracy.