Adding batching to index command for performance
This commit is contained in:
@@ -2,10 +2,12 @@
|
|||||||
|
|
||||||
namespace Pcm\SearchBundle\Command;
|
namespace Pcm\SearchBundle\Command;
|
||||||
|
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Pcm\SearchBundle\Entity\SearchIndex;
|
use Pcm\SearchBundle\Entity\SearchIndex;
|
||||||
use Pcm\SearchBundle\Service\SearchService;
|
use Pcm\SearchBundle\Service\SearchService;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Symfony\Component\Console\Helper\Table;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
use Symfony\Component\Console\Helper\ProgressBar;
|
use Symfony\Component\Console\Helper\ProgressBar;
|
||||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||||
use Symfony\Component\Console\Attribute\AsCommand;
|
use Symfony\Component\Console\Attribute\AsCommand;
|
||||||
@@ -23,7 +25,16 @@ class SearchIndexCommand extends Command
|
|||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function configure(): void {}
|
protected function configure(): void
|
||||||
|
{
|
||||||
|
$this->addOption(
|
||||||
|
'batchsize',
|
||||||
|
'b',
|
||||||
|
InputOption::VALUE_REQUIRED,
|
||||||
|
'Number of rows to process before flushing',
|
||||||
|
2000
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||||
{
|
{
|
||||||
@@ -42,29 +53,46 @@ class SearchIndexCommand extends Command
|
|||||||
$progress_bar->setProgressCharacter("<fg=green>➤</>");
|
$progress_bar->setProgressCharacter("<fg=green>➤</>");
|
||||||
$progress_bar->start();
|
$progress_bar->start();
|
||||||
|
|
||||||
|
$batchSize = $input->getOption('batchsize');
|
||||||
|
$searchEntities = null;
|
||||||
|
|
||||||
foreach ($searchables as $searchable) {
|
foreach ($searchables as $searchable) {
|
||||||
$entities = $this
|
$entities = $this
|
||||||
->em
|
->em
|
||||||
->getRepository($searchable)
|
->getRepository($searchable)
|
||||||
->findAll();
|
->findAll();
|
||||||
|
|
||||||
|
$searchEntities[] = [$searchable, count($entities)];
|
||||||
|
|
||||||
|
$this->em->clear();
|
||||||
|
$counter = 1;
|
||||||
|
|
||||||
foreach ($entities as $entity) {
|
foreach ($entities as $entity) {
|
||||||
$search_result = $this->searchService->createSearchResult($entity);
|
$search_result = $this->searchService->createSearchResult($entity);
|
||||||
|
|
||||||
$this->em->persist($search_result);
|
$this->em->persist($search_result);
|
||||||
|
|
||||||
|
if (($counter % $batchSize) === 0) {
|
||||||
|
$this->em->flush();
|
||||||
|
$this->em->clear();
|
||||||
|
}
|
||||||
|
$counter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
$progress_bar->advance();
|
$progress_bar->advance();
|
||||||
|
$this->em->flush();
|
||||||
|
$this->em->clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->em->flush();
|
|
||||||
|
|
||||||
$progress_bar->finish();
|
$progress_bar->finish();
|
||||||
|
|
||||||
$io->writeln('');
|
$table = new Table($output);
|
||||||
$io->writeln('');
|
$table
|
||||||
|
->setHeaders(['Class', 'Count'])
|
||||||
|
->setRows($searchEntities)
|
||||||
|
;
|
||||||
|
$table->render();
|
||||||
|
|
||||||
|
$io->writeln('');
|
||||||
$io->success('Index updated');
|
$io->success('Index updated');
|
||||||
|
|
||||||
return Command::SUCCESS;
|
return Command::SUCCESS;
|
||||||
|
|||||||
Reference in New Issue
Block a user