具有大量数据的 Android 应用程序的最佳数据库设计选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12027852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:31:13  来源:igfitidea点击:

Best Database design option for Android application with huge data

androiddatabase

提问by Abhishek Sabbarwal

I am new to Android Application Development and a new member at stackoverflow. I am currently trying to design a recipe application. I have decided upon the features of the app and the scope it will cover. The scope is very vast for me in terms of covering all the recipes from all over the world. I am to deal with a lot of data in this process.

我是 Android 应用程序开发的新手,也是 stackoverflow 的新成员。我目前正在尝试设计一个配方应用程序。我已经决定了应用程序的功能及其涵盖的范围。就涵盖来自世界各地的所有食谱而言,范围对我来说非常广泛。我在这个过程中要处理很多数据。

I am currently trying to figure a good and efficient way of handling the data in my app. So far, as per what I have read in different forums, I believe that I have two options in terms of a database choice : 1) SQLite 2) Database on remote server (MySql/Postgre)

我目前正试图找出一种良好而有效的方法来处理我的应用程序中的数据。到目前为止,根据我在不同论坛上阅读的内容,我相信在数据库选择方面我有两个选择:1)SQLite 2)远程服务器上的数据库(MySql/Postgre)

Following are some of the thoughts that have been going on in my mind when it comes to taking a decision between the two :

以下是我在两者之间做出决定时一直在想的一些想法:

1) SQLite : This could be a good option but would be slow as it would need to access the file system. I could eliminate the slowness by performing DB data fetch tasks in the AsyncTask. But then there could be a limitation of the storage on different phones. Also I believe using SQLite would be easier as compared to using a remote DB.

1) SQLite :这可能是一个不错的选择,但会很慢,因为它需要访问文件系统。我可以通过在 AsyncTask 中执行数据库数据获取任务来消除缓慢。但是,不同手机上的存储空间可能会受到限制。另外我相信与使用远程数据库相比,使用 SQLite 会更容易。

2) Remote Database : The issue that I can see here is the slowness with multiple DB requests coming at the same time. Can I use threads here in some way to queue multiple requests and handle them one by one ? Is there an efficient way to do this.

2) 远程数据库:我在这里看到的问题是多个数据库请求同时到来的缓慢。我可以在这里以某种方式使用线程来排队多个请求并一个一个地处理它们吗?有没有一种有效的方法来做到这一点。

Also I have one more question in terms of the formatting of my data once I pull it out from the above DB's. Is there a way I could preserve the formatting of my data ?

此外,一旦我从上述数据库中取出数据,我还有一个关于数据格式的问题。有没有办法可以保留我的数据格式?

I would be more than thankful if someone could share their knowledgeable and expert comments on the above scenario. Also this is not a homework for me and I am not looking for any ready made code solutions. I am just looking for hints/suggestions that would help me clear my thoughts and help me take a decision. I have been looking for this for sometime now but was not able to find concrete information. I hope I will get some good advice here from the experienced people who might have encountered similar situation.

如果有人可以分享他们对上述情况的知识渊博和专家评论,我将不胜感激。此外,这对我来说不是作业,我也不是在寻找任何现成的代码解决方案。我只是在寻找可以帮助我理清思路并帮助我做出决定的提示/建议。我一直在寻找这个一段时间,但无法找到具体信息。我希望我能从可能遇到过类似情况的有经验的人那里得到一些好的建议。

Thanks for reading this long post.

感谢您阅读这篇长文。

采纳答案by zapl

What about combining both approaches?

结合这两种方法怎么样?

  • A local SQLite database that has the least recently used receipes so you don't need network all the time. Network is way slower than accessing the filesystem.

  • Some remote database accessed via some HTTP interface where you can read / write the whole database. And if you want users to be able to add receipes for other users to see you'll need an external database anyways.

  • 本地 SQLite 数据库具有最近最少使用的收据,因此您不需要一直使用网络。网络比访问文件系统慢得多。

  • 通过一些 HTTP 接口访问一些远程数据库,您可以在其中读取/写入整个数据库。如果您希望用户能够添加收据供其他用户查看,您无论如何都需要一个外部数据库。

SQLite : This could be a good option but would be slow as it would need to access the file system.

SQLite :这可能是一个不错的选择,但会很慢,因为它需要访问文件系统。

Accessing a local database is pretty fast, 5ms or so if it's just a simple read only query on a small database.

访问本地数据库非常快,如果它只是对小型数据库的简单只读查询,则需要 5 毫秒左右。

But then there could be a limitation of the storage on different phones

但是,不同手机上的存储空间可能会受到限制

Depends on your definition of huge database. It is okay if it is only 2MB which would be enough to store lots of text-only receipes.

取决于您对庞大数据库的定义。如果它只有 2MB,这足以存储大量纯文本的 receipes,那也没关系。

Also I believe using SQLite would be easier as compared to using a remote DB.

另外我相信与使用远程数据库相比,使用 SQLite 会更容易。

Yes, Android has a nice built-in SQLite API but no remote database API. And you don't need to setup a database server & interface.

是的,Android 有一个很好的内置 SQLite API,但没有远程数据库 API。而且您不需要设置数据库服务器和接口。

The issue that I can see here is the slowness with multiple DB requests coming at the same time.

我在这里看到的问题是多个数据库请求同时到来的缓慢。

A decent database server can handle thousands of requests. Depends on your server hardware & software. https://dba.stackexchange.com/should have more info on that. Required performance depends on how much users you have / expect.

一个体面的数据库服务器可以处理数千个请求。取决于您的服务器硬件和软件。https://dba.stackexchange.com/应该有更多信息。所需的性能取决于您拥有/期望的用户数量。

I'd suggest a simple REST interface to your database since it's pretty lightweight but does not expose your database directly to the web. There are tons of tutorialsand booksabout creating such interfaces to databases. There are even hosted database services like nextDbthat do most of the work for you.

我建议为您的数据库使用一个简单的 REST 接口,因为它非常轻巧,但不会将您的数据库直接暴露给网络。有大量关于创建数据库接口的教程书籍。甚至还有像nextDb这样的托管数据库服务可以为您完成大部分工作。

Is there a way I could preserve the formatting of my data ?

有没有办法可以保留我的数据格式?

You could store HTML formatted data in your database and display it in a WebViewor a TextView(via Html#fromHtml()) - both can display formatted text.

您可以将 HTML 格式的数据存储在您的数据库中并以 aWebView或 a TextView(via Html#fromHtml()) 形式显示 - 两者都可以显示格式化文本。

Databases don't care what type of text you store, for transfer over the internets you may need to encode the text so it does not interfere with the transport formatting (XML, JSON, ...).

数据库不关心你存储什么类型的文本,为了通过互联网传输,你可能需要对文本进行编码,这样它就不会干扰传输格式(XML、JSON 等)。

回答by markbratanov

A simple way is to integrate Parseinto your app. They have a nice framework that easily integrates into iOS and Android. Their plan is freemium, so you'll be able to use up to 1 million API request for no charge, and then its 7 cents for every request after that.

一种简单的方法是将Parse集成到您的应用程序中。他们有一个很好的框架,可以轻松集成到 iOS 和 Android 中。他们的计划是免费增值,因此您最多可以免费使用 100 万个 API 请求,之后每个请求只需 7 美分。

You'll have 1gb to store all your data sets / images, etc.

您将有 1gb 来存储所有数据集/图像等。

I don't use parse for everything, but I HIGHLY recommended it for large data schemes because they do all the scaling for you. Check out the API, I think it would be worth your time.

我不会对所有内容都使用 parse,但我强烈建议将其用于大数据方案,因为它们可以为您完成所有缩放。查看API,我认为值得您花时间。

I just started to work on a few of my own projects, and I'm using Parse again. I have to say it's improved a lot over the last 6-8 months. Especially with the Twitter and Facebook integration.

我刚刚开始处理我自己的一些项目,我再次使用 Parse。我不得不说它在过去的 6-8 个月里有了很大的改善。尤其是与 Twitter 和 Facebook 的整合。

回答by Elemental

The key issue here is the size of the data - any significant database of recipes would be too large to store on the phone imho,thus you seem stuck with the remote database solution.

这里的关键问题是数据的大小 - 任何重要的食谱数据库都会太大而无法存储在手机上,恕我直言,因此您似乎坚持使用远程数据库解决方案。

As opposed to trying access the remote database from android I suggest you use a a go between web application that will process requests from the application and return JSON objects that you need.

与尝试从 android 访问远程数据库相反,我建议您在 web 应用程序之间使用 aa go,它将处理来自应用程序的请求并返回您需要的 JSON 对象。

回答by Soumyadip Das

It totally depends on your software requirements. If you need to deal with a small amount of data then you may choose SQLite, but for a huge amount to data better use a remote DB.

这完全取决于您的软件要求。如果您需要处理少量数据,那么您可以选择 SQLite,但对于大量数据,最好使用远程数据库。

SQLite:It works fine with little amount of data & I experienced it response time is good.

SQLite:它在数据量很少的情况下运行良好,我体验到它响应时间很好。

Remote DB:I think you may use small server side app to submit the data to your client app. It will solve/reduce your thread related issues/complexities.

远程数据库:我认为您可以使用小型服务器端应用程序将数据提交到您的客户端应用程序。它将解决/减少与线程相关的问题/复杂性。