-
Notifications
You must be signed in to change notification settings - Fork 165
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
Very slow deletion, multiple objects #3566
Comments
➤ PM Bot commented: Jira ticket: RNET-1131 |
My guess is that just parsing the query is taking the majority of the time here. I don't expect this is a case we'd like to actively optimize for, but if you're curious, you could try and measure how long |
Thank you for a quick answer and sorry for the bad formatting in OP. It does seem it actual is realm.RemoveRange that is slow: I should have tested the filtering before saying that. The filtering looks pretty good.
|
just a little correction. I have previosly tested on a more advanced class. I now tested on the class that I wrote here and I get total deletion time of around 10000ms |
I was able to somewhat reproduce this with the following code: using System.Diagnostics;
using Realms;
using var realm = Realm.GetInstance("RemoveRange.realm");
var ids = Enumerable.Range(0, 100_000).Select(_ => Guid.NewGuid()).ToArray();
realm.Write(() =>
{
foreach (var id in ids)
{
realm.Add(new PrimaryKeyGuidObject
{
Id = id
});
}
});
var sw = new Stopwatch();
sw.Start();
var query = string.Join(" OR ", ids.Select(i => $"Id == uuid({i})"));
Console.WriteLine($"Construct string query: {sw.ElapsedMilliseconds}");
var results = realm.All<PrimaryKeyGuidObject>().Filter(query);
Console.WriteLine($"Construct Realm query: {sw.ElapsedMilliseconds}");
realm.Write(() =>
{
realm.RemoveRange(results);
Console.WriteLine($"RemoveRange: {sw.ElapsedMilliseconds}");
});
Console.WriteLine($"Commit: {sw.ElapsedMilliseconds}");
public partial class PrimaryKeyGuidObject : IRealmObject
{
[PrimaryKey]
public Guid Id { get; set; }
} On my M1 mac, this prints out:
I.e. the removal takes about 7.5 seconds. It's not amazing, but it's not the end of the world either, so I don't expect we'll go out of our way to optimize this use case. I'll reach out to the core database team and see if they spot any low-hanging fruit that could speed it up. |
Another interesting observation - if instead of constructing a massive query and deleting all objects that match it, you delete the objects 1 by 1, this is much faster. Essentially, by replacing the second foreach (var id in ids)
{
realm.Remove(realm.Find<PrimaryKeyGuidObject>(id)!);
} I get the whole operation to complete in 350 ms. |
thank you for reply I just tried this class ("PrimaryKeyGuidObject/MyObject") with sqlite (Microsoft.Data.Sqlite). I takes 682ms. So Realm is about 11x times slower in delete range. It is weird that inserts are must faster than deletes? |
I agree - this appears to be hitting some weirdness with how the query is evaluated and maintained throughout the deletion. Not sure if you saw my follow-up comment, but looking up objects and deleting them one by one seems to be, unintuitively, much faster than passing down a query. |
very nice, I can confirm this. Now we are seeing more reasonable ms, and even faster than sqlite which is resonating with other comparisons I have tried. Thank you for this tip! very useful. however i didnt expect RemoveRange to be so slow. maybe there is some weirdness going on there |
What happened?
I am playing around realm and trying to delete 100k items at once.
It takes for me around 20-25 seconds for that write to execute
Maybe my filtering is pretty bad. I followed this syntax: https://stackoverflow.com/questions/70905369/what-is-the-correct-syntax-to-write-a-filter-query-in-realm-net-using-the-in
Repro steps
Create a simple poco class that has PrimaryKey Guid
Create 100k instances and add to the realm.
Select all the ids and filter the realm on the ids.
Delete range with the result.
Expected result: sqllite does it in around 3-4 seconds
Actual result: 20-25 seconds
Version
11.7.0
What Atlas Services are you using?
Local Database only
What type of application is this?
Console/Server
Client OS and version
windows 10 19045
Code snippets
Stacktrace of the exception/crash you're getting
Relevant log output
The text was updated successfully, but these errors were encountered: