简体   繁体   English

使用node.js从mysql查询大量数据时,网页渲染失败

[英]Web page render fails when querying large amount of data from mysql using node.js

I am trying to query a mysql database from node.js and display its results on a web page. 我正在尝试从node.js查询mysql数据库并将其结果显示在网页上。 My code works when the size of the mysql table is small. 当mysql表的大小较小时,我的代码有效。 But when the size of the mysql table is big, around 4GB , then it takes quite some time to execute the query. 但是,当mysql表的大小很大(大约4GB)时,执行查询将花费相当长的时间。 Although the data is extracted from the mysql table, (I can see that in the node console as I am printing it out), the web page does not render the data. 尽管数据是从mysql表中提取的(在打印输出时,我可以在节点控制台中看到它),但网页不会呈现数据。 I am assuming that some timeout occurs and so, it does not render anything, just says 'web page not available'. 我假设发生一些超时,因此,它不会呈现任何内容,只是说“网页不可用”。

here is my code for it: 这是我的代码:

    var connection = mysql.createConnection({
         host: 'localhost',
         user: 'xxxx',
         password: 'xxxxxx',
         debug: true
       });
       connection.connect(function (err) {
          if (err) {
          console.log('error in connection is :' + err);
          res.send(500);
          } else {

          connection.query(query, function (err1, rows) {
          // connected! (unless `err` is set)
          if (err1) {
            console.log('error' + err1);
            var model = [{
                "error": "No data matching the query"
            }];
            res.json(model);

           } else {
            console.log('result of query' + rows);
            res.json(rows);
        }

    });
}

}); });

I am using dust.js for rendering the data. 我正在使用dust.js呈现数据。

Any pointers on what I am missing would be helpful. 任何有关我所缺少的内容的建议都会有所帮助。

You are probably not missing much, and it probably is the timeout you mention. 您可能不会错过太多,可能是您提到的超时。 It really depends on how you want your client to behave: 这实际上取决于您希望客户的行为方式:

1) You could increase the client's timeout (maybe it's an internal application, and it's OK to increase? Maybe it's an XHR object and you can control the timeout, let it wait "in the background" while the user is busy doing other things on the page?). 1)您可以增加客户端的超时(也许是内部应用程序,可以增加吗?也许是XHR对象,您可以控制超时,让它在用户忙于执行其他操作时“在后台”等待)这一页?)。

2) You could change the client-side code to do this in the background, updating the DOM once it gets the results. 2)您可以更改客户端代码以在后台执行此操作,并在获得结果后更新DOM。

3) You could have a server-side queue with the results, immediately returning to the client a "job id" and let the client poll on it. 3)您可能会有一个服务器端队列,其中包含结果,并立即向客户端返回一个“作业ID”,并让客户端对其进行轮询。 Or maybe use "comet" and send a notification to the client once the job is complete. 或者也许使用“ comet”并在作业完成后向客户端发送通知。

As I said, you really need to define how you want your client to behave. 正如我所说,您确实需要定义客户的行为方式。

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

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