Use LinqDB Sqlite database query data, whether large or small amounts of data data, I feel particularly time-consuming, especially in the first query
A data table containing data of 27000
The first query:
- Query 27000 data, time-consuming 1s
- 8 data query, but also took 750ms
Secondary query:
- 27000 data query, took 475ms
- Queries a specified data and time-consuming 73ms
Let's try to optimize it, use Sql statement to query
Sql query the database
Sql connection string:
1 var dbRelativePath = "Dbs\\EnglishDict.db3"; 2 var connectionString = "data source=" + System.Environment.CurrentDirectory + "\\" + dbRelativePath + ";version=3;";
Sql query returns a DataSet collection
1 /// <summary> 2 /// 获得数据列表 3 /// </summary> 4 public DataSet GetList(string strWhere, string tableName) 5 { 6 StringBuilder strSql = new StringBuilder(); 7 strSql.Append("select * "); 8 strSql.Append($" FROM {tableName} "); 9 if (strWhere.Trim() != "") 10 { 11 strSql.Append ( " WHERE " + strWhere); 12 is } 13 is return Query (strSql.ToString ()); 14 } 15 /// <Summary> 16 /// execute a query, returns the DataSet . 17 /// </ Summary > 18 is /// <param name = "SqlString"> query </ param> . 19 /// <Returns> the DataSet </ Returns> 20 is public the DataSet query ( String SqlString) 21 is { 22 is the using (SQLiteConnection connection = new SQLiteConnection(connectionString)) 23 { 24 DataSet ds = new DataSet(); 25 try 26 { 27 connection.Open(); 28 SQLiteDataAdapter command = new SQLiteDataAdapter(sQLString, connection); 29 command.Fill(ds, "ds"); 30 } 31 catch (System.Data.SQLite.SQLiteException ex) 32 { 33 throw new Exception(ex.Message); 34 } 35 return ds; 36 } 37 }
DataSet dataset data transfer list
1. Using reflection, the data is mapped to the class Entity
See database query - DataTable Entity types of data transfer
. 1 /// <Summary> 2 /// obtain data list . 3 /// </ Summary> . 4 public List <CoursewareInfo> GetCoursewares () . 5 { . 6 the DataSet the GetList DS = ( String .Empty, " Courseware " ); . 7 / / by mapping, DataSet transfer entity classes . 8 var modelList = DataTableConverter <CoursewareInfo> .ToList (ds.Tables [ 0 ]); . 9 return modelList; 10 } . 11 /// <Summary> 12 is /// obtained data list 13 /// </summary> 14 public List<CoursewareInfo> GetCoursewares(string queryText) 15 { 16 var queryString = $"Name like '%{queryText}%'"; 17 DataSet ds = GetList(queryString, "Courseware"); 18 //通过映射,DataSet转实体类 19 var modelList = DataTableConverter<CoursewareInfo>.ToList(ds.Tables[0]); 20 return modelList; 21 }
We look at the performance of the query data or the same data table
The first query:
- 27000 data query, took 1612ms
- Queries a specified data, but also took 196ms
Secondary query:
- 27000 data query, took 1484ms
- Queries a specified data and time-consuming 59ms
This program time-consuming, it should be reflected hurt performance, give up
2. Direct Data Class field attribute assignment to
DataTable transfer data class:
1 /// <summary> 2 /// 将DataTable转换成Entity列表 3 /// </summary> 4 /// <param name="dt"></param> 5 /// <returns></returns> 6 public List<CoursewareInfo> ConvertDtToModelList(DataTable dt) 7 { 8 List<CoursewareInfo> list = new List<CoursewareInfo>(); 9 foreach (DataRow dr in dt.Rows) 10 { 11 list.Add(DataRowToModel(dr)); 12 } 13 return list; 14 } 15 /// <summary> 16 /// 得到一个对象实体 17 /// </summary> 18 public CoursewareInfo DataRowToModel(DataRow row) 19 { 20 CoursewareInfo model = new CoursewareInfo(); 21 if (row != null) 22 { 23 model.LocalId = row["LocalId"].ToString(); 24 model.RemoteId = row["RemoteId"].ToString(); 25 model.Name = row["Name"].ToString(); 26 } 27 return model; 28 }
Gets a list:
. 1 /// <Summary> 2 /// obtain data list . 3 /// </ Summary> . 4 public List <CoursewareInfo> GetCoursewares () . 5 { . 6 the DataSet the GetList DS = ( String .Empty, " Courseware " ); . 7 / / via field assignment, DataSet transfer entity classes . 8 var modelList = ConvertDtToModelList (ds.Tables [ 0 ]); . 9 return modelList; 10 } . 11 /// <Summary> 12 is /// obtain data list 13 /// </ Summary> 14 public List <CoursewareInfo> GetCoursewares ( String the queryText) 15 { 16 var the queryString $ = " the Name like 'the queryText% {%}' " ; . 17 the DataSet DS = the GetList (the queryString, " Courseware " ); 18 // through field assignment, DataSet transfer entity classes . 19 var modelList = ConvertDtToModelList (ds.Tables [ 0 ]); 20 is return modelList; 21 is }
Look at the performance of the query data or the same data table
The first query:
- 27000 data query, took 660ms
- Queries a specified data, but also took 191ms
Secondary query:
- 27000 data query, took 500ms
- Queries a specified data and time-consuming 58ms
In this scenario, data query performance is significantly improved.
Summary: Relative LINDB, use Sql query program query data performance will be much better