[REFACTOR] 優化資料庫查詢效能:在多個 Service 與 Controller 中加入 select 欄位限制,並新增租戶資料表索引 Migration。
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
<?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
|
||||
{
|
||||
// 1. warehouses (倉庫)
|
||||
Schema::table('warehouses', function (Blueprint $table) {
|
||||
$table->index('type');
|
||||
});
|
||||
|
||||
// 2. categories (分類)
|
||||
Schema::table('categories', function (Blueprint $table) {
|
||||
$table->index('is_active');
|
||||
});
|
||||
|
||||
// 3. products (商品/原物料)
|
||||
Schema::table('products', function (Blueprint $table) {
|
||||
// is_active was added in a later migration, need to make sure column exists before indexing
|
||||
// Same for brand if not added at start (but brand is in the create migration)
|
||||
if (Schema::hasColumn('products', 'is_active')) {
|
||||
$table->index('is_active');
|
||||
}
|
||||
$table->index('brand');
|
||||
});
|
||||
|
||||
// 4. recipes (配方/BOM)
|
||||
Schema::table('recipes', function (Blueprint $table) {
|
||||
$table->index('is_active');
|
||||
});
|
||||
|
||||
// 5. inventory_transactions (庫存異動紀錄)
|
||||
Schema::table('inventory_transactions', function (Blueprint $table) {
|
||||
$table->index('type');
|
||||
$table->index('created_at');
|
||||
});
|
||||
|
||||
// 6. purchase_orders (採購單)
|
||||
Schema::table('purchase_orders', function (Blueprint $table) {
|
||||
$table->index('status');
|
||||
$table->index('expected_delivery_date');
|
||||
$table->index('created_at');
|
||||
});
|
||||
|
||||
// 7. production_orders (生產工單)
|
||||
Schema::table('production_orders', function (Blueprint $table) {
|
||||
$table->index('status');
|
||||
$table->index('created_at');
|
||||
});
|
||||
|
||||
// 8. sales_orders (門市/銷售單)
|
||||
Schema::table('sales_orders', function (Blueprint $table) {
|
||||
$table->index('status');
|
||||
$table->index('sold_at');
|
||||
$table->index('created_at');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('warehouses', function (Blueprint $table) {
|
||||
$table->dropIndex(['type']);
|
||||
});
|
||||
|
||||
Schema::table('categories', function (Blueprint $table) {
|
||||
$table->dropIndex(['is_active']);
|
||||
});
|
||||
|
||||
Schema::table('products', function (Blueprint $table) {
|
||||
if (Schema::hasColumn('products', 'is_active')) {
|
||||
$table->dropIndex(['is_active']);
|
||||
}
|
||||
$table->dropIndex(['brand']);
|
||||
});
|
||||
|
||||
Schema::table('recipes', function (Blueprint $table) {
|
||||
$table->dropIndex(['is_active']);
|
||||
});
|
||||
|
||||
Schema::table('inventory_transactions', function (Blueprint $table) {
|
||||
$table->dropIndex(['type']);
|
||||
$table->dropIndex(['created_at']);
|
||||
});
|
||||
|
||||
Schema::table('purchase_orders', function (Blueprint $table) {
|
||||
$table->dropIndex(['status']);
|
||||
$table->dropIndex(['expected_delivery_date']);
|
||||
$table->dropIndex(['created_at']);
|
||||
});
|
||||
|
||||
Schema::table('production_orders', function (Blueprint $table) {
|
||||
$table->dropIndex(['status']);
|
||||
$table->dropIndex(['created_at']);
|
||||
});
|
||||
|
||||
Schema::table('sales_orders', function (Blueprint $table) {
|
||||
$table->dropIndex(['status']);
|
||||
$table->dropIndex(['sold_at']);
|
||||
$table->dropIndex(['created_at']);
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user