当前位置:网站首页>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 .
边栏推荐
- LeetCode 503. Next bigger Element II
- 单片机原理与接口技术(ESP8266/ESP32)机器人类草稿
- TDengine ×英特尔边缘洞见软件包 加速传统行业的数字化转型
- tongweb设置gzip
- Baidu app's continuous integration practice based on pipeline as code
- Node の MongoDB Driver
- 卷起来,突破35岁焦虑,动画演示CPU记录函数调用过程
- Understand the window query function of tdengine in one article
- H.265编码原理入门
- What should we pay attention to when entering the community e-commerce business?
猜你喜欢
TDengine 已经支持工业英特尔 边缘洞见软件包
移动端异构运算技术-GPU OpenCL编程(进阶篇)
An article takes you into the world of cookies, sessions, and tokens
[technical live broadcast] how to rewrite tdengine code from 0 to 1 with vscode
百度APP 基于Pipeline as Code的持续集成实践
Roll up, break 35 - year - old Anxiety, animation Demonstration CPU recording Function call Process
H.265编码原理入门
Develop and implement movie recommendation applet based on wechat cloud
LeetCode 503. Next bigger Element II
如何正确的评测视频画质
随机推荐
TDengine 离线升级流程
[ManageEngine] how to make good use of the report function of OpManager
Principle and performance analysis of lepton lossless compression
Understanding of smt32h7 series DMA and DMAMUX
如何正确的评测视频画质
Why don't you recommend using products like mongodb to replace time series databases?
Community group buying has triggered heated discussion. How does this model work?
TDengine ×英特尔边缘洞见软件包 加速传统行业的数字化转型
【饿了么动态表格】
LeetCode 503. Next bigger Element II
LeetCode 496. Next larger element I
【el-table如何禁用】
Online chain offline integrated chain store e-commerce solution
Lepton 无损压缩原理及性能分析
TDengine 连接器上线 Google Data Studio 应用商店
OpenGL - Lighting
How to empty uploaded attachments with components encapsulated by El upload
Go 语言使用 MySQL 的常见故障分析和应对方法
MySQL does not take effect in sorting string types
High performance spark_ Transformation performance