这只是为了解释我认为它可能是如何工作的:假设 Web 服务器需要来自 10 个表的数据。最终将在客户端显示的数据需要某种格式,可以在数据库或 Web 服务器上完成。假设获取一个表的原始数据的时间是 1 秒,而获取格式化数据的时间是一张表是 2 秒(格式化一张表的数据需要一秒钟,并且可以在 Web 服务器或数据库上轻松完成格式化。)
让我们考虑以下通信情况:
情况1:
for(i = 0; i < 10; i++)
{
table[i].getDataFromDB(); //2 sec - gets formatted datafrom DB, Call is completed before control goes to next statement
table[i].sendDataToClient(); //0 sec - control doesn't wait for completion of this step
}
案例二:
for(i = 0; i < 10; i++)
{
table[i].getDataFromDB(); //1 sec - gets raw data from DB, Call is completed before control goes to next statement
table[i].formatData(); //0 sec - will be executed as a parallel process which takes 1 sec to complete (control moves to next statement before completion)
}
formatData()
{
//format the data which takes 1 sec
sendDataToClient(); //0 sec - control doesn't wait for completion of this step
}
假设将数据从 Web 服务器发送到客户端不需要时间(0 秒),因为这两种情况都是恒定的。
情况1,每张表的数据会在客户端每隔2秒显示一次,20秒后会在客户端显示完整的数据。
在情况 2 中,第一个表的数据将在 2 秒后显示,但接下来 9 的数据将在 3,4,...,11 秒显示。
哪种方法是正确的,它是如何在流行的 Web 服务器和数据库之间实现的?