Web服务器如何与数据库通信?

时间:2016-01-08 06:41:50

标签: mysql database architecture webserver web-application-design

这只是为了解释我认为它可能有效: 假设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
}

案例2:

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服务器和数据库之间实现?

1 个答案:

答案 0 :(得分:1)

流行的Web服务器和数据库可以以任何方式工作,具体取决于应用程序的编写方式。

尽管如此,除非您遇到极端情况,否则您可能会发现性能影响足够小,以至于您的主要关注应该是代码可维护性。从这个角度来看,通常首选格式化应用程序中的数据(在Web服务器上运行),因为维护在数据库级别实现的业务逻辑通常更难。

许多Web应用程序框架也将为您完成大部分格式化工作。