当前位置:网站首页>Optimize database queries using the cursor object of SQLite
Optimize database queries using the cursor object of SQLite
2022-07-05 09:47:00 【Black Mountain demon 2018】
Use a background
I read a piece of code written by my colleagues very early , very interesting , In order to obtain the id aggregate , Through the first dao Object to get all personnel information in the database ,
Then one by one add Into the list aggregate . If the number of database reaches tens of thousands , In fact, whether in terms of memory or efficiency , This way is not advisable .
/**
* Get personId aggregate
* todo This method can be optimized
*
* @return
*/
public List<String> getPersonIdList() {
List<PersonInfo> personInfos = queryAll();
if (personInfos == null || personInfos.size() == 0) {
return new ArrayList<>();
}
List<String> personIdsList = new ArrayList<>();
for (PersonInfo info : personInfos) {
if (info == null || TextUtils.isEmpty(info.getPersonId())) {
continue;
}
personIdsList.add(info.getPersonId());
}
return personIdsList;
}
Cursor object
Let's get to know Cursor object , He is right sqlite The result set after database query , It can be understood as a set of each row of query results .
Simple api Use as follows
// Close cursor , Release resources
close()
// Retrieve the text of the requested column in the buffer , Store it
copyStringToBuffer(int columnIndex, CharArrayBuffer buffer)
// Returns the total number of all columns
getColumnCount()
// Returns the name of the specified column , Returns if none exists -1
getColumnIndex(String columnName)
// Returns the specified column name from zero , If it does not exist, it will throw IllegalArgumentException abnormal .
getColumnIndexOrThrow(String columnName)
// Returns the column name from the given index
getColumnName(int columnIndex)
// Returns the column name of a string array
getColumnNames()
// return Cursor The number of lines in
getCount()
// Move the cursor to the first line
moveToFirst()
// Move the cursor to the last line
moveToLast()
// Move the cursor to the next line
moveToNext()
// Move the cursor to an absolute position
moveToPosition(int position)
Move the cursor to the previous line
moveToPrevious()
Use Cursor Optimize database queries
/**
* Get personId aggregate
*
* @return
*/
public List<String> getPersonIdList() {
List<String> personIdsList = new ArrayList<>();
Cursor cursor = null;
try {
cursor = DbCore.getDaoSession().getRecordDao().getDatabase().rawQuery("select PERSON_ID from person_info", null);
while (cursor.moveToNext()) {
personIdsList.add(cursor.getString(0));
}
cursor.close();
} catch (Exception e) {
if (cursor != null) {
cursor.close();
cursor = null;
}
}
return personIdsList;
}
The above code can be seen through sql Statement to query only the required fields PERSON_ID Information , Then the data of each row passes cursor.getString(0) To get , In terms of memory allocation , This avoids building a large number of unnecessary personInfos object , In terms of reading efficiency , Also avoid querying unnecessary information .
边栏推荐
- 揭秘百度智能测试在测试自动执行领域实践
- H. 265 introduction to coding principles
- Analysis on the wallet system architecture of Baidu trading platform
- Mobile heterogeneous computing technology GPU OpenCL programming (Advanced)
- 【el-table如何禁用】
- TDengine ×英特尔边缘洞见软件包 加速传统行业的数字化转型
- [two objects merged into one object]
- LeetCode 556. 下一个更大元素 III
- 小程序启动性能优化实践
- 【技术直播】如何用 VSCode 从 0 到 1 改写 TDengine 代码
猜你喜欢
百度APP 基于Pipeline as Code的持续集成实践
OpenGL - Model Loading
TDengine × Intel edge insight software package accelerates the digital transformation of traditional industries
E-commerce apps are becoming more and more popular. What are the advantages of being an app?
Unity SKFramework框架(二十二)、Runtime Console 运行时调试工具
[technical live broadcast] how to rewrite tdengine code from 0 to 1 with vscode
LeetCode 556. 下一个更大元素 III
Observation cloud and tdengine have reached in-depth cooperation to optimize the cloud experience of enterprises
观测云与 TDengine 达成深度合作,优化企业上云体验
What are the advantages of the live teaching system to improve learning quickly?
随机推荐
High performance spark_ Transformation performance
How Windows bat script automatically executes sqlcipher command
基于宽表的数据建模应用
【对象数组a与对象数组b取出id不同元素赋值给新的数组】
An article takes you into the world of cookies, sessions, and tokens
Nips2021 | new SOTA for node classification beyond graphcl, gnn+ comparative learning
[object array A and object array B take out different elements of ID and assign them to the new array]
Application of data modeling based on wide table
Tdengine connector goes online Google Data Studio app store
How to implement complex SQL such as distributed database sub query and join?
LeetCode 496. 下一个更大元素 I
Unity skframework framework (24), avatar controller third person control
A detailed explanation of the general process and the latest research trends of map comparative learning (gnn+cl)
使用el-upload封装得组件怎么清空已上传附件
Why don't you recommend using products like mongodb to replace time series databases?
How to choose the right chain management software?
TDengine可通过数据同步工具 DataX读写
【两个对象合并成一个对象】
A keepalived high availability accident made me learn it again
Node の MongoDB Driver