<?php
use Migrations\AbstractMigration;

class UserLangIndex extends AbstractMigration
{
    public function up()
    {
        $table = $this->table('sentences');
        $table->addIndex(['user_id', 'lang'], ['name' => 'user_lang_idx'])
              ->removeIndexByName('user_id')
              ->update();
    }

    public function down()
    {
        $table = $this->table('sentences');
        $table->addIndex(['user_id'], ['name' => 'user_id'])
              ->removeIndexByName('user_lang_idx')
              ->update();
    }
}

Optimize querying sentences owned by a given user
Optimize querying sentences owned by a given user

While sentence queries generated by /sentences/of_user/xxx
are fine, queries generated by /sentences/of_user/xxx/yyy
can result in very poor performance, hitting PHP script
execution timeout. This especially happens when user xxx
owns less than 10 sentences in language yyy, but a lot of
sentences in other languages. In this case, mysql needs to
go through all xxx’s sentences just to be sure xxx owns
less than 10 sentences in yyy. To make things worse, the
interface currently makes it quite easy to fall in this
case because the language dropdown does not limit choices
to languages in which xxx owns sentences (making it likely
to select a language in which xxx owns no sentences).

Closes #2630.
