berlindb / core

All of the required core code
MIT License
253 stars 27 forks source link

Feature Request: add batch update, add, and delete methods. #18

Open alexstandiford opened 4 years ago

alexstandiford commented 4 years ago

There are many situations where it's practical to create, or update many records at one time, using the same values. Currently, the only way to-do this with Berlin is by querying the records, looping through each one, and updating the record one at a time.

While working with Mongoose, I found that they have three methods, updateMany, addMany, and deleteMany, which would make the database update using a single query instead of x+1 queries as mentioned in the above paragraph.

alexstandiford commented 4 years ago

WIP PR for update_many method. #20

alexstandiford commented 3 years ago

So, after working with BerlinDB for a while, I've come to realize that this is not a great idea, in-general. The query is technically faster than looping through and running many queries, however, in a production site this could really gum up the WordPress install while this query runs.

If anything, I think update_many could potentially be basically a method that accepts a query, and will delete all records that match that query. The key difference is it would be little more than a helper function that would use a foreach loop to loop through the found records, and delete each one.

JJJ commented 3 years ago

The nice thing about having methods be wrappers like you suggest @alexstandiford, is that it offers up an opportunity to build in some kind of batch processing/jobs/deferment type stuff. 👍

alexstandiford commented 3 years ago

Right, which is exactly how you would actually delete large quantities of data in WordPress, anyway. These seem like a great opportunity to make a static method.

Something like:


/**
 * @var array $deleted array of deleted data IDs
**/
$deleted = Query::delete_many( [/** query args **/] );

/**
 * @var array $inserted array of inserted data IDs
**/
$inserted = Query::insert_many([/** array of items to insert **/]);

/**
 * @var array $inserted array of updated data IDs
**/
$update_many = Query::update_many([/** array of items to update, potentially keyed by the item ID or something like that. **/]);