[英]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.