我如何实现在Android中使用SQLite最佳性能? [英] How do I achieve best performance using SQLite in Android?

查看:270
本文介绍了我如何实现在Android中使用SQLite最佳性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经注意到,有多种方法可以做到SQLite的操作(查询,插入,更新,删除),有的可能比其他地区更快。许多网站提供不同的技巧,并与其他人有些冲突。

  1. 看来,使用交易批量插入是有点快于在一个循环中这样做。

    怎么会呢?什么是使用SQLite时达到最佳性能的最好方法是什么?如何在Android上做sqlite的工作?

  2. 混淆使用<一个href="http://developer.android.com/reference/android/database/DatabaseUtils.InsertHelper.html">InserHelper VS ContentValues​​ ,如<一个href="http://www.outofwhatbox.com/blog/2010/12/android-using-databaseutils-inserthelper-for-faster-insertions-into-sqlite-database/">here.

    如何在<一个href="http://developer.android.com/reference/android/database/DatabaseUtils.InsertHelper.html">InserHelper工作,而且它总是比 ContentValues​​ 更快?将其包装与交易速度起来更进一步?

  3. 混乱其他SQL框架。我记得,加入指数将推动相关指数查询的性能。这是真的为Android吗?这是一个好的做法呢?

  4. 我听说,在某些情况下,最好不要使用UI线程,数据库操作。在这情况下,这是建议?如,怎么可能会慢DB操作变成什么?如果数据库变得70MB,这是否意味着这将是又慢又UI线程不应该处理呢?

解决方案
  

怎么来的?

交易中,磁盘I / O。这是更便宜的做磁盘I / O的一个容量较大的块比许多磁盘的小数据块I / O。

  

请问该怎么InserHelper工作,并将它总是比contentValues​​快?

引述的文档:这个类允许用户做多插入到一个表中,但编译SQL INSERT语句只有一次,这可能会提高性能。

  

将其包装与交易速度甚至进一步?

presumably肯定的。

  

这是真的为Android呢?

索引的可以的提高查询速度,如果你创建正确的索引,你正在执行什么查询。索引的总是减少的插入/更新/删除的速度,因为现在这些索引需要更新。这有什么特别做机器人。

  

时,是一件好事使用?

这是不可能的,在抽象的回答。这有什么特别做机器人。

  

这是最好不要使用DB操作UI线程

正确的。

  

在这种情况下,将它推荐?

这是从来没有建议进行任何磁盘I / O的主应用程序线程上。

  

怎么能慢DB操作变成什么?

世纪。也许到那时,事情会在设备出现故障,否则就会耗尽电池电量。

  

如果数据库变为70MB,这是否意味着,这将是非常慢

这取决于你用​​它做什么。

  

和UI线程不应该处理呢?

正确的。

I've noticed that there are multiple ways to do SQLite operations (query, insert, update, delete), and some can be faster than the rest. Many websites provide different tips, and some conflict with the others.

  1. It seems that using a transaction for bulk insert is somehow faster than doing so in a loop.

    How come? What is the best way to achieve best performance when using SQLite? How does sqlite work on Android?

  2. Confusing with using InserHelper vs ContentValues, as shown here.

    How does the InserHelper work and will it always be faster than ContentValues? Will wrapping it with a transaction speed it up even further?

  3. Confusion on other SQL frameworks. I remember that adding indices would boost the performance of queries related to the indices. Is this true for Android too? Is this a good practice?

  4. I've heard that in some cases, it's best to not use the UI thread for DB operations. In which cases is this recommended? As in, how slow could DB operation become? If the DB becomes 70MB, does it mean that it will be much slower and UI thread should never handle it?

解决方案

how come?

Transactions involve disk I/O. It is much less expensive to do one larger chunk of disk I/O than lots of little chunks of disk I/O.

how does the InserHelper work and will it always be faster than contentValues?

Quoting the documentation: "This class allows users to do multiple inserts into a table but compile the SQL insert statement only once, which may increase performance."

will wrapping it with a transaction speed it even further ?

Presumably yes.

is it true for android too ?

Indexes can increase query speed, if you create the proper indexes for whatever queries you are performing. Indexes always decrease insert/update/delete speed, as now those indexes need to be updated. This has nothing in particular to do with Android.

is is a good thing to use ?

That is impossible to answer in the abstract. This has nothing in particular to do with Android.

it's best to not use the UI thread for DB operations

Correct.

in which cases will it be recommended ?

It is never recommended to perform any disk I/O on the main application thread.

how slow could DB operation become ?

Centuries. Probably by then, something will fail in the device, or it will run out of battery power.

if the DB becomes 70MB , does it mean that it will be much slower

It depends upon what you do with it.

and UI thread should never handle it ?

Correct.

这篇关于我如何实现在Android中使用SQLite最佳性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆