繁体   English   中英

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

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

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

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

我建议您一次调用该方法,并让它填充一个像Dictionary这样的集合,以便您可以从用户ID密钥快速查找Username值。

像这样:

// 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();

暂无
暂无

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

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