Optimize product import performance
Problem Context
The product import feature is slow when importing large CSV files. A developer tried to optimize it by using batch inserts, but performance is still poor with 10,000+ products.
Advanced
200 points
File Changes (1)
src/main/java/com/example/service/ProductImportService.java
MODIFIED
@@ -15 +15 @@
| 15 | 15 | @Service |
| 16 | 16 | @RequiredArgsConstructor |
| 17 | 17 | public class ProductImportService { |
| 18 | 18 | private final ProductRepository productRepository; |
| 19 | 19 | private final CategoryRepository categoryRepository; |
| 20 | + |
|
| 21 | + @Transactional |
|
| 22 | + public ImportResult importProducts(List<ProductCSVRow> rows) { |
|
| 23 | + List<Product> products = new ArrayList<>(); |
|
| 24 | + |
|
| 25 | + for (ProductCSVRow row : rows) { |
|
| 26 | + // Lookup category for each product |
|
| 27 | + Category category = categoryRepository.findByName(row.getCategoryName()) |
|
| 28 | + .orElseThrow(() -> new IllegalArgumentException("Unknown category")); |
|
| 29 | + |
|
| 30 | + Product product = Product.builder() |
|
| 31 | + .name(row.getName()) |
|
| 32 | + .sku(row.getSku()) |
|
| 33 | + .price(row.getPrice()) |
|
| 34 | + .category(category) |
|
| 35 | + .build(); |
|
| 36 | + products.add(product); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + // Batch save all products |
|
| 40 | + productRepository.saveAll(products); |
|
| 41 | + |
|
| 42 | + return new ImportResult(products.size(), 0); |
|
| 43 | + } |
|
| 20 | 44 | } |
Login Required: You must be registered to submit reviews and receive AI feedback.
Register or
login to start reviewing!
Your Review
Review Tips
- Look for security vulnerabilities (SQL injection, XSS, etc.)
- Check for null pointer exceptions and error handling
- Consider performance implications
- Evaluate code maintainability and readability
- Check for proper resource management
- Look for logic errors or edge cases