SQLite.NET性能使用SQLiteParameter和LIKE运算符

Ter*_*rry 8 c# sqlite system.data.sqlite entity-framework-4.1

我在SQLite查询中使用SQLiteParameters和LIKE运算符时遇到问题.这是一段代码,如果我这里没有足够的代码,我会道歉.如果是这种情况,我可以轻松发布更多内容.

表现不佳:

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

很好的表现;很好的绩效:

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

其他重要代码:

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

public class SearchResult
{
    [Key]
    public Guid Id { get; set; }
    public string ContactName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

第一个例子需要700毫秒才能执行,我的主管认为这是不可接受的.第二个例子需要7毫秒才能执行.为什么不同?为了赢得新手身份,有什么我做错了吗?

提前致谢!

Ter*_*rry 2

所以,我想我可能已经将问题范围缩小到了 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);
}
Run Code Online (Sandbox Code Playgroud)

PoorPerformance 和 GoodPerformance 函数对于 11 行的结果都是 1 毫秒。我所做的和 System.Data.SQLite 应该做的有什么不同吗?希望这只是我可以将其报告为 System.Data.SQLite 的错误,并可能应用我自己的修复程序。