feat: add performance indexes to optimize database queries

This commit is contained in:
idevakk
2025-11-14 03:41:46 -08:00
parent ae795880ed
commit d9291f06eb
2 changed files with 210 additions and 1 deletions

View File

@@ -0,0 +1,208 @@
<?php
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::table('users', function (Blueprint $table): void {
$table->index('email_verified_at', 'users_email_verified_at_index');
$table->index(['level', 'email'], 'users_level_email_index');
});
Schema::table('messages', function (Blueprint $table): void {
$table->index('from', 'messages_from_index');
$table->index('to', 'messages_to_index');
$table->index('is_seen', 'messages_is_seen_index');
$table->index(['from', 'to'], 'messages_from_to_index');
$table->index('created_at', 'messages_created_at_index');
});
Schema::table('emails', function (Blueprint $table): void {
$table->index('from_email', 'emails_from_email_index');
$table->index('timestamp', 'emails_timestamp_index');
$table->index('is_seen', 'emails_is_seen_index');
$table->index('is_flagged', 'emails_is_flagged_index');
$table->index('mailbox', 'emails_mailbox_index');
$table->index(['from_email', 'timestamp'], 'emails_from_email_timestamp_index');
$table->index(['is_seen', 'timestamp'], 'emails_is_seen_timestamp_index');
});
Schema::table('premium_emails', function (Blueprint $table): void {
$table->index('user_id', 'premium_emails_user_id_index');
$table->index('from_email', 'premium_emails_from_email_index');
$table->index('timestamp', 'premium_emails_timestamp_index');
$table->index('is_seen', 'premium_emails_is_seen_index');
$table->index('is_flagged', 'premium_emails_is_flagged_index');
$table->index('mailbox', 'premium_emails_mailbox_index');
$table->index(['user_id', 'timestamp'], 'premium_emails_user_id_timestamp_index');
$table->index(['user_id', 'is_seen'], 'premium_emails_user_id_is_seen_index');
});
Schema::table('categories', function (Blueprint $table): void {
$table->index('slug', 'categories_slug_index');
$table->index('is_active', 'categories_is_active_index');
$table->index(['is_active', 'slug'], 'categories_is_active_slug_index');
});
Schema::table('blogs', function (Blueprint $table): void {
$table->index('slug', 'blogs_slug_index');
$table->index('is_published', 'blogs_is_published_index');
$table->index(['category_id', 'is_published'], 'blogs_category_id_published_index');
$table->index(['is_published', 'created_at'], 'blogs_published_created_at_index');
});
Schema::table('usage_logs', function (Blueprint $table): void {
$table->index('user_id', 'usage_logs_user_id_index');
$table->index('ip_address', 'usage_logs_ip_address_index');
$table->index('created_at', 'usage_logs_created_at_index');
$table->index(['user_id', 'created_at'], 'usage_logs_user_id_created_at_index');
});
Schema::table('logs', function (Blueprint $table): void {
$table->index('user_id', 'logs_user_id_index');
$table->index('email', 'logs_email_index');
$table->index('ip', 'logs_ip_index');
$table->index('created_at', 'logs_created_at_index');
$table->index(['user_id', 'created_at'], 'logs_user_id_created_at_index');
$table->index(['email', 'created_at'], 'logs_email_created_at_index');
});
Schema::table('tickets', function (Blueprint $table): void {
$table->index('user_id', 'tickets_user_id_index');
$table->index('status', 'tickets_status_index');
$table->index('last_response_at', 'tickets_last_response_at_index');
$table->index(['user_id', 'status'], 'tickets_user_id_status_index');
$table->index(['status', 'last_response_at'], 'tickets_status_last_response_index');
});
Schema::table('ticket_responses', function (Blueprint $table): void {
$table->index('ticket_id', 'ticket_responses_ticket_id_index');
$table->index('user_id', 'ticket_responses_user_id_index');
$table->index('created_at', 'ticket_responses_created_at_index');
$table->index(['ticket_id', 'created_at'], 'ticket_responses_ticket_id_created_at_index');
});
Schema::table('activation_keys', function (Blueprint $table): void {
$table->index('user_id', 'activation_keys_user_id_index');
$table->index('is_activated', 'activation_keys_is_activated_index');
$table->index('price_id', 'activation_keys_price_id_index');
$table->index(['user_id', 'is_activated'], 'activation_keys_user_id_activated_index');
$table->index(['is_activated', 'created_at'], 'activation_keys_activated_created_at_index');
});
Schema::table('settings', function (Blueprint $table): void {
$table->index('key', 'settings_key_index');
});
Schema::table('metas', function (Blueprint $table): void {
$table->index('key', 'metas_key_index');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table): void {
$table->dropIndex('users_email_verified_at_index');
$table->dropIndex('users_level_email_index');
});
Schema::table('messages', function (Blueprint $table): void {
$table->dropIndex('messages_from_index');
$table->dropIndex('messages_to_index');
$table->dropIndex('messages_is_seen_index');
$table->dropIndex('messages_from_to_index');
$table->dropIndex('messages_created_at_index');
});
Schema::table('emails', function (Blueprint $table): void {
$table->dropIndex('emails_from_email_index');
$table->dropIndex('emails_timestamp_index');
$table->dropIndex('emails_is_seen_index');
$table->dropIndex('emails_is_flagged_index');
$table->dropIndex('emails_mailbox_index');
$table->dropIndex('emails_from_email_timestamp_index');
$table->dropIndex('emails_is_seen_timestamp_index');
});
Schema::table('premium_emails', function (Blueprint $table): void {
$table->dropIndex('premium_emails_user_id_index');
$table->dropIndex('premium_emails_from_email_index');
$table->dropIndex('premium_emails_timestamp_index');
$table->dropIndex('premium_emails_is_seen_index');
$table->dropIndex('premium_emails_is_flagged_index');
$table->dropIndex('premium_emails_mailbox_index');
$table->dropIndex('premium_emails_user_id_timestamp_index');
$table->dropIndex('premium_emails_user_id_is_seen_index');
});
Schema::table('categories', function (Blueprint $table): void {
$table->dropIndex('categories_slug_index');
$table->dropIndex('categories_is_active_index');
$table->dropIndex('categories_is_active_slug_index');
});
Schema::table('blogs', function (Blueprint $table): void {
$table->dropIndex('blogs_slug_index');
$table->dropIndex('blogs_is_published_index');
$table->dropIndex('blogs_category_id_published_index');
$table->dropIndex('blogs_published_created_at_index');
});
Schema::table('usage_logs', function (Blueprint $table): void {
$table->dropIndex('usage_logs_user_id_index');
$table->dropIndex('usage_logs_ip_address_index');
$table->dropIndex('usage_logs_created_at_index');
$table->dropIndex('usage_logs_user_id_created_at_index');
});
Schema::table('logs', function (Blueprint $table): void {
$table->dropIndex('logs_user_id_index');
$table->dropIndex('logs_email_index');
$table->dropIndex('logs_ip_index');
$table->dropIndex('logs_created_at_index');
$table->dropIndex('logs_user_id_created_at_index');
$table->dropIndex('logs_email_created_at_index');
});
Schema::table('tickets', function (Blueprint $table): void {
$table->dropIndex('tickets_user_id_index');
$table->dropIndex('tickets_status_index');
$table->dropIndex('tickets_last_response_at_index');
$table->dropIndex('tickets_user_id_status_index');
$table->dropIndex('tickets_status_last_response_index');
});
Schema::table('ticket_responses', function (Blueprint $table): void {
$table->dropIndex('ticket_responses_ticket_id_index');
$table->dropIndex('ticket_responses_user_id_index');
$table->dropIndex('ticket_responses_created_at_index');
$table->dropIndex('ticket_responses_ticket_id_created_at_index');
});
Schema::table('activation_keys', function (Blueprint $table): void {
$table->dropIndex('activation_keys_user_id_index');
$table->dropIndex('activation_keys_is_activated_index');
$table->dropIndex('activation_keys_price_id_index');
$table->dropIndex('activation_keys_user_id_activated_index');
$table->dropIndex('activation_keys_activated_created_at_index');
});
Schema::table('settings', function (Blueprint $table): void {
$table->dropIndex('settings_key_index');
});
Schema::table('metas', function (Blueprint $table): void {
$table->dropIndex('metas_key_index');
});
}
};