我正在使用SQLite作为数据库的Ormlite-ServiceStack。我正在从单个Select查询(C#DotNet和数据库为SQLite(v4.0.30319))中的SQLite数据库表中检索数百万(1195935)记录,如下所示。
SQLite不支持存储过程。
整个过程要花费30秒钟以上的时间才能通过单个查询检索数据。如何提高毫秒级的性能。我已经尝试过其他方法,例如实体框架,SQLiteData Adapter,但无法加快毫秒级从数据库获取数据的速度。
我的机器也是非常快速的固态硬盘,具有16 GB RAM,基于X64的Windows 7专业版。
public string connectionstring** = "Data Source = " + System.Configuration.ConfigurationManager.AppSettings["DatabasePath"].ToString() + ";";
public class ClientSideDataResponse
{
public int ID { get; set; }
public int ByDevSessionId { get; set; }
public int ByDeviceId { get; set; }
public int value1 { get; set; }
public int value2 { get; set; }
public int SequenceId{ get; set; }
public DateTime Timestamp { get; set; }
}
public List< ClientSideDataResponse> executeReadQuery_List_ClientSideData()
{
System.Data.SQLite.SQLiteCommand myCommand = new System.Data.SQLite.SQLiteCommand();
List<ClientSideDataResponse> results = new List<ClientSideDataResponse>();
String _query = "SELECT ID, ByDevSessionId, ByDeviceId, Value1, Value2, SequenceId, Timestamp FROM ClientSideData ORDER BY ID";
try
{
using (System.Data.SQLite.SQLiteConnection con = new System.Data.SQLite.SQLiteConnection(connectionstring))
{
using (var cmd = con.CreateCommand())
{
if (con.State == ConnectionState.Closed || con.State == ConnectionState.Broken)
{
con.Open();
}
using (var transaction = con.BeginTransaction())
{
cmd.CommandText = _query;
try
{
System.Data.SQLite.SQLiteDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
ClientSideDataResponse newItem = new ClientSideDataResponse();
if (!string.IsNullOrEmpty(reader["ID"].ToString()) == true)
{
newItem.ID = Convert.ToInt32(reader["ID"]);
}
if (!string.IsNullOrEmpty(reader["ByDevSessionId"].ToString()) == true)
{
newItem.ByDevSessionId = Convert.ToInt32(reader["ByDevSessionId"]);
}
if (!string.IsNullOrEmpty(reader["ByDeviceId"].ToString()) == true)
{
newItem.ByDeviceId = Convert.ToInt32(reader["ByDeviceId"]);
}
if (!string.IsNullOrEmpty(reader["Value1"].ToString()) == true)
{
newItem.Value1 = Convert.ToInt32(reader["Value1"]);
}
if (!string.IsNullOrEmpty(reader["Value2"].ToString()) == true)
{
newItem.Pulse = Convert.ToInt32(reader["Value2"]);
}
if (!string.IsNullOrEmpty(reader["SequenceId"].ToString()) == true)
{
newItem.SequenceId = Convert.ToInt32(reader["SequenceId"]);
}
if (!string.IsNullOrEmpty(reader["Timestamp"].ToString()) == true)
{
newItem.Timestamp = Convert.ToDateTime(reader["Timestamp"].ToString());
}
results.Add(newItem);
}
reader.Close();
}
catch (Exception ex)
{
logger.Debug(ex.Message);
return results;
}
transaction.Commit();
cmd.Dispose();
if (con.State == ConnectionState.Open)
{
con.Close();
}
return results;
}
}
}
}
catch (Exception ex)
{
logger.Debug(ex.Message);
return results;
}
}
最佳答案
您可以采取一些措施来加快速度。 (但是它可能不会使它快很多)
根据需要创建索引以提高order by
的性能。查看查询的执行计划。甚至更好的是,如果使用者不需要,请删除order by
。
将结果列表的容量设置为接近结果(如果已知)的容量。 new List<ClientSideDataResponse>(1200000);
而不是与string
进行转换,而要使用数据库中的实际值。 (当然,除非数据库中的值实际上是字符串。然后需要重新设计)
这个:
if (!string.IsNullOrEmpty(reader["ID"].ToString()) == true)
{
newItem.ID = Convert.ToInt32(reader["ID"]);
}
而是:
if(!reader.IsDBNull(0)) //0 is the index of the column.
newItem.ID = reader.GetInt32(0);
查询的所有其他结果相同。