How to add a database index to a base field Goal:

Add a database index to a 'created' field.

Given:

There is a custom entity with a base field created.
The field is stored the entity table. 
Field source code of the created field:

$fields['created'] = BaseFieldDefinition::create('created')
  -&gt;setLabel(t('Created'))
  -&gt;setDescription(t('The time that the entity was created.'))
  -&gt;setDisplayOptions('view', [
    'label' =&gt; 'hidden',
    'type' =&gt; 'timestamp',
    'weight' =&gt; 3,
    'settings' =&gt; [
      'date_format' =&gt; 'medium',
      'custom_date_format' =&gt; '',
      'timezone' =&gt; '',
    ],
  ])
  -&gt;setDisplayConfigurable('view', TRUE);


What I researched:

I have not found a way to add index using the \Drupal\Core\Field\BaseFieldDefinition().

As I understand indexes are defined in a @FieldType plugin. The created field is implemented via the \Drupal\Core\Field\Plugin\Field\FieldType\TimestampItem() plugin and obviously does not have have indexes there.

P.S. I know, that I can write an updatescript to add an index to the table manually.
Currently the only way to do that is to create your own storage_schema entity handler.

See Node as an example, the annotation:

*     "storage_schema" = "Drupal\node\NodeStorageSchema",


There you can alter both the schema for a while table, or you can do it for a specific field. NodeStorageSchema does both:

  /**
   * {@inheritdoc}
   */
  protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) {
    $schema = parent::getEntitySchema($entity_type, $reset);

    $schema['node_field_data']['indexes'] += array(
      'node__frontpage' =&gt; array('promote', 'status', 'sticky', 'created'),
      'node__status_type' =&gt; array('status', 'type', 'nid'),
      'node__title_type' =&gt; array('title', array('type', 4)),
    );

    return $schema;
  }


And below in getSharedTableFieldSchema():

if ($table_name == 'node_revision') {
  switch ($field_name) {
    case 'langcode':
      $this-&gt;addSharedTableFieldIndex($storage_definition, $schema, TRUE);
      break;

    case 'revision_uid':
      $this-&gt;addSharedTableFieldForeignKey($storage_definition, $schema, 'users', 'uid');
      break;
  }
}