jptsetme
jptsetme

Reputation: 158

What's an efficient way to do a partial update to a collection

I have a large collection of model objects with a single field that I'd like to update for all of them indicating a change in their status (they're all transitioning to the same new state.) I was inefficiently updating the entire collection using UpdateAll(), but for large collections this has become quite slow.

I am wondering if there's a collection friendly way to call UpdateOnly(), and if not, I'm wondering if my best route would be to wrap a transaction around individual calls to UpdateOnly() for each model object. (And if not, I'm interested to learn the recommended way to accomplish this.)

Lastly as an aside -- awesome framework -- huge thanks to Demis and all of the other contributors!

Upvotes: 1

Views: 238

Answers (1)

jptsetme
jptsetme

Reputation: 158

In the absence of an answer, I implemented the workaround I mentioned in my question (wrapping a transaction around individual calls to UpdateOnly(). This seemed better when the table in question did not have a lot of contention on it. But when we started performance testing, that loop became the bottleneck of the whole application.

I ended up looking at the ORMLite source (which, in hindsight, I should have in the first place.) You can use the Update(this IDbConnection dbConn, string table = null, string set = null, string where = null) method to do bulk partial updates in ORMLite. For example:

    var ids = MyObjectList.Select(h => h.Id);
    string idsCsv = string.Join(", ", ids);
    string whereClause = "Id in (" + idsCsv + ")";
    const string setClause = "MyStatus = 'Queried'";
    db.Update("MyObject", setClause, whereClause);

Upvotes: 2

Related Questions