SQLite.NET性能——使用带LIKE运算符的SQLiteParameter
本文关键字:运算符 LIKE SQLiteParameter NET 性能 SQLite | 更新日期: 2023-09-27 17:59:42
我在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毫秒才能执行。为什么有区别?是不是我做了什么完全错误的事情来赢得我的新手身份?
提前感谢!
所以,我想我可能已经将其缩小到系统的问题。数据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行。我所做的和系统应该做的之间有什么不同吗。数据SQLite?希望这只是我可以报告的一个系统错误。数据SQLite,也许可以应用我自己的修复程序。
由于我看不出这两个查询之间有任何区别,但事实上,一个使用sqliteparameter,另一个使用完整的sql语句作为字符串-我只是在谷歌上搜索了您的问题,并偶然发现了这一点。
在那里,它表明在SQLiteCommand对象上有一个名为ParameterCheck的属性,这可能会导致一些性能损失。
您可以尝试重写代码以传递SQLiteCommand对象,并将ParameterCheck属性设置为false。我认为你应该加快速度。
至少值得一试:)
我也遇到过系统性能问题。数据SQLite,其中一些我已经能够解决和改进,而另一些我还没有。
然而,最近我发现了这个替代的C#SQLite库:http://code.google.com/p/csharp-sqlite/
它并没有给我带来任何性能问题,我实际上取代了System。数据SQLite在一个现有的项目中使用了这个(几乎没有语法上的变化——我或多或少只是替换了DLL和using指令……有几行我必须打字),它惊人地加快了速度。有几次,我在System上等待了几秒钟。数据SQLite,现在执行是即时的。