perf: 编写并执行数据库迁移,为 messages 表添加 (room_id, sent_at) 联合索引以消除慢查询
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 文件功能:为消息表(messages)添加 (room_id, sent_at) 联合复合索引
|
||||
* 解决拉取房间聊天历史消息时的 Slow Query 瓶颈
|
||||
*
|
||||
* @author ChatRoom Laravel
|
||||
* @version 1.0.0
|
||||
*/
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* 执行迁移,添加联合索引
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('messages', function (Blueprint $table) {
|
||||
// 添加复合索引 idx_room_sent_at
|
||||
$table->index(['room_id', 'sent_at'], 'idx_room_sent_at');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 回滚迁移,删除联合索引
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('messages', function (Blueprint $table) {
|
||||
$table->dropIndex('idx_room_sent_at');
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user