doctrine / orm

Doctrine Object Relational Mapper (ORM)
https://www.doctrine-project.org/projects/orm.html
MIT License
9.93k stars 2.51k forks source link

Cascade remove to generate bulk delete SQL #7418

Open umpirsky opened 6 years ago

umpirsky commented 6 years ago

In current implementation cascade={"remove"} produces DELETE query for each record on the "Many" side. That can be many DELETE queries if there are many related records. :)

Is it possible to implement (or override outside of Doctrine itself) bulk deletion with single DELETE FROM table WHERE id IN (...) query?

Q A
New Feature yes
RFC no
BC Break no
Ocramius commented 6 years ago

@umpirsky this is because the removal is scheduled in the UnitOfWork, not in the dedicated persister. Optimising that for a batch of entities of the same type (and therefore any deletion at all) should be feasible, check https://github.com/doctrine/doctrine2/blob/b40c2c6b7893c929f754150d480c20f9b22e0afe/lib/Doctrine/ORM/UnitOfWork.php#L1028-L1033

umpirsky commented 6 years ago

@Ocramius Thanks for pointing me in the right direction!

So, grouping per entity type should be easy, but how to do bulk operations? Using $persister->delete($entity); should be replaced with something else that is capable of bulk delete? Sounds like lower level operation then what EntityPersister is capable of in the current implementation.

Ocramius commented 6 years ago

Should probably be $persister->deleteBulk($entities)

The bulk deletion is something we can then add to DBAL, like in https://github.com/doctrine/dbal/pull/682

umpirsky commented 6 years ago

Wow, that would be great! Thanks for linking that, good to see bulk insert shaping up. :+1:

Ocramius commented 6 years ago

It's not moving since ages, and requires work :-P

umpirsky commented 6 years ago

Oh, only now spotted timestamps!