简体   繁体   English

ServiceStack.Ormlite for sqlite运行时间非常慢

[英]ServiceStack.Ormlite for sqlite with really slow running time

I am using ServerStack.OrmLite 4.0 on Windows 7. I created a table with OrmLite and inserted about 100 rows of data on a Sqlite file. 我在Windows 7上使用ServerStack.OrmLite 4.0。我用OrmLite创建了一个表,并在Sqlite文件中插入了约100行数据。 The time of the Db.Select() took about 1 minute. Db.Select()的时间约为1分钟。 When I changed the database to mysql, it returns the result instantly. 当我将数据库更改为mysql时,它会立即返回结果。 I also tried access the sqlite database using another GUI software, and tried execute some sql statements and they all worked fine. 我还尝试使用其他GUI软件访问sqlite数据库,并尝试执行一些sql语句,它们都工作正常。 Does anybody have any clue? 有人有任何线索吗?

Updated With Code: 用代码更新:

static void Main(string[] args)
    {
        string dbName = "testdb.sqlite";
        var path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        if (!System.IO.File.Exists(path + "/" + dbName))
        { 
            System.IO.File.Create(path + "/" + dbName).Dispose();
        }
        var dbFacory = new OrmLiteConnectionFactory("Data Source=./testdb.sqlite;Version=3;UTF8Encoding=True;", SqliteDialect.Provider);
        //var dbFacory = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider);
        var db = dbFacory.OpenDbConnection();
        db.DropAndCreateTable<TestTable>();
        db.DropAndCreateTable<BasicPersonnelInfo>();

        Console.WriteLine("Starts at : {0}", DateTime.Now.Second);
        for (int i = 0; i < 100; i++)
        {
            db.Insert<TestTable>(new TestTable { TestField = i.ToString()});
            db.Insert<BasicPersonnelInfo>(new BasicPersonnelInfo { Test3 = i.ToString()});
        }
        Console.WriteLine("Inserting Completed;");
        Console.WriteLine("Select at : {0}", DateTime.Now.Second);
        db.Select<BasicPersonnelInfo>();
        Console.WriteLine("Ends   at : {0}", DateTime.Now.Second);
        Console.WriteLine("Prese anykey to quit!");
        Console.ReadKey();
    }

If you're saving SQLite to disk, I've seen really long running times due to file permission, if you're running in ASP.NET your SQLite database should be saved in your ~/App_Data folder and should be given write permissions to IIS_USR User Account. 如果要将SQLite保存到磁盘,由于文件权限的原因,我发现运行时间非常长;如果您在ASP.NET中运行,则应将SQLite数据库保存在~/App_Data文件夹中,并应授予对IIS_USR用户帐户。

To provide some idea of the expected time it should take I've added a Simple Insert/Select Benchmark that inserts and selects 100 rows in a table containing 20 string columns for both SQLite in memory: 为了提供预期的时间,我添加了一个简单的插入/选择基准 ,该基准可以在包含20个字符串列的表中插入并选择100行,该表包含内存中的两个SQLite:

var dbFactory = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider);
using (var db = dbFactory.Open())
{
    db.DropAndCreateTable<TableWithStrings>();

    var sw = Stopwatch.StartNew();
    for (int i = 0; i < 100; i++)
    {
        var row = TableWithStrings.Create(i);
        db.Insert(row);
    }
    "[:memory:] Time to INSERT 100 rows: {0}ms".Print(sw.ElapsedMilliseconds);

    sw = Stopwatch.StartNew();
    var rows = db.Select<TableWithStrings>();
    "[:memory:] Time to SELECT {0} rows: {1}ms".Print(rows.Count, sw.ElapsedMilliseconds);
}

And an SQLite File Database: 还有一个SQLite文件数据库:

var dbPath = "~/App_Data/db.sqlite".MapProjectPath();
var dbFactory = new OrmLiteConnectionFactory(dbPath, SqliteDialect.Provider);
using (var db = dbFactory.Open())
{
    db.DropAndCreateTable<TableWithStrings>();

    var sw = Stopwatch.StartNew();
    for (int i = 0; i < 100; i++)
    {
        var row = TableWithStrings.Create(i);
        db.Insert(row);
    }
    "[db.sqlite] Time to INSERT 100 rows: {0}ms".Print(sw.ElapsedMilliseconds);

    sw = Stopwatch.StartNew();
    var rows = db.Select<TableWithStrings>();
    "[db.sqlite] Time to SELECT {0} rows: {1}ms".Print(rows.Count, sw.ElapsedMilliseconds);
}

Running this as a R# NUnit test inside VS.NET 2015 on my 2013 Macbook Pro workstation (with 4 VS instances and multiple RDBMS's running in background) results in: 在我的2013 Macbook Pro工作站上(带有4个VS实例和多个RDBMS在后台运行)在VS.NET 2015中作为R#NUnit测试运行此程序会导致:

[:memory:] Time to INSERT 100 rows: 10ms
[:memory:] Time to SELECT 100 rows: 1ms

And for SQLite file database: 对于SQLite文件数据库:

[db.sqlite] Time to INSERT 100 rows: 659ms
[db.sqlite] Time to SELECT 100 rows: 13ms

Whilst this isn't a proper benchmark (ie no warm-up, not in Console App w/ more iterations), it should provide some indication on expected results. 虽然这不是一个适当的基准(例如,没有预热,也没有在带有更多迭代的Console App中使用),但它应该提供一些有关预期结果的指示。 Note OrmLite tests use the ServiceStack.OrmLite.Sqlite.Mono version of OrmLite SQLite. 注意OrmLite测试使用OrmLite SQLite的ServiceStack.OrmLite.Sqlite.Mono版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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