1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| --------------
START TRANSACTION
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE IF NOT EXISTS `base`
DEFAULT CHARACTER SET `latin1`
DEFAULT COLLATE `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `test`
--------------
--------------
CREATE TABLE `test`
( `id` timestamp(6) NOT NULL,
`lib` varchar(255) NOT NULL,
`annee` smallint unsigned GENERATED ALWAYS AS (year(`id`)) stored,
`mois` tinyint unsigned GENERATED ALWAYS AS (month(`id`)) stored,
`jour` tinyint unsigned GENERATED ALWAYS AS (day(`id`)) stored,
primary key (`annee`,`mois`,`jour`,`id`)
) ENGINE=Innodb
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
PARTITION BY RANGE COLUMNS (`annee`,`mois`)
(
PARTITION p2022s09 values less than (2022,00),
PARTITION p2022s10 values less than (2022,11),
PARTITION p2022s11 values less than (2022,12),
PARTITION p2022s12 values less than (2023,01),
PARTITION p2023s01 values less than (2023,02),
PARTITION p2023s02 values less than (2023,03),
PARTITION p2023s03 values less than (2023,04),
PARTITION p2023s04 values less than (maxvalue,maxvalue)
)
--------------
--------------
insert into `test` (`id`,`lib`) values
('2022-12-01 00:00:00.000001','Un'),
('2022-12-02 00:00:00.000001','Deux'),
('2022-12-03 00:00:00.000001','Trois'),
('2022-11-01 00:00:00.000001','Quatre'),
('2022-11-02 00:00:00.000001','Cinq'),
('2022-11-03 00:00:00.000001','Six'),
('2022-11-04 00:00:00.000001','Sept'),
('2022-10-01 00:00:00.000001','Huit'),
('2022-10-02 00:00:00.000001','neuf'),
('2022-10-03 00:00:00.000001','Dix'),
('2022-09-01 00:00:00.000001','Onze'),
('2022-09-02 00:00:00.000001','Douze'),
('2022-09-03 00:00:00.000001','Treize'),
('2022-09-04 00:00:00.000001','Quatorze')
--------------
--------------
select * from `test`
--------------
+----------------------------+----------+-------+------+------+
| id | lib | annee | mois | jour |
+----------------------------+----------+-------+------+------+
| 2022-09-01 00:00:00.000001 | Onze | 2022 | 9 | 1 |
| 2022-09-02 00:00:00.000001 | Douze | 2022 | 9 | 2 |
| 2022-09-03 00:00:00.000001 | Treize | 2022 | 9 | 3 |
| 2022-09-04 00:00:00.000001 | Quatorze | 2022 | 9 | 4 |
| 2022-10-01 00:00:00.000001 | Huit | 2022 | 10 | 1 |
| 2022-10-02 00:00:00.000001 | neuf | 2022 | 10 | 2 |
| 2022-10-03 00:00:00.000001 | Dix | 2022 | 10 | 3 |
| 2022-11-01 00:00:00.000001 | Quatre | 2022 | 11 | 1 |
| 2022-11-02 00:00:00.000001 | Cinq | 2022 | 11 | 2 |
| 2022-11-03 00:00:00.000001 | Six | 2022 | 11 | 3 |
| 2022-11-04 00:00:00.000001 | Sept | 2022 | 11 | 4 |
| 2022-12-01 00:00:00.000001 | Un | 2022 | 12 | 1 |
| 2022-12-02 00:00:00.000001 | Deux | 2022 | 12 | 2 |
| 2022-12-03 00:00:00.000001 | Trois | 2022 | 12 | 3 |
+----------------------------+----------+-------+------+------+
--------------
explain
delete from `test`
where `annee` = 2023
and `mois` = 9
and `jour` != 1
--------------
+----+-------------+-------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------------+
| 1 | DELETE | test | p2023s04 | range | PRIMARY | PRIMARY | 4 | const,const,const | 2 | 100.00 | Using where |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------------+
--------------
delete from `test`
where `annee` = 2022
and `mois` = 9
and `jour` != 1
--------------
--------------
select * from `test`
--------------
+----------------------------+--------+-------+------+------+
| id | lib | annee | mois | jour |
+----------------------------+--------+-------+------+------+
| 2022-09-01 00:00:00.000001 | Onze | 2022 | 9 | 1 |
| 2022-10-01 00:00:00.000001 | Huit | 2022 | 10 | 1 |
| 2022-10-02 00:00:00.000001 | neuf | 2022 | 10 | 2 |
| 2022-10-03 00:00:00.000001 | Dix | 2022 | 10 | 3 |
| 2022-11-01 00:00:00.000001 | Quatre | 2022 | 11 | 1 |
| 2022-11-02 00:00:00.000001 | Cinq | 2022 | 11 | 2 |
| 2022-11-03 00:00:00.000001 | Six | 2022 | 11 | 3 |
| 2022-11-04 00:00:00.000001 | Sept | 2022 | 11 | 4 |
| 2022-12-01 00:00:00.000001 | Un | 2022 | 12 | 1 |
| 2022-12-02 00:00:00.000001 | Deux | 2022 | 12 | 2 |
| 2022-12-03 00:00:00.000001 | Trois | 2022 | 12 | 3 |
+----------------------------+--------+-------+------+------+
--------------
COMMIT
--------------
Appuyez sur une touche pour continuer... |
Partager