繁体   English   中英

SQLite.NET性能与LIKE运算符配合使用SQLiteParameter

[英]SQLite.NET Performance Using SQLiteParameter with LIKE operator

我在SQLite查询中使用SQLiteParameters和LIKE运算符时遇到问题。 这是一段代码,如果这里没有足够的代码,我深表歉意。 如果是这样,我可以轻松发布更多。

表现不佳:

using (OdysseyDataContext entities = new OdysseyDataContext())
{
    var results = entities.SearchResults.SqlQuery(
        "SELECT * FROM SearchResults WHERE ContactName LIKE @ContactName",
        new SQLiteParameter("@ContactName", "test")
    );
}

很棒的演出:

using (OdysseyDataContext entities = new OdysseyDataContext())
{
    var results = entities.SearchResults.SqlQuery(
        string.Format(
            "SELECT * FROM SearchResults WHERE ContactName LIKE '{0}'",
            "test"
        )
    );
}

其他重要代码:

public class OdysseyDataContext : DbContext
{
    public DbSet<SearchResult> SearchResults { get; set; }
}

public class SearchResult
{
    [Key]
    public Guid Id { get; set; }
    public string ContactName { get; set; }
}

第一个示例需要700毫秒来执行,我的主管认为这是不可接受的。 第二个示例需要7毫秒才能执行。 为什么会有所不同? 为获得新手身份,我在做些完全错误的事情吗?

提前致谢!

因此,我想我可能已将其范围缩小到System.Data.SQLite的问题。 我在C ++中尝试了以下代码:

#include "sqlite3.h"
#include <stdio.h>

void xProfile(void* pArg, const char* query, sqlite3_uint64 pTimeTaken)
{
    printf("%s\n", query);
    printf("%I64d ms\n", pTimeTaken / 1000000);
}

void PoorPerformance();
void GoodPerformance();

int main()
{
    printf("Poor Performance:\n");
    PoorPerformance();

    printf("Good Performance:\n");
    GoodPerformance();

    return 0;
}

void PoorPerformance()
{
    int rc;
    int rowCount = 0;

    sqlite3 *db;
    if (sqlite3_open("<<File Here>>", &db))
    {
        printf("Could not open the database.");
        return;
    }

    sqlite3_profile(db, &xProfile, NULL);

    sqlite3_stmt *statement;
    if (!sqlite3_prepare_v2(db, "SELECT * FROM SearchResults WHERE ContactName LIKE @ContactName;", -1, &statement, 0))
    {
        int result = 0;
        int parameterIndex = sqlite3_bind_parameter_index(statement, "@ContactName");
        sqlite3_bind_text(statement, 1, "test", -1, NULL);
        while (result != SQLITE_DONE)
        {
            result = sqlite3_step(statement);

            if (result == SQLITE_ROW)
            {
                rowCount++;
            }
        }

        sqlite3_finalize(statement);
    }

    printf("%d rows\n", rowCount);

    sqlite3_close(db);
}

void GoodPerformance()
{
    int rc;
    int rowCount = 0;

    sqlite3 *db;
    if (sqlite3_open("<<File Here>>", &db))
    {
        printf("Could not open the database.");
        return;
    }

    sqlite3_profile(db, &xProfile, NULL);

    sqlite3_stmt *statement;
    if (!sqlite3_prepare_v2(db, "SELECT * FROM SearchResults WHERE ContactName LIKE 'test';", -1, &statement, 0))
    {
        int result = 0;

        while (result != SQLITE_DONE)
        {
            result = sqlite3_step(statement);

            if (result == SQLITE_ROW)
            {
                rowCount++;
            }
        }

        sqlite3_finalize(statement);
    }

    printf("%d rows\n", rowCount);

    sqlite3_close(db);
}

PoorPerformance和GoodPerformance函数的结果都是1毫秒(11行)。 我所做的事情和应该由System.Data.SQLite完成的工作之间有什么不同吗? 希望这只是我可以报告为System.Data.SQLite的错误并可能应用我自己的修复程序的东西。

因为我看不出这两个查询之间的事实,另一个完整的SQL语句作为字符串的任何区别,但是,一个使用sqliteparameter -我只是用Google搜索你的问题,偶然发现

那里表明在SQLiteCommand对象上有一个名为ParameterCheck的属性,这可能会导致性能下降。

您可以尝试重写代码以传递SQLiteCommand对象,并将ParameterCheck属性设置为false。 我认为您应该这样做。

至少值得一试:)

我还遇到了System.Data.SQLite的性能问题,其中一些我已经能够解决和改进,而另一些我没有。

但是,最近我发现了这个替代的C#SQLite库: http : //code.google.com/p/csharp-sqlite/

它没有给我带来任何性能问题,实际上我在现有项目中用这个替换了System.Data.SQLite(语法几乎没有变化-我或多或少实际上只是替换了DLL和using指令。。在我不得不打字的地方排成一行),它使事情变得异常快。 有时候我在等待使用System.Data.SQLite的秒级命令,现在执行是瞬时的。

暂无
暂无

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

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