【问题标题】:What is the fastest way of retrieving data from mysql database hosted online?从在线托管的 mysql 数据库中检索数据的最快方法是什么?
【发布时间】:2018-11-12 18:41:06
【问题描述】:

我目前正在构建一个将数据存储在远程数据库中的 android 应用程序,并且我想将数据检索到应用程序,但这需要一段时间。所以我想知道将检索数据的数据发送到 Android 的最佳方式(Sqlite 会提高应用程序的性能吗?)。

【问题讨论】:

  • sqlite 在本地托管,所以是的,这样会更快

标签: java android-studio sqlite android-sqlite


【解决方案1】:

您经常调用 GetDataFromDB(string query) 方法。这很糟糕,因为您每次都创建一个新的 SqlConnection 和 SqlCommand。这需要时间和资源。此外,如果有任何网络延迟,则乘以您正在拨打的电话数量。所以这只是个坏主意。

我建议您调用一次该方法并让它像字典一样填充集合,以便您可以快速从用户 ID 键中查找您的用户名值。

像这样:

// In the DataField class, have this code.
// This method will query the database for all usernames and user ids and
// return a Dictionary<int, string> where the key is the Id and the value is the 
// username. Make this a global variable within the DataField class.
Dictionary<int, string> usernameDict = GetDataFromDB("select id, username from Users");

// Then in the GetValue(int userId) method, do this:
public string GetValue(int userId)
{
    // Add some error handling and whatnot. 
    // And a better name for this method is GetUsername(int userId)
    return this.usernameDict[userId];
}

建议 2 这是您可以改进的另一种方法,尽管在这种情况下略有改进 - 使用 StringBuilder 类。有显着的性能提升(这里是一个概述:http://support.microsoft.com/kb/306822)。

SringBuilder sb = new StringBuilder();
sb.Append("<table><tr><th>Username</th>");
foreach (DataField f in fields)
{
    sb.Append("<th>" + f.Name + "</th>");
}

// Then, when you need the string
string html = sb.ToString();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 2016-04-20
    相关资源
    最近更新 更多