Skip to content

Commit a5dc090

Browse files
committed
增加lazy方法 惰性加载数据 关联查询统一使用lazy方法优化查询
1 parent ea2e627 commit a5dc090

File tree

8 files changed

+63
-22
lines changed

8 files changed

+63
-22
lines changed

src/db/Query.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,10 @@ public function cursor($data = null)
536536
*/
537537
public function chunk(int $count, callable $callback, string | array | null $column = null, string $order = 'asc'): bool
538538
{
539+
if ($count < 1) {
540+
throw new Exception('The chunk size should be at least 1');
541+
}
542+
539543
$options = $this->getOptions();
540544
$column = $column ?: $this->getPk();
541545

@@ -560,11 +564,15 @@ public function chunk(int $count, callable $callback, string | array | null $col
560564

561565
$resultSet = $query->order($column, $order)->select();
562566

563-
while (count($resultSet) > 0) {
567+
while (true) {
564568
if (false === call_user_func($callback, $resultSet)) {
565569
return false;
566570
}
567571

572+
if (count($resultSet) < $count) {
573+
break;
574+
}
575+
568576
if (isset($times)) {
569577
$times++;
570578
$query = $this->options($options)->page($times, $count);
@@ -582,4 +590,44 @@ public function chunk(int $count, callable $callback, string | array | null $col
582590

583591
return true;
584592
}
593+
594+
/**
595+
* 惰性分批遍历数据
596+
* @param int $count 每批处理的数量
597+
* @param string|null $column 分批处理的字段名
598+
* @param string $order 字段排序
599+
* @return \Generator
600+
*/
601+
public function lazy(int $count = 1000, ?string $column = null, string $order = 'desc')
602+
{
603+
if ($count < 1) {
604+
throw new Exception('The chunk size should be at least 1');
605+
}
606+
607+
$limit = (int)$this->getOption('limit', 0);
608+
$column = $column ?: $this->getPk();
609+
$this->order($column, $order)->limit($count > $limit ? $limit : $count);
610+
611+
$options = $this->getOptions();
612+
$bind = $this->bind;
613+
$times = 0;
614+
$resultSet = $this->options($options)->select();
615+
616+
while (true) {
617+
foreach ($resultSet as $item) {
618+
yield $item;
619+
$lastId = $item[$column];
620+
$times++;
621+
}
622+
623+
if (count($resultSet) < $count || ($limit && $times >= $limit)) {
624+
break;
625+
}
626+
627+
$resultSet = $this->options($options)
628+
->where($column, 'asc' == strtolower($order) ? '>' : '<', $lastId)
629+
->bind($bind)
630+
->select();
631+
};
632+
}
585633
}

src/model/relation/BelongsToMany.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,11 +452,10 @@ protected function eagerlyManyToMany(array $where, array $subRelation = [], ?Clo
452452
}
453453

454454
// 预载入关联查询 支持嵌套预载入
455-
$method = ($subRelation || !empty($cache)) ? 'select' : 'cursor';
456-
$list = $this->belongsToManyQuery($this->foreignKey, $this->localKey, $where)
455+
$list = $this->belongsToManyQuery($this->foreignKey, $this->localKey, $where)
457456
->with($subRelation)
458457
->cache($cache[0] ?? false, $cache[1] ?? null, $cache[2] ?? null)
459-
->$method();
458+
->lazy();
460459

461460
// 组装模型数据
462461
$data = [];

src/model/relation/HasMany.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,11 @@ protected function eagerlyOneToMany(array $where, array $subRelation = [], ?Clos
225225
}
226226
}
227227

228-
$method = ($subRelation || !empty($cache)) ? 'select' : 'cursor';
229-
$list = $this->query
228+
$list = $this->query
230229
->where($where)
231230
->cache($cache[0] ?? false, $cache[1] ?? null, $cache[2] ?? null)
232231
->with($subRelation)
233-
->$method();
232+
->lazy();
234233

235234
// 组装模型数据
236235
$data = [];

src/model/relation/HasManyThrough.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,10 @@ protected function eagerlyWhere(array $where, string $key, array $subRelation =
265265
}
266266
}
267267

268-
$method = ($subRelation || !empty($cache)) ? 'select' : 'cursor';
269-
$list = $this->query
268+
$list = $this->query
270269
->where($throughKey, 'in', $keys)
271270
->cache($cache[0] ?? false, $cache[1] ?? null, $cache[2] ?? null)
272-
->$method();
271+
->lazy();
273272

274273
// 组装模型数据
275274
$data = [];

src/model/relation/MorphMany.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,11 @@ protected function eagerlyMorphToMany(array $where, array $subRelation = [], ?Cl
307307
}
308308
}
309309

310-
$method = ($subRelation || !empty($cache)) ? 'select' : 'cursor';
311-
$list = $this->query
310+
$list = $this->query
312311
->where($where)
313312
->with($subRelation)
314313
->cache($cache[0] ?? false, $cache[1] ?? null, $cache[2] ?? null)
315-
->$method();
314+
->lazy();
316315

317316
// 组装模型数据
318317
$data = [];

src/model/relation/MorphOne.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,11 @@ protected function eagerlyMorphToOne(array $where, array $subRelation = [], ?Clo
272272
$closure($this->query);
273273
}
274274

275-
$method = ($subRelation || !empty($cache)) ? 'select' : 'cursor';
276-
$list = $this->query
275+
$list = $this->query
277276
->where($where)
278277
->with($subRelation)
279278
->cache($cache[0] ?? false, $cache[1] ?? null, $cache[2] ?? null)
280-
->$method();
279+
->lazy();
281280

282281
// 组装模型数据
283282
$data = [];

src/model/relation/MorphToMany.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,10 @@ protected function eagerlyManyToMany(array $where, array $subRelation = [], ?Clo
265265
}
266266

267267
// 预载入关联查询 支持嵌套预载入
268-
$method = ($subRelation || !empty($cache)) ? 'select' : 'cursor';
269-
$list = $this->belongsToManyQuery($this->foreignKey, $this->localKey, $where)
268+
$list = $this->belongsToManyQuery($this->foreignKey, $this->localKey, $where)
270269
->with($subRelation)
271270
->cache($cache[0] ?? false, $cache[1] ?? null, $cache[2] ?? null)
272-
->$method();
271+
->lazy();
273272

274273
// 组装模型数据
275274
$data = [];

src/model/relation/OneToOne.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,11 @@ protected function eagerlyWhere(array $where, string $key, array $subRelation =
328328
$this->query->limit(1);
329329
}
330330

331-
$method = ($subRelation || !empty($cache)) ? 'select' : 'cursor';
332-
$list = $this->query
331+
$list = $this->query
333332
->where($where)
334333
->with($subRelation)
335334
->cache($cache[0] ?? false, $cache[1] ?? null, $cache[2] ?? null)
336-
->$method();
335+
->lazy();
337336

338337
// 组装模型数据
339338
$data = [];

0 commit comments

Comments
 (0)