简体   繁体   English

从在线托管的mysql数据库中检索数据的最快方法是什么?

[英]What is the fastest way of retrieving data from mysql database hosted online?

I'm currently building an android app that stores data on a remote database, and I want to retrieve the data to the app, but this takes a while. 我目前正在构建一个将数据存储在远程数据库上的android应用,并且我想将数据检索到该应用,但这需要一段时间。 So I want to know the best way to send data of retrieving the data to Android(Would Sqlite increase the performance of the app?). 所以我想知道发送数据到Android的最佳方法(Sqlite是否会提高应用程序的性能?)。

You call the method GetDataFromDB(string query) very often. 您经常调用方法GetDataFromDB(string query)。 This is bad because you create a new SqlConnection and SqlCommand each time. 这很不好,因为您每次都创建一个新的SqlConnection和SqlCommand。 This takes time and resources. 这需要时间和资源。 Also, if there is any network delay, that is multiplied by the number of calls you are making. 同样,如果有任何网络延迟,则将其乘以您正在拨打的电话数。 So it's just a bad idea. 因此,这是一个坏主意。

I suggest that you call that method once and have it populate a collection like a Dictionary so that you can quickly look up your Username value from the user id key. 我建议您一次调用该方法,并让它填充一个像Dictionary这样的集合,以便您可以从用户ID密钥快速查找Username值。

Like this: 像这样:

// 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];
}

Suggestion 2 Here is another way that you can improve things, though slightly in this case—use the StringBuilder class. 建议2这是另一种可以改进的方法,尽管在这种情况下可以稍作改进-使用StringBuilder类。 There are significant performance gains (here is an overview: http://support.microsoft.com/kb/306822 ). 性能显着提高(这里是概述: 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM