Database indexes¶

Indexing tables allow you to decrease fetching time from the database.

As an example, let’s take a look at the customers’ list.

The default index page is sorted by registration date, to create a table index all you need to do is modify AppEntityCustomerCustomer entity and add the index using annotations.

In this file, add indexes attribute into the table configuration:

/**
* @ORM\Table(name="sylius_customer", indexes={@ORM\Index(name="created_at_index", columns={"created_at"})})
*/

Your class should now look like this:

<?php

declare(strict_types=1);

namespace App\Entity\Customer;

use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Core\Model\Customer as BaseCustomer;

/**
 * @ORM\Entity
 * @ORM\Table(name="sylius_customer",indexes={@ORM\Index(name="created_at_index", columns={"created_at"})})
 */
class Customer extends BaseCustomer
{
}

Note

You can learn more here about ORM annotations

This should be considered for the most common sorting in your application.

Note

Using this solution you can increase speed of customer listing by around 10%. Indexes should be used when working with huge tables, otherwise it doesnt really affect loading times.