سؤال

I'm constructing my migration in and would like two fields to have an index for performance reasons as several joins are performed on these fields.

http://laravel.com/docs/schema#adding-indexes

These two fields also happen to be unique together so I need a unique constraints index. In the laravel doc the unique and the index are both under the same section 'indexes' so I was wondering if they are essentially the same in the background. Would a unique index automatically give me the index performance benefit?

If I set a unique constraints index

$table->unique(array('object_type', 'object_id')); 

question 1) will this achieve the same benefits as a basic index

$table->index('object_type');
$table->index('object_id');

question 2: do I need to set the engine type as InnoDB when I set these types of index?

Schema::create('my_table', function (Blueprint $table) {
  $table->increments('id');

  $table->string('object_type');
  $table->unsignedInteger('object_id');

  $table->engine = 'InnoDB';
  $table->unique(array('object_type', 'object_id'));
});

لا يوجد حل صحيح

نصائح أخرى

The performance benefit from an index for a particular query depends on the search parameters (aka where clause).

A single index with object_type and object_id together (and in this order) would help queries that specify object_type; or object_type and object_id in the where clause. It would not help if object_type is not provided.

Two indexes - one on object_type, other on object_id, would individually help based on the existence of the corresponding column in the where clause. If a where clause specifies both object_type and object_id, specificity of values would determine which of the two indexes are picked up for your query.

First up, even though you're using the Schema builder to create your migrations, these are more mysql and sql based questions, so I've tagged them as such and you might get some more insightful answers.

On to your questions:

1) No. If you want to have individual indexes on the fields, you'll also need to index them separately. The combined unique index you've got will only come into play when being used with both fields combined.

2) You don't have to use InnoDB if you don't want for your indexes. MyISAM still supports indexxes and uniques.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top