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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
| --------------
SET AUTOCOMMIT = 0
--------------
--------------
START TRANSACTION
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE `base`
DEFAULT CHARACTER SET `latin1`
DEFAULT COLLATE `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `membre`
--------------
--------------
CREATE TABLE `membre`
( `id` integer unsigned NOT NULL AUTO_INCREMENT primary key,
`pseudo` varchar(255) NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `membre` (`pseudo`) values
('albert'), ('moebius'), ('tournesol'), ('zebulon')
--------------
--------------
select * from membre
--------------
+----+-----------+
| id | pseudo |
+----+-----------+
| 1 | albert |
| 2 | moebius |
| 3 | tournesol |
| 4 | zebulon |
+----+-----------+
--------------
DROP TABLE IF EXISTS `message`
--------------
--------------
CREATE TABLE `message`
( `id` integer unsigned NOT NULL AUTO_INCREMENT,
`envoi` datetime NOT NULL,
`expediteur` integer unsigned NOT NULL,
`destinataire` integer unsigned NOT NULL,
CONSTRAINT `FK_01` FOREIGN KEY (`expediteur`) REFERENCES `membre` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_02` FOREIGN KEY (`destinataire`) REFERENCES `membre` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
primary key (`destinataire`,`envoi`),
unique index `idx` (`id`)
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `message` (`envoi`,`expediteur`,`destinataire`) values
('2016-06-01 12:25:17', 3, 4),
('2016-06-01 13:45:55', 2, 1),
('2016-06-01 14:05:23', 3, 1),
('2016-06-01 14:25:38', 2, 3),
('2016-06-01 15:01:00', 4, 3),
('2016-06-01 15:26:48', 1, 3),
('2016-06-01 16:25:33', 1, 2)
--------------
--------------
select * from message
--------------
+----+---------------------+------------+--------------+
| id | envoi | expediteur | destinataire |
+----+---------------------+------------+--------------+
| 2 | 2016-06-01 13:45:55 | 2 | 1 |
| 3 | 2016-06-01 14:05:23 | 3 | 1 |
| 7 | 2016-06-01 16:25:33 | 1 | 2 |
| 4 | 2016-06-01 14:25:38 | 2 | 3 |
| 5 | 2016-06-01 15:01:00 | 4 | 3 |
| 6 | 2016-06-01 15:26:48 | 1 | 3 |
| 1 | 2016-06-01 12:25:17 | 3 | 4 |
+----+---------------------+------------+--------------+
--------------
explain
select tb1.id,
tb1.envoi,
tb3.pseudo as 'expéditeur',
tb4.pseudo as 'destinataire'
from message as tb1
inner join membre as tb3
on tb3.id = tb1.expediteur
inner join membre as tb4
on tb4.id = tb1.destinataire
where tb1.destinataire = 3
--------------
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+----------------------------------------------------+
| 1 | SIMPLE | tb4 | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
| 1 | SIMPLE | tb1 | NULL | ref | PRIMARY,FK_01 | PRIMARY | 4 | const | 3 | 100.00 | NULL |
| 1 | SIMPLE | tb3 | NULL | ALL | PRIMARY | NULL | NULL | NULL | 4 | 25.00 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+----------------------------------------------------+
--------------
select tb1.id,
tb1.envoi,
tb3.pseudo as 'expéditeur',
tb4.pseudo as 'destinataire'
from message as tb1
inner join membre as tb3
on tb3.id = tb1.expediteur
inner join membre as tb4
on tb4.id = tb1.destinataire
where tb1.destinataire = 3
--------------
+----+---------------------+------------+--------------+
| id | envoi | expéditeur | destinataire |
+----+---------------------+------------+--------------+
| 6 | 2016-06-01 15:26:48 | albert | tournesol |
| 4 | 2016-06-01 14:25:38 | moebius | tournesol |
| 5 | 2016-06-01 15:01:00 | zebulon | tournesol |
+----+---------------------+------------+--------------+
--------------
explain
select tb1.id,
tb1.envoi,
tb3.pseudo as 'expéditeur',
tb4.pseudo as 'destinataire'
from message as tb1
left outer join message as tb2
on tb2.destinataire = 3
and tb2.envoi > tb1.envoi
inner join membre as tb3
on tb3.id = tb1.expediteur
inner join membre as tb4
on tb4.id = tb1.destinataire
where tb1.destinataire = 3
and tb2.id is null
--------------
+----+-------------+-------+------------+--------+---------------+---------+---------+---------------------+------+----------+-------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+--------+---------------+---------+---------+---------------------+------+----------+-------------------------+
| 1 | SIMPLE | tb4 | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
| 1 | SIMPLE | tb1 | NULL | ref | PRIMARY,FK_01 | PRIMARY | 4 | const | 3 | 100.00 | NULL |
| 1 | SIMPLE | tb2 | NULL | ref | PRIMARY | PRIMARY | 4 | const | 3 | 14.29 | Using where; Not exists |
| 1 | SIMPLE | tb3 | NULL | eq_ref | PRIMARY | PRIMARY | 4 | base.tb1.expediteur | 1 | 100.00 | NULL |
+----+-------------+-------+------------+--------+---------------+---------+---------+---------------------+------+----------+-------------------------+
--------------
select tb1.id,
tb1.envoi,
tb3.pseudo as 'expéditeur',
tb4.pseudo as 'destinataire'
from message as tb1
left outer join message as tb2
on tb2.destinataire = 3
and tb2.envoi > tb1.envoi
inner join membre as tb3
on tb3.id = tb1.expediteur
inner join membre as tb4
on tb4.id = tb1.destinataire
where tb1.destinataire = 3
and tb2.id is null
--------------
+----+---------------------+------------+--------------+
| id | envoi | expéditeur | destinataire |
+----+---------------------+------------+--------------+
| 6 | 2016-06-01 15:26:48 | albert | tournesol |
+----+---------------------+------------+--------------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager