-
Notifications
You must be signed in to change notification settings - Fork 1
⚡ Optimize Product Import N+1 SKU Check #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
sayuru-akash
wants to merge
1
commit into
main
from
performance/optimize-product-import-sku-check-1392367880616500587
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Feature; | ||
|
|
||
| use Tests\TestCase; | ||
| use App\Models\User; | ||
| use App\Models\Category; | ||
| use App\Models\SubCategory; | ||
| use App\Models\Unit; | ||
| use App\Models\Product; | ||
| use App\Models\ProductVariant; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Illuminate\Support\Facades\DB; | ||
|
|
||
| class ProductImportPerformanceTest extends TestCase | ||
| { | ||
| use RefreshDatabase; | ||
|
|
||
| public function test_import_performance() | ||
| { | ||
| // Setup | ||
| $user = User::factory()->create(); | ||
| $this->actingAs($user); | ||
|
|
||
| $category = Category::create(['name' => 'Test Cat', 'code' => 'TC']); | ||
| $subCategory = SubCategory::create(['name' => 'Test Sub', 'category_id' => $category->id, 'code' => 'TS']); | ||
| $unit = Unit::create(['name' => 'Test Unit', 'short_name' => 'TU']); | ||
|
|
||
| // Seed some existing data to simulate a real-world scenario where check is necessary | ||
| $existingCount = 50; | ||
| for ($i = 0; $i < $existingCount; $i++) { | ||
| $p = Product::create([ | ||
| 'name' => "Existing Product $i", | ||
| 'category_id' => $category->id, | ||
| 'sub_category_id' => $subCategory->id, | ||
| 'description' => 'Test', | ||
| ]); | ||
| ProductVariant::create([ | ||
| 'product_id' => $p->id, | ||
| 'unit_id' => $unit->id, | ||
| 'sku' => "SKU-$i", | ||
| 'selling_price' => 100, | ||
| 'quantity' => 10, | ||
| 'unit_value' => 1, | ||
| ]); | ||
| } | ||
|
|
||
| // Prepare import data | ||
| $importCount = 500; | ||
| $previewData = []; | ||
|
|
||
| for ($i = 0; $i < $importCount; $i++) { | ||
| // First 50 are duplicates (SKU-0 to SKU-49) | ||
| // Next are new | ||
| $sku = "SKU-$i"; | ||
|
|
||
| $previewData[] = [ | ||
| 'row_id' => $i, | ||
| 'name' => "Import Product $i", | ||
| 'category_id' => $category->id, | ||
| 'sub_category_id' => $subCategory->id, | ||
| 'description' => 'Desc', | ||
| 'unit_id' => $unit->id, | ||
| 'unit_value' => 1, | ||
| 'sku' => $sku, | ||
| 'selling_price' => 120, | ||
| 'limit_price' => 110, | ||
| 'quantity' => 20, | ||
| 'alert_quantity' => 5, | ||
| 'image_url' => null, | ||
| 'errors' => [] | ||
| ]; | ||
| } | ||
|
|
||
| session(['product_import_preview_data' => $previewData]); | ||
|
|
||
| // Enable query logging to count queries | ||
| DB::enableQueryLog(); | ||
|
|
||
| $startTime = microtime(true); | ||
|
|
||
| $response = $this->post(route('products.import.store')); | ||
|
|
||
| $endTime = microtime(true); | ||
| $duration = $endTime - $startTime; | ||
| $queryLog = DB::getQueryLog(); | ||
| $queryCount = count($queryLog); | ||
|
|
||
| echo "\nPerformance Results:\n"; | ||
| echo "Time: " . number_format($duration, 4) . " seconds\n"; | ||
| echo "Queries: " . $queryCount . "\n"; | ||
|
|
||
| // Assert redirect to confirm no crash | ||
| $response->assertRedirect(route('products.index')); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$existingSkusis initialized once before the transaction and never updated after a new variant is created, so later rows rely on stale duplicate information. If another writer inserts one of the pending SKUs during this import (or duplicated preview data reaches this action),isset($existingSkus[...])will miss it andcreate()will hit the uniqueskuconstraint, rolling back the whole transaction instead of skipping just the conflicting row as the previous per-row existence check could for later rows.Useful? React with 👍 / 👎.