[MDEV-6259] DELETE with OFFSET Created: 2014-05-22  Updated: 2015-11-17

Status: Open
Project: MariaDB Server
Component/s: None
Fix Version/s: None

Type: Task Priority: Major
Reporter: roberto spadim Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None


 Description   

Allow DELETE statment with OFFSET, this allow a DELETE like SQLITE:
http://www.sqlite.org/lang_delete.html

it's interesting with history table applications where a fixed number of rows is wanted, for example

CREATE TABLE history (
 channel_id INT NOT NULL DEFAULT 0,
 date_unix DECIMAL(22,6) NOT NULL DEFAULT 0,
 value DECIMAL(22,7) NOT NULL DEFAULT 0,
 PRIMARY KEY (channel_id,date_unix)
);

many inserts....

INSERT INTO history (1,now(),current_temperature);
INSERT INTO history (1,now(),current_temperature);
INSERT INTO history (1,now(),current_temperature);
INSERT INTO history (1,now(),current_temperature);
.....

an crond job executed at every hour:

DELETE FROM history WHERE channel_id=1 ORDER BY date_unix DESC LIMIT -1 OFFSET 5000

from SQLITE: A negative LIMIT value is interpreted as "no limit".

this will remove all rows from this 'channel' that are older than the 5000th row


today this should be done with two queries:

SELECT date_unix FROM history WHERE channel_id=1 ORDER BY date_unix DESC LIMIT 1 OFFSET 5000

get the date_unix value, and execute:

DELETE FROM history WHERE channel_id=1 AND date_unix<"{$date_unix from previous query$}"


Generated at Thu Feb 08 07:10:32 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.