Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Massive performance issue with setParameter() and DateTime* objects #8113

Closed
maks-rafalko opened this issue Apr 17, 2020 · 2 comments
Closed

Comments

@maks-rafalko
Copy link
Contributor

maks-rafalko commented Apr 17, 2020

Bug Report

Q A
BC Break yes/no
Version 2.7.2

Related doc PR to warn users: #8114

Summary

Using $qb->setParameter('date', new \DateTimeImmutable()) without specifying 3rd argument (the type) leads to massive performance issues in long running processes.

This is a follow up for #7527 (originally posted here #7471 (comment))

Current behavior

As it's stated in the official docs (binding parameters to query)

Calling setParameter() automatically infers which type you are setting as value. This works for integers, arrays of strings/integers, DateTime instances and for managed entities.

So you can just pass a datetime object and it will work:

$qb->setParameter('date', new \DateTimeImmutable())

However, this leads to a) search this metadata in a cache, it fails and then b) load class metadata for every parameter of date types (e.g. \DateTimeImmutable), and such metadata does not exist of course.

Performance impact

We have a consumer that continuously reads data from the queue and executes queries, passing datetime object to setParameter().

Consumer is able to handle ~250 messages per secong, producing thousands of queries per second when it is fresh / is just started.

However, after some time there is a massive performance degradation (from 250/s, 230/s, ... 100/s, ... and so on). And this is exactly because of all the code that is executed just to load metadata for \DateTimeImmutable class which never exist).

So there are thousands of cache misses, thrown but caught exceptions, attempts to load metadata.

After changing this

$qb->setParameter('date', new \DateTimeImmutable())

to this

use Doctrine\DBAL\Types\Types;

$qb->setParameter('date', new \DateTimeImmutable(), Types::DATE_IMMUTABLE)

we are no longer experiencing performance issues. After 30 minutes of working, consumer still handles ~250 records per second without degradation.

How to reproduce

Just create any entity with datetime / datetime_immutable mapped property.

$em->getRepository(User::class)->createQueryBuilder('u');

$qb->select('u')
        ->where('u.createdAt > :now')
        ->setParameter(
            'now',
            new \DateTimeImmutable()
            // ,Types::DATE_IMMUTABLE uncomment to fix performance issue
        )
        ->getQuery()
        ->getResult();

Expected behavior

Expected behavior would be to not try loading metadata for datetime objects, but return the type immediately somewhere here:

if ($parameter->typeWasSpecified()) {
return [$parameter->getValue(), $parameter->getType()];
}

before trying to load metadata.

At this point, $parameter already have datetime type inside:

datetime

@maks-rafalko
Copy link
Contributor Author

For maintainers: seems like this should be marked as won't fix according to Marco's answers in #8115

@bastnic
Copy link

bastnic commented Apr 19, 2020

FYI, the root issue happens in doctrine/persistence, see doctrine/persistence#96

orocrmdeployer pushed a commit to oroinc/resource-library that referenced this issue May 20, 2020
…data lookups (#27580)

- to prevent the issue described in doctrine/orm#8113

Co-authored-by: Mykhailo Sulyma <[email protected]>
inri13666 pushed a commit to oro-flex/batch-bundle that referenced this issue Dec 28, 2021
…data lookups (#27579)

- to prevent the issue described in doctrine/orm#8113
inri13666 pushed a commit to oro-flex/message-queue-bundle that referenced this issue Jan 6, 2022
…data lookups (#27579)

- to prevent the issue described in doctrine/orm#8113
jenkins-oroinc-app bot pushed a commit to oroinc/resource-library that referenced this issue Jan 10, 2022
…data lookups (#27579)

- to prevent the issue described in doctrine/orm#8113
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants