我有一个名为“产品”的模型 我有一个名为 Note_voucher_line 的模型
这是产品内部的关系
public function get_note_voucher_lines()
{
return $this->hasMany('App\Models\Note_voucher_line','product_id','id')->orderBy('date','asc')->orderBy('note_voucher_id','asc');
}
现在有时我必须循环像这样的代码的产品
$products = Product::whereBetween('id',[$num1,$num2])->get();
foreach($products as $product)
{
$lines = $product['get_note_voucher_lines'];
// when i use this relation it tack long long time
}
模型 Note_voucher_line 拥有超过 300k 行
我在 migration 上有索引
这是note_voucher_lines内部的索引迁移
Schema::table('note_voucher_lines', function($table) {
$table->foreign('note_voucher_id')->references('id')->on('note_vouchers');
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('journal_entry_id')->references('id')->on('journal_entries');
$table->foreign('warehouse_id')->references('id')->on('warehouses');
$table->foreign('product_id')->references('id')->on('products');
$table->foreign('cost_center_id')->references('id')->on('cost_centers');
$table->foreign('unit_id')->references('id')->on('units');
$table->foreign('is_it_bonus')->references('id')->on('status');
$table->foreign('note_voucher_type_id')->references('id')->on('note_voucher_types');
$table->foreign('posting_state_id')->references('id')->on('posting_status');
$table->foreign('product_total_quantity_id')->references('id')->on('product_total_quantitys');
$table->foreign('is_componentable')->references('id')->on('status');
$table->foreign('approved_state_id')->references('id')->on('approval_status');
$table->foreign('currency_id')->references('id')->on('currencies');
$table->foreign('branch_id')->references('id')->on('branches');
$table->foreign('created_by')->references('id')->on('users');
$table->foreign('deleted_by')->references('id')->on('users');
});
产品表中有一个名为product_id的索引 这里有任何帮助可以使它更快 谢谢
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
在这种情况下,急切地加载你们的关系会很有帮助。
$products = Product::whereBetween('id',[$num1,$num2])->with('get_note_voucher_lines')->get(); foreach($products as $product) { $lines = $product->get_note_voucher_lines; // This should be faster and note that this is the right way to fetch laravel relation not as an array }这里发生的情况是,我们使用
with()方法在每次迭代中从note_voucher_lines表预加载 2k 行(假设您的产品表包含 2k 行),而不是之前加载的一行。这减少了对数据库服务器进行的网络调用次数,现在不再进行 300k 次调用,而是进行 300k/2k 次调用。注意:您还应该考虑对您的产品使用块加载,以避免随着数据的持续增长而达到内存限制。 https://laravel.com/docs/10.x/eloquent#chunking-结果
延迟的主要原因似乎是 get_note_voucher_lines 关系的延迟加载。
每次您在循环内访问此关系时,Laravel 都会进行单独的查询来获取相关行。这就是所谓的 N+1 问题。
为了缓解这种情况,请使用预先加载:
$products = Product::whereBetween('id',[$num1,$num2])->with('get_note_voucher_lines')->get();您还可以使用分块来处理大数据:
Product::whereBetween('id',[$num1,$num2])->with('get_note_voucher_lines')->chunk(100, function ($products) { foreach ($products as $product) { $lines = $product->get_note_voucher_lines; }});确保 id 字段上有索引。您提到有一个索引,但请确保它是一个正确的索引,而不仅仅是外键约束。