1

I am building a monitoring application for a machine, position must be read and stored every second for a period of a month. I wrote a procedure to fill a table with initial 0 value.

CREATE DEFINER=`root`@`localhost` PROCEDURE `new_procedure`(start_date datetime, end_date datetime)
BEGIN

DECLARE interval_var INT DEFAULT 1;

    WHILE end_date >= start_date DO

        INSERT INTO table1(datetime, value) VALUES(start_date, 0);
        SET start_date = DATE_ADD(start_date, INTERVAL interval_var SECOND);

    END WHILE;

END

This process is very slow, and most of the time the connection with the sql database is lost. For example, when I try to fill the table from "2016-01-14 07:00:00" to "2016-01-15 07:00:00" the procedure reached 2016-01-14 07:16:39 and crashed due to lost connection with database.

Is there a more efficient way to create a template table for a month with second increments and 0 values? My monitoring application is built on vb.net and I have tried to create a code on vb to create this template table, but it was slower and more likely to crash than direct procedure on mysql workstation.

4

1 回答 1

0

我建议首先查看应用程序架构。期望整个系统在整个月内运行甚至 1 秒都不会出现故障,这要求非常高。另外,试着想想你是否真的需要这么多数据。

1 条记录/秒*3600 秒/小时*24 小时/天*30 天/月超过 310 万条记录。试图处理这么多信息会导致很多软件窒息。

如果测量是离散的,您可以通过仅记录数据库中的更改来显着减少这种情况。如果是模拟的,你可能别无选择。

我建议创建两个单独的应用程序:一个存储本地数据的本地监视器,然后每隔一小时左右向 mysql 服务器应用程序报告。这样,如果数据库不可用,它会继续收集数据,当数据库再次可用时,它可以传输自上次连接以来记录的所有内容。然后mysql应用就可以一键将数据存入数据库。如果失败,它可以重试,并保留它自己的数据副本,直到它被存储到数据库中。

前任:

machine => 监控站app => mysql app => mysql 数据库

这需要更多的工作,但每个应用程序都非常小,它们甚至可以重用。它将使故障排除变得更加容易,并显着提高系统的容错能力。

于 2016-01-13T21:02:43.290 回答