Make data rotation with MariaDB database
1
vote
2
answers
282
views
We have a small local computer that we use as a temporary database backup to automatically recover data from sensors when we lost connection to the main cloud server or when we have to make maintenance to this server.
Due to the small space disk available for data (32go) the idea was to keep last 3 month of sensors data and delete oldest entries.
Here is the description of the database:
CREATE TABLE IF NOT EXISTS machine
(
Nom
varchar(50) NOT NULL,
Site
smallint(5) unsigned NOT NULL,
Emplacement
smallint(5) unsigned DEFAULT NULL,
ID
smallint(5) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (ID
),
UNIQUE KEY Nom
(Nom
),
KEY FK__site
(Site
),
CONSTRAINT FK__site
FOREIGN KEY (Site
) REFERENCES site
(ID
)
) ENGINE=InnoDB AUTO_INCREMENT=77 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS mesure
(
Machine
smallint(5) unsigned NOT NULL,
Date
datetime NOT NULL,
Valeur
decimal(13,2) NOT NULL,
PRIMARY KEY (Machine
,Date
),
KEY Date
(Date
) USING BTREE,
CONSTRAINT FK__machine
FOREIGN KEY (Machine
) REFERENCES machine
(ID
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS site
(
Nom
varchar(50) NOT NULL,
ID
smallint(5) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (ID
)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
In a first time I thought that making periodic deletion will be sufficent but I discovered that innoDB does not release space disk to the OS, and now the disk partition is full (filled in 4 month). More, these delete are bit slow.
After some researches I found that partitions can be the solution to my issue but it seems that it's not possible to make partition with a foreign key inside the table.
Tried this code, just for testing purpose :
ALTER TABLE mesure
PARTITION BY RANGE(UNIX_TIMESTAMP(Date))
(
PARTITION START VALUES LESS THAN (UNIX_TIMESTAMP("2021-10-01 00:00:00")),
PARTITION MONTH1 VALUES LESS THAN (UNIX_TIMESTAMP("2021-11-01 00:00:00")),
PARTITION MONTH2 VALUES LESS THAN (UNIX_TIMESTAMP("2021-12-01 00:00:00")),
PARTITION MONTH3 VALUES LESS THAN (UNIX_TIMESTAMP("2022-01-01 00:00:00")),
PARTITION END VALUES LESS THAN MAXVALUE
);
That results to : /* Erreur SQL (1217) : Cannot delete or update a parent row: a foreign key constraint fails */
I'm now run out solutions to make this solution viable on our hardware.
Any idea on how I can manage this ?
Asked by Afaeld
(23 rep)
Sep 13, 2022, 05:55 PM
Last activity: May 12, 2025, 08:03 PM
Last activity: May 12, 2025, 08:03 PM