当前位置:网站首页>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 .
边栏推荐
- Nips2021 | new SOTA for node classification beyond graphcl, gnn+ comparative learning
- 基于模板配置的数据可视化平台
- First understanding of structure
- Why do offline stores need cashier software?
- 植物大战僵尸Scratch
- [sorting of object array]
- Lepton 无损压缩原理及性能分析
- 使用el-upload封装得组件怎么清空已上传附件
- Common fault analysis and Countermeasures of using MySQL in go language
- 百度智能小程序巡檢調度方案演進之路
猜你喜欢
百度智能小程序巡检调度方案演进之路
LeetCode 503. 下一个更大元素 II
22-07-04 Xi'an Shanghao housing project experience summary (01)
OpenGL - Coordinate Systems
Nips2021 | new SOTA for node classification beyond graphcl, gnn+ comparative learning
Baidu app's continuous integration practice based on pipeline as code
What should we pay attention to when developing B2C websites?
基于宽表的数据建模应用
Viewpager pageradapter notifydatasetchanged invalid problem
Uncover the practice of Baidu intelligent testing in the field of automatic test execution
随机推荐
Why does everyone want to do e-commerce? How much do you know about the advantages of online shopping malls?
Unity SKFramework框架(二十四)、Avatar Controller 第三人称控制
Tdengine offline upgrade process
MYSQL 对字符串类型排序不生效问题
Vs code problem: the length of long lines can be configured through "editor.maxtokenizationlinelength"
OpenGL - Coordinate Systems
First understanding of structure
Nips2021 | new SOTA for node classification beyond graphcl, gnn+ comparative learning
百度智能小程序巡检调度方案演进之路
Tdengine already supports the industrial Intel edge insight package
How to implement complex SQL such as distributed database sub query and join?
【对象数组a与对象数组b取出id不同元素赋值给新的数组】
分布式数据库下子查询和 Join 等复杂 SQL 如何实现?
The most comprehensive promotion strategy: online and offline promotion methods of E-commerce mall
Tdengine connector goes online Google Data Studio app store
LeetCode 503. Next bigger Element II
Figure neural network + comparative learning, where to go next?
Go 语言使用 MySQL 的常见故障分析和应对方法
About getfragmentmanager () and getchildfragmentmanager ()
idea用debug调试出现com.intellij.rt.debugger.agent.CaptureAgent,导致无法进行调试