Android Performance Optimization-sqlite database

Source: Internet
Author: User

In this series I would like to share some of the optimization details around Android with everyone. Android is an important task, Android is not enough to like the PC side has a high memory execution space for us to use the heavy-duty overhead. Limited memory resources limit our direction of expansion. As a result, memory optimization and performance optimization in Android have become a key point for a siege lion to be overlooked. This series of blog about performance optimization I will be divided into 4 modules to share with you:


(1)Android performance optimization one SQLite database

(2)Android performance optimization one layout optimization

(3)Android performance optimization one data optimization

(4)Android performance Optimization one network optimization


Today, let's take a look at some of the things that need to be optimized and noticed in the operation of SQLite database in Android.

Android in the operation of the SQLite database, the system provides us with a very convenient implementation. All we need to do is create our own class to inherit the Sqliteopenhelper class and implement the OnCreate method and Onupgrade method in it . In the context of database optimization, SQLite and other databases such as Oracle, SQL Server, My SQL, and so on, have the same principle, but SQLite is an embedded database, in mobile phones to make it more appropriate. For SQLite optimization, we can be divided into the following two categories:


1> Index

2> Transactions


about database Creation Index It must be familiar to everyone. The benefits of indexing are obvious. A tens of data, the existence of the index to make the speed of query data sublimation to a better height. Similarly, the overhead of creating an index is not negligible at the same time as query speed increases. There are pros and cons, so creating an index in an appropriate situation can make a qualitative difference to our data query.

Knowing the pros and cons of creating indexes, let's look at how the SQLite database in Android creates indexes:




from the above code we see that in the OnCreate method, only the Sqlitedatabase instance db Call Execsql method is required to execute the SQL statement creating the index creation. The use of indexes is appropriate when a field is queried at a high frequency and the update operation frequency is low, and often a range of queries (>,< >=,<=,=) have been made to order by and the operation of Group by. Create an index for SQLite Let's talk about this here, for children's boots that don't know much about the index, you can read this article:SQLite Index


Knowing the index, let's go on to SQLite's business.


for a transaction, it is an atomic execution of the database. Atomic execution provides reliable security for the integrity of the data. In SQLite, if we have a default transaction (which creates a transaction for each insert and update , and commits the operation immediately after each insert and update), the transaction is not created manually, assuming there are 1000 data at this time, then the process of data execution is create transaction-- Commit the transaction with the INSERT or update operation , and the process executes 1000 times. If we created the transaction manually, the execution process is: Create transaction, execute 1000 SQL data operations, COMMIT transaction. Obviously, the use of transactions in SQLite can lead to great optimizations for insert and update operations. Here's how to use transactions in SQLite:




From what we see, the execution of a transaction is divided into four steps: start transaction, Execute SQL , set transaction execution Success , end transaction

OK, two main modules we've covered, here are some other optimizations for SQLite:

1> less with Cursor.getcolumnindex (): Using the cursor.getcolumnindex (), the system will be based on the column name to get the subscript of the column, more time-consuming, So we can use the static to define the subscript, directly through the subscript to get a column.

2> uses StringBuilder or StringBuffer to stitch strings : The concatenation of SQL statement strings or the creation of multiple temporary variables, at which point we can use the StringBuilder or StringBuffer to splice strings, reduce unnecessary resource consumption, this is not much to say, we all understand.

when 3> queries, only return the values or results that you need: Some boots may be simple, get all the data directly when querying the data, and then get to a column value that you want. Such operations can also lead to unnecessary system resource overhead and waste. So, when querying, we only take the fields and results we need.

4>cursor is closed when it is used : After the result is queried, call Cursor.close () to close the resource.

About the optimization of the SQLite database in Android, and we share here, there is a bad place to look at the great God to remind. Next, I will share with you the second in Android Optimization series:Android Performance optimization-layout optimization

Android Performance Optimization-sqlite database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.