Cannot Batch-delete using UnitOfWork & adapter.BatchSize

Posts   
 
    
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 17-Jul-2019 13:30:08   

(Using LLBLGen 5.5.1, Adapter templates & SQL Azure)

Hi, I'm building in the adapter.BatchSize feature to speed up some of our queries. In our system we have an entity called "WorkingCopy" and sometimes we need a lot of these entities to work on, save them and delete them.

I've seen a great increase in speed our Save routine by using a UnitOfWork2, adding the WorkingCopyEntity's with uow.AddForSave and then set the adapter.BatchSize to 50 before doing the uow.Commit. It's 3x faster than using adapter.SaveEntityCollection !

But for the delete, it just does't work. Nothing gets deleted.

This was the original routine :

        public override void Remove(IEnumerable<int> workingcopyIds)
        {
            using (var adapter = CreateAdapter())
            {
                foreach (var workingcopyId in workingcopyIds)
                {
                    var entity = new WorkingCopyEntity(workingcopyId);
                    adapter.DeleteEntity(entity);
                }
            }
        }

which works, but obviously is very slow.

I have tried both the AddForDelete or AddcollectionForDelete of the UnitOfWork, and both don't do anything :

        public override void Remove(IEnumerable<int> workingcopyIds)
        {
            var list = workingcopyIds.ToList();
            var uow = new UnitOfWork2();
            EntityCollection<WorkingCopyEntity> coll = new EntityCollection<WorkingCopyEntity>();
            foreach (var workingcopyId in workingcopyIds)
            {
                var entity = new WorkingCopyEntity(workingcopyId);
                uow.AddForDelete(entity);
            }
            using (var adapter = CreateAdapter())
            {
                adapter.BatchSize = 50;
                uow.Commit(adapter, true);
            }
        }

or

        public override void Remove(IEnumerable<int> workingcopyIds)
        {
            var list = workingcopyIds.ToList();
            var uow = new UnitOfWork2();
            EntityCollection<WorkingCopyEntity> coll = new EntityCollection<WorkingCopyEntity>();
            foreach (var workingcopyId in workingcopyIds)
            {
                var entity = new WorkingCopyEntity(workingcopyId);
                coll.Add(entity);
            }
            uow.AddCollectionForDelete(coll);
            using (var adapter = CreateAdapter())
            {
                adapter.BatchSize = 50;
                uow.Commit(adapter, true);
            }
        }

It does nothing. No error, no output logging when setting the web.config value SqlServerDQE to 4 ... just nothing.

What am I doing wrong ?

Kind regards, Sven.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39613
Joined: 17-Aug-2003
# Posted on: 17-Jul-2019 15:11:14   

The batching facility is for inserts/updates only at the moment, so deletes aren't batched. So it's indeed not going to work: it's not built-in at the moment.

Are there criteria you can use to delete them with a direct delete on the database instead? You can use these calls in a Uow as well.

Frans Bouma | Lead developer LLBLGen Pro
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 17-Jul-2019 15:15:31   

Well, I have the list of Ids that need to be deleted ....

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 17-Jul-2019 23:02:30   

The best way is to execute the delete directly against the database without creating or fetching entities.

var bucket = new RelationPredicateBucket(CustomerFields.CustomerID.In(IDs) ); // IDs is int[] in this case.
using(var adapter = new DataAccessAdapter())
{
    adapter.DeleteEntitiesDirectly("CustomerEntity", bucket);
}

using UnitOfWork:

uow.AddDeleteEntitiesDirectlyCall("CustomerEntity", bucket);