This blog is part of our Rails 5.2 series.
An index is used to speed up the performance of queries on a database.
Rails allows us to create index on a database column by means of a migration. By default, the sort order for the index is ascending.
But consider the case where we are fetching reports from the database. And while querying the database, we always want to get the latest report. In this case, it is efficient to specify the sort order for the index to be descending.
We can specify the sort order by adding an index to the required column by adding a migration .
If our Rails application is using postgres database, after running the above migration we can verify that the sort order was added in schema.rb
the index for
has sort order in descending.
Since the default is ascending,
the sort order
user_id is not specified
MySQL < 8.0.1
For MySQL < 8.0.1, running the above migration, would generate the following schema.rb
As we can see, although the migration runs successfully, it ignores the sort order and the default ascending order is added.
Rails 5.2 and MySQL > 8.0.1
MySQL 8.0.1 added support for descending indices.
Rails community was quick to integrate it as well. So now in Rails 5.2, we can add descending indexes for MySQL databases.
Running the above migration would lead to the same output in schema.rb file as that of the postgres one.