omines / datatables-bundle

DataTables bundle for Symfony
https://omines.github.io/datatables-bundle/
MIT License
252 stars 113 forks source link

Same query running twice on count doctrine #192

Closed pkly closed 3 years ago

pkly commented 3 years ago

In ORMAdapter::prepareQuery there's the following code

        $query->setTotalRows($this->getCount($builder, $identifier));

        // Get record count after filtering
        $this->buildCriteria($builder, $state);
        $query->setFilteredRows($this->getCount($builder, $identifier));

Yet, this is not quite always necessary.

        $lastDQL = $builder->getQuery()->getDQL();
        $query->setTotalRows($this->getCount($builder, $identifier));

        // Get record count after filtering
        $this->buildCriteria($builder, $state);
        $query->setFilteredRows(
            $builder->getQuery()->getDQL() === $lastDQL ?
                $query->getTotalRows() :
                $this->getCount($builder, $identifier)
        );

A simple text comparison can tell us if we've already ran a query. If yes, there's no need to run it again.

curry684 commented 3 years ago

The point is valid, however this will in general boil down to superfluous micro-optimization as MySQL/MariaDB/Postgres/MSSQL and any other database worth their salt will also note that the query is identical and return the cached result immediately. I highly doubt this optimization would ever provide measurable performance increase in exchange for making all filtered queries slightly slower (you're now building the query DQL 4 times instead of 2).

So closing this unless I get proof the optimization is worth it 😉 thnx anyway!