简体   繁体   中英

Why does db.select<T> is so slow when the model inherits from AuditBase?

I can observe that fetching all records from a small table (100 records) can take 1600 miliseconds, even using a ":memory:" SQLite database.

This happens only when the model inherits from AuditBase ; otherwise the performance is fast (around 8 miliseconds).

My test code is:

public class Record : AuditBase
{
    [PrimaryKey]
    [AutoIncrement]
    public int Id { get; set; }
    
    public string Name { get; set; }
}

Table is created and pre-populated with 100 records:

using var db = appHost.Resolve<IDbConnectionFactory>().Open();


if (db.CreateTableIfNotExists<Record>())
{
    for (int i = 0; i < 100; i++)
    {
        db.Insert(new Record()
        {
            Name = $"Name {i}" ,
            
            CreatedBy =  "TEST",
            ModifiedBy = "TEST",
            CreatedDate = DateTime.Now,
            ModifiedDate = DateTime.Now
                                     
        });
    }
}

Selecting the data from the service:

public class MyServices : Service
{
    public IAutoQueryDb AutoQuery { get; set; } = null!;
    public object Any(Hello request)
    {
        var sw = new Stopwatch();
        
        sw.Start();
        
        var records = Db.Select<Record>();
        
        var loadTime = sw.ElapsedMilliseconds;

        Console.WriteLine($"Took {loadTime}ms to load {records.Count} records");

        return new HelloResponse { Result = $"Hello, {request.Name}!" };
    }
}   

The console output will read: Took 1584ms to load 100 records .

Why does it take so long?

I'm not able to repro this perf issue, I've created 2 programs, 1 which inherits AuditBase as done in this example at:

https://gist.cafe/fe8d04617ee1b061f24b0d8d56948119

Which shows it completes in 23ms .

Whilst another example which replaces DateTime properties with long's to store Unix Timestamps to avoid DateTime conversions during Select at: https://gist.cafe/a7c55a56842899a1ce34f11456027a9b

Which completes in roughly the same time at 21ms .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM