E
E
Eugene2018-02-20 15:36:24
MySQL
Eugene, 2018-02-20 15:36:24

How to create MySQL indexes in Laravel?

Hello!
I create a table of tags

public function up()
    {
        Schema::create('tags', function (Blueprint $table) {
            $table->increments('tag_id');
            $table->integer('c_id');
            $table->string('tag_name');
            $table->text('tag_description')->nullable();
            $table->string('tag_color')->default('#ffffff');
            
            $table->index(['c_id', 'tag_id']);
        });
    }

Migration creates indexes for tag_id and for c_id, however, when added, data is entered into the table sorted by tag_id (aka primary key), but it should be by c_id.
I make a request (in the table there are 7 records for an example) The rows value shows 7, do I understand correctly that the index did not work? Please tell me how to correctly specify indexes in Laravel. PS: that is, I want to get at the output that the data sorted by c_id would be entered. Now the data is entered like this: You need to get it like this:
EXPLAIN SELECT * FROM tags WHERE c_id=1
5a8c1876eef64606804195.png
5a8c18877e0c4523411017.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Antonio Solo, 2018-02-20
@nimfai

if you need to have data sorted by c_id specify in the selection `order by c_id`
the presence of indexes in itself does not determine the sort order in the selection.
data is entered and stored without any sorting. if you write without deletions, then the data will lie in the natural order of writing, if with deletions, no one knows what will happen here

I
Ivan, 2018-02-20
@HunterNNm

I'll just write this:
$table->increments('tag_id');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question