Created
January 3, 2026 17:25
-
-
Save moaalaa/37242bc3c2057cfcdc234e5ff7a85f9a to your computer and use it in GitHub Desktop.
Indexing Order and explaining
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use App\Enums\DefaultStatus; | |
| use App\Enums\OrganizationTypes; | |
| use Illuminate\Database\Migrations\Migration; | |
| use Illuminate\Database\Schema\Blueprint; | |
| use Illuminate\Support\Facades\Schema; | |
| return new class extends Migration | |
| { | |
| /** | |
| * Run the migrations. | |
| */ | |
| public function up(): void | |
| { | |
| Schema::create('organizations', function (Blueprint $table) { | |
| $table->uuid('id')->primary(); | |
| $table->json('name')->nullable(); | |
| $table->json('slug')->nullable(); | |
| $table->json('overview')->nullable(); | |
| $table->json('social_media_links_metadata')->nullable(); | |
| $table->string('type')->default(OrganizationTypes::Hospital->value); | |
| $table->string('status')->default(DefaultStatus::Active->value); | |
| $table->unsignedInteger('average_rate')->default(0); | |
| $table->foreignUuid('creator_id')->nullable()->references('id')->on('users')->nullOnDelete(); | |
| $table->foreignUuid('owner_id')->nullable()->references('id')->on('users')->nullOnDelete(); | |
| $table->softDeletes(); | |
| $table->timestamps(); | |
| // Column order in composite indexes MATTERS. | |
| // Rule: | |
| // Equality filters first → sorting column → LIMIT | |
| // Should status come before or after type? | |
| // Use this rule: | |
| // Put the most selective column first | |
| // Typical case: | |
| // type → 4 values | |
| // status → 2 values (active/inactive) | |
| // Either works, but type first is usually fine. | |
| $table->index(['type', 'created_at']); | |
| $table->index(['type', 'average_rate']); | |
| $table->index(['type', 'status', 'created_at']); | |
| $table->index(['type', 'status', 'average_rate']); | |
| }); | |
| } | |
| /** | |
| * Reverse the migrations. | |
| */ | |
| public function down(): void | |
| { | |
| Schema::dropIfExists('organizations'); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment