mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 09:35:49 +02:00
Do not use fetch join, as even with the N+1 problem the queries are faster than with the very complex and slow expressions needed for the fetch Join pagination
This commit is contained in:
parent
8ce5f4a796
commit
d59b8817c3
3 changed files with 102 additions and 13 deletions
69
src/DataTables/Adapters/FetchResultsAtOnceORMAdapter.php
Normal file
69
src/DataTables/Adapters/FetchResultsAtOnceORMAdapter.php
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published
|
||||||
|
* by the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
|
||||||
|
namespace App\DataTables\Adapters;
|
||||||
|
|
||||||
|
use App\DataTables\Events\ORMPostQueryEvent;
|
||||||
|
use Doctrine\ORM\AbstractQuery;
|
||||||
|
use Doctrine\ORM\QueryBuilder;
|
||||||
|
use Omines\DataTablesBundle\Adapter\AdapterQuery;
|
||||||
|
use Omines\DataTablesBundle\Adapter\Doctrine\Event\ORMAdapterQueryEvent;
|
||||||
|
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapter;
|
||||||
|
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapterEvents;
|
||||||
|
use Omines\DataTablesBundle\Column\AbstractColumn;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is very similar to the original ORMAdapter, but instead of getting each line one by one, it gets all the results at once,
|
||||||
|
* which can save some time in combination with fetch hints.
|
||||||
|
*/
|
||||||
|
class FetchResultsAtOnceORMAdapter extends ORMAdapter
|
||||||
|
{
|
||||||
|
protected function getResults(AdapterQuery $query): \Traversable
|
||||||
|
{
|
||||||
|
/** @var QueryBuilder $builder */
|
||||||
|
$builder = $query->get('qb');
|
||||||
|
$state = $query->getState();
|
||||||
|
|
||||||
|
// Apply definitive view state for current 'page' of the table
|
||||||
|
foreach ($state->getOrderBy() as list($column, $direction)) {
|
||||||
|
/** @var AbstractColumn $column */
|
||||||
|
if ($column->isOrderable()) {
|
||||||
|
$builder->addOrderBy($column->getOrderField(), $direction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (null !== $state->getLength()) {
|
||||||
|
$builder
|
||||||
|
->setFirstResult($state->getStart())
|
||||||
|
->setMaxResults($state->getLength())
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
$q = $builder->getQuery();
|
||||||
|
$event = new ORMAdapterQueryEvent($q);
|
||||||
|
$state->getDataTable()->getEventDispatcher()->dispatch($event, ORMAdapterEvents::PRE_QUERY);
|
||||||
|
|
||||||
|
foreach ($q->getResult() as $item) {
|
||||||
|
yield $item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,11 +22,16 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\DataTables;
|
namespace App\DataTables;
|
||||||
|
|
||||||
|
use App\DataTables\Adapters\FetchResultsAtOnceORMAdapter;
|
||||||
use App\DataTables\Column\EnumColumn;
|
use App\DataTables\Column\EnumColumn;
|
||||||
use App\Entity\Parts\ManufacturingStatus;
|
use App\Entity\Parts\ManufacturingStatus;
|
||||||
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||||
|
use Doctrine\ORM\Mapping\ClassMetadataInfo;
|
||||||
|
use Doctrine\ORM\Query;
|
||||||
|
use Omines\DataTablesBundle\Adapter\Doctrine\Event\ORMAdapterQueryEvent;
|
||||||
|
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapterEvents;
|
||||||
use Symfony\Bundle\SecurityBundle\Security;
|
use Symfony\Bundle\SecurityBundle\Security;
|
||||||
use App\Entity\Parts\Storelocation;
|
use App\Entity\Parts\Storelocation;
|
||||||
use App\DataTables\Adapters\CustomFetchJoinORMAdapter;
|
|
||||||
use App\DataTables\Column\EntityColumn;
|
use App\DataTables\Column\EntityColumn;
|
||||||
use App\DataTables\Column\IconLinkColumn;
|
use App\DataTables\Column\IconLinkColumn;
|
||||||
use App\DataTables\Column\LocaleDateTimeColumn;
|
use App\DataTables\Column\LocaleDateTimeColumn;
|
||||||
|
@ -43,12 +48,9 @@ use App\DataTables\Helpers\PartDataTableHelper;
|
||||||
use App\Entity\Parts\Part;
|
use App\Entity\Parts\Part;
|
||||||
use App\Entity\Parts\PartLot;
|
use App\Entity\Parts\PartLot;
|
||||||
use App\Services\Formatters\AmountFormatter;
|
use App\Services\Formatters\AmountFormatter;
|
||||||
use App\Services\Attachments\AttachmentURLGenerator;
|
|
||||||
use App\Services\EntityURLGenerator;
|
use App\Services\EntityURLGenerator;
|
||||||
use App\Services\Trees\NodesListBuilder;
|
|
||||||
use Doctrine\ORM\QueryBuilder;
|
use Doctrine\ORM\QueryBuilder;
|
||||||
use Omines\DataTablesBundle\Adapter\Doctrine\ORM\SearchCriteriaProvider;
|
use Omines\DataTablesBundle\Adapter\Doctrine\ORM\SearchCriteriaProvider;
|
||||||
use Omines\DataTablesBundle\Column\MapColumn;
|
|
||||||
use Omines\DataTablesBundle\Column\TextColumn;
|
use Omines\DataTablesBundle\Column\TextColumn;
|
||||||
use Omines\DataTablesBundle\DataTable;
|
use Omines\DataTablesBundle\DataTable;
|
||||||
use Omines\DataTablesBundle\DataTableTypeInterface;
|
use Omines\DataTablesBundle\DataTableTypeInterface;
|
||||||
|
@ -267,12 +269,12 @@ final class PartsDataTable implements DataTableTypeInterface
|
||||||
])
|
])
|
||||||
|
|
||||||
->addOrderBy('name')
|
->addOrderBy('name')
|
||||||
->createAdapter(CustomFetchJoinORMAdapter::class, [
|
->createAdapter(FetchResultsAtOnceORMAdapter::class, [
|
||||||
'simple_total_query' => true,
|
|
||||||
'query' => function (QueryBuilder $builder): void {
|
'query' => function (QueryBuilder $builder): void {
|
||||||
$this->getQuery($builder);
|
$this->getQuery($builder);
|
||||||
},
|
},
|
||||||
'entity' => Part::class,
|
'entity' => Part::class,
|
||||||
|
'hydrate' => Query::HYDRATE_OBJECT,
|
||||||
'criteria' => [
|
'criteria' => [
|
||||||
function (QueryBuilder $builder) use ($options): void {
|
function (QueryBuilder $builder) use ($options): void {
|
||||||
$this->buildCriteria($builder, $options);
|
$this->buildCriteria($builder, $options);
|
||||||
|
@ -280,13 +282,29 @@ final class PartsDataTable implements DataTableTypeInterface
|
||||||
new SearchCriteriaProvider(),
|
new SearchCriteriaProvider(),
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$dataTable->addEventListener(ORMAdapterEvents::PRE_QUERY, $this->preQueryEventHandler(...));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function preQueryEventHandler(ORMAdapterQueryEvent $event): void
|
||||||
|
{
|
||||||
|
$query = $event->getQuery();
|
||||||
|
|
||||||
|
//Eager fetch the associations of the part entity (we can only fetch toOne associations that way)
|
||||||
|
$query->setFetchMode(Part::class, 'category', ClassMetadataInfo::FETCH_EAGER);
|
||||||
|
$query->setFetchMode(Part::class, 'footprint', ClassMetadata::FETCH_EAGER);
|
||||||
|
$query->setFetchMode(Part::class, 'manufacturer', ClassMetadata::FETCH_EAGER);
|
||||||
|
$query->setFetchMode(Part::class, 'partUnit', ClassMetadata::FETCH_EAGER);
|
||||||
|
$query->setFetchMode(Part::class, 'master_picture_attachment', ClassMetadata::FETCH_EAGER);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getQuery(QueryBuilder $builder): void
|
private function getQuery(QueryBuilder $builder): void
|
||||||
{
|
{
|
||||||
//Distinct is very slow here, do not add this here (also I think this is not needed here, as the id column is always distinct)
|
//Distinct is very slow here, do not add this here (also I think this is not needed here, as the id column is always distinct)
|
||||||
$builder->select('part')
|
$builder
|
||||||
->addSelect('category')
|
//->distinct()
|
||||||
|
->select('part')
|
||||||
|
/*->addSelect('category')
|
||||||
->addSelect('footprint')
|
->addSelect('footprint')
|
||||||
->addSelect('manufacturer')
|
->addSelect('manufacturer')
|
||||||
->addSelect('partUnit')
|
->addSelect('partUnit')
|
||||||
|
@ -295,7 +313,7 @@ final class PartsDataTable implements DataTableTypeInterface
|
||||||
->addSelect('partLots')
|
->addSelect('partLots')
|
||||||
->addSelect('orderdetails')
|
->addSelect('orderdetails')
|
||||||
->addSelect('attachments')
|
->addSelect('attachments')
|
||||||
->addSelect('storelocations')
|
->addSelect('storelocations')*/
|
||||||
//Calculate amount sum using a subquery, so we can filter and sort by it
|
//Calculate amount sum using a subquery, so we can filter and sort by it
|
||||||
->addSelect(
|
->addSelect(
|
||||||
'(
|
'(
|
||||||
|
@ -321,8 +339,9 @@ final class PartsDataTable implements DataTableTypeInterface
|
||||||
->leftJoin('part.parameters', 'parameters')
|
->leftJoin('part.parameters', 'parameters')
|
||||||
|
|
||||||
//We have to group by all elements, or only the first sub elements of an association is fetched! (caused issue #190)
|
//We have to group by all elements, or only the first sub elements of an association is fetched! (caused issue #190)
|
||||||
->addGroupBy('part')
|
->addGroupBy('part.id')
|
||||||
->addGroupBy('partLots')
|
//->addGroupBy('part')
|
||||||
|
/*->addGroupBy('partLots')
|
||||||
->addGroupBy('category')
|
->addGroupBy('category')
|
||||||
->addGroupBy('master_picture_attachment')
|
->addGroupBy('master_picture_attachment')
|
||||||
->addGroupBy('storelocations')
|
->addGroupBy('storelocations')
|
||||||
|
@ -333,7 +352,7 @@ final class PartsDataTable implements DataTableTypeInterface
|
||||||
->addGroupBy('suppliers')
|
->addGroupBy('suppliers')
|
||||||
->addGroupBy('attachments')
|
->addGroupBy('attachments')
|
||||||
->addGroupBy('partUnit')
|
->addGroupBy('partUnit')
|
||||||
->addGroupBy('parameters')
|
->addGroupBy('parameters')*/
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\Entity\Parts;
|
namespace App\Entity\Parts;
|
||||||
|
|
||||||
|
use App\Repository\PartLotRepository;
|
||||||
use Doctrine\DBAL\Types\Types;
|
use Doctrine\DBAL\Types\Types;
|
||||||
use App\Entity\Base\AbstractDBElement;
|
use App\Entity\Base\AbstractDBElement;
|
||||||
use App\Entity\Base\TimestampTrait;
|
use App\Entity\Base\TimestampTrait;
|
||||||
|
@ -79,7 +80,7 @@ class PartLot extends AbstractDBElement implements TimeStampableInterface, Named
|
||||||
* @var Storelocation|null The storelocation of this lot
|
* @var Storelocation|null The storelocation of this lot
|
||||||
*/
|
*/
|
||||||
#[Groups(['simple', 'extended', 'full', 'import'])]
|
#[Groups(['simple', 'extended', 'full', 'import'])]
|
||||||
#[ORM\ManyToOne(targetEntity: Storelocation::class)]
|
#[ORM\ManyToOne(targetEntity: Storelocation::class, fetch: 'EAGER')]
|
||||||
#[ORM\JoinColumn(name: 'id_store_location')]
|
#[ORM\JoinColumn(name: 'id_store_location')]
|
||||||
#[Selectable()]
|
#[Selectable()]
|
||||||
protected ?Storelocation $storage_location = null;
|
protected ?Storelocation $storage_location = null;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue