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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
--------------
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 `machine`
--------------
--------------
CREATE TABLE `machine`
( `id_machine` integer unsigned NOT NULL auto_increment primary key,
`nom` varchar(255) NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `machine` (`nom`) values ('machine 1'),('machine 2'),('machine 3'),('machine 4'),('machine 5'),('machine 6')
--------------
--------------
select * from machine
--------------
+------------+-----------+
| id_machine | nom |
+------------+-----------+
| 1 | machine 1 |
| 2 | machine 2 |
| 3 | machine 3 |
| 4 | machine 4 |
| 5 | machine 5 |
| 6 | machine 6 |
+------------+-----------+
--------------
DROP TABLE IF EXISTS `piece`
--------------
--------------
CREATE TABLE `piece`
( `id_piece` integer unsigned NOT NULL auto_increment primary key,
`clef_machine` integer unsigned NOT NULL,
`nom` varchar(255) NOT NULL,
`capacite` integer unsigned NOT NULL,
CONSTRAINT `FK_PIECE_MACHINE` FOREIGN KEY (`clef_machine`) REFERENCES `machine` (`id_machine`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `piece` (`clef_machine`,`nom`,`capacite`) values
(1, 'piece 1', 150), (1, 'piece 2', 125), (1, 'piece 3', 250), (1, 'piece 4', 375), (1, 'piece 5', 400),
(2, 'piece 1', 200), (2, 'piece 2', 250), (2, 'piece 3', 200), (2, 'piece 4', 150),
(3, 'piece 1', 125), (3, 'piece 2', 275), (3, 'piece 3', 225), (3, 'piece 4', 175),
(4, 'piece 1', 300), (4, 'piece 2', 275), (4, 'piece 3', 225), (4, 'piece 4', 175),
(5, 'piece 1', 210), (5, 'piece 2', 255), (5, 'piece 3', 225), (5, 'piece 4', 150), (5, 'piece 5', 175),
(6, 'piece 1', 250), (6, 'piece 2', 200), (6, 'piece 3', 150), (6, 'piece 4', 200)
--------------
--------------
select * from piece
--------------
+----------+--------------+---------+----------+
| id_piece | clef_machine | nom | capacite |
+----------+--------------+---------+----------+
| 1 | 1 | piece 1 | 150 |
| 2 | 1 | piece 2 | 125 |
| 3 | 1 | piece 3 | 250 |
| 4 | 1 | piece 4 | 375 |
| 5 | 1 | piece 5 | 400 |
| 6 | 2 | piece 1 | 200 |
| 7 | 2 | piece 2 | 250 |
| 8 | 2 | piece 3 | 200 |
| 9 | 2 | piece 4 | 150 |
| 10 | 3 | piece 1 | 125 |
| 11 | 3 | piece 2 | 275 |
| 12 | 3 | piece 3 | 225 |
| 13 | 3 | piece 4 | 175 |
| 14 | 4 | piece 1 | 300 |
| 15 | 4 | piece 2 | 275 |
| 16 | 4 | piece 3 | 225 |
| 17 | 4 | piece 4 | 175 |
| 18 | 5 | piece 1 | 210 |
| 19 | 5 | piece 2 | 255 |
| 20 | 5 | piece 3 | 225 |
| 21 | 5 | piece 4 | 150 |
| 22 | 5 | piece 5 | 175 |
| 23 | 6 | piece 1 | 250 |
| 24 | 6 | piece 2 | 200 |
| 25 | 6 | piece 3 | 150 |
| 26 | 6 | piece 4 | 200 |
+----------+--------------+---------+----------+
--------------
select distinct mch.id_machine
from machine as mch
inner join piece as pc1
on mch.id_machine = pc1.clef_machine
inner join piece as pc2
on mch.id_machine = pc2.clef_machine
inner join piece as pc3
on mch.id_machine = pc3.clef_machine
inner join piece as pc4
on mch.id_machine = pc4.clef_machine
where pc1.id_piece not in (pc2.id_piece, pc3.id_piece, pc4.id_piece)
and pc2.id_piece not in (pc1.id_piece, pc3.id_piece, pc4.id_piece)
and pc3.id_piece not in (pc1.id_piece, pc2.id_piece, pc4.id_piece)
and pc4.id_piece not in (pc1.id_piece, pc2.id_piece, pc3.id_piece)
and pc1.capacite >= 200
and pc2.capacite >= 250
and pc3.capacite >= 200
and pc4.capacite >= 150
and exists ( select null from piece as pcs where mch.id_machine = pcs.clef_machine having count(*) = 4)
--------------
+------------+
| id_machine |
+------------+
| 2 |
| 4 |
| 6 |
+------------+
--------------
explain select distinct mch.id_machine
from machine as mch
inner join piece as pc1
on mch.id_machine = pc1.clef_machine
inner join piece as pc2
on mch.id_machine = pc2.clef_machine
inner join piece as pc3
on mch.id_machine = pc3.clef_machine
inner join piece as pc4
on mch.id_machine = pc4.clef_machine
where pc1.id_piece not in (pc2.id_piece, pc3.id_piece, pc4.id_piece)
and pc2.id_piece not in (pc1.id_piece, pc3.id_piece, pc4.id_piece)
and pc3.id_piece not in (pc1.id_piece, pc2.id_piece, pc4.id_piece)
and pc4.id_piece not in (pc1.id_piece, pc2.id_piece, pc3.id_piece)
and pc1.capacite >= 200
and pc2.capacite >= 250
and pc3.capacite >= 200
and pc4.capacite >= 150
and exists ( select null from piece as pcs where mch.id_machine = pcs.clef_machine having count(*) = 4)
--------------
+----+--------------------+-------+--------+--------------------------+------------------+---------+-----------------------+------+--------------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------+--------+--------------------------+------------------+---------+-----------------------+------+--------------------------------------------------------------+
| 1 | PRIMARY | pc1 | ALL | PRIMARY,FK_PIECE_MACHINE | NULL | NULL | NULL | 26 | Using where; Using temporary |
| 1 | PRIMARY | mch | eq_ref | PRIMARY | PRIMARY | 4 | base.pc1.clef_machine | 1 | Using where; Using index |
| 1 | PRIMARY | pc2 | ALL | PRIMARY,FK_PIECE_MACHINE | NULL | NULL | NULL | 26 | Using where; Distinct; Using join buffer (Block Nested Loop) |
| 1 | PRIMARY | pc3 | ALL | PRIMARY,FK_PIECE_MACHINE | NULL | NULL | NULL | 26 | Using where; Distinct; Using join buffer (Block Nested Loop) |
| 1 | PRIMARY | pc4 | ALL | PRIMARY,FK_PIECE_MACHINE | NULL | NULL | NULL | 26 | Range checked for each record (index map: 0x3); Distinct |
| 2 | DEPENDENT SUBQUERY | pcs | ref | FK_PIECE_MACHINE | FK_PIECE_MACHINE | 4 | base.mch.id_machine | 13 | Using index |
+----+--------------------+-------+--------+--------------------------+------------------+---------+-----------------------+------+--------------------------------------------------------------+
--------------
select id_machine
from (
select m.id_machine,
p1.capacite,
count(p2.id_piece)+1 as rang
from machine as m
left outer join piece as p1
on p1.clef_machine = m.id_machine
left outer join piece as p2
on p2.clef_machine = m.id_machine
and p2.id_piece < p1.id_piece
group by m.id_machine, p1.id_piece
having ((rang=1 and capacite>=200)
or (rang=2 and capacite>=250)
or (rang=3 and capacite>=200)
or (rang=4 and capacite>=150))
order by m.id_machine, p1.id_piece
) as s
group by id_machine
having count(*) = ( select count(*) from piece as p3 where p3.clef_machine = s.id_machine )
--------------
+------------+
| id_machine |
+------------+
| 2 |
| 4 |
+------------+
--------------
explain select id_machine
from (
select m.id_machine,
p1.capacite,
count(p2.id_piece)+1 as rang
from machine as m
left outer join piece as p1
on p1.clef_machine = m.id_machine
left outer join piece as p2
on p2.clef_machine = m.id_machine
and p2.id_piece < p1.id_piece
group by m.id_machine, p1.id_piece
having ((rang=1 and capacite>=200)
or (rang=2 and capacite>=250)
or (rang=3 and capacite>=200)
or (rang=4 and capacite>=150))
order by m.id_machine, p1.id_piece
) as s
group by id_machine
having count(*) = ( select count(*) from piece as p3 where p3.clef_machine = s.id_machine )
--------------
+----+--------------------+------------+-------+--------------------------+------------------+---------+-------------------+------+----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+------------+-------+--------------------------+------------------+---------+-------------------+------+----------------------------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 1560 | Using temporary; Using filesort |
| 3 | DEPENDENT SUBQUERY | p3 | ref | FK_PIECE_MACHINE | FK_PIECE_MACHINE | 4 | func | 13 | Using index |
| 2 | DERIVED | m | index | NULL | PRIMARY | 4 | NULL | 6 | Using index; Using temporary; Using filesort |
| 2 | DERIVED | p1 | ALL | FK_PIECE_MACHINE | NULL | NULL | NULL | 26 | Using where; Using join buffer (Block Nested Loop) |
| 2 | DERIVED | p2 | ref | PRIMARY,FK_PIECE_MACHINE | FK_PIECE_MACHINE | 4 | base.m.id_machine | 13 | Using where; Using index |
+----+--------------------+------------+-------+--------------------------+------------------+---------+-------------------+------+----------------------------------------------------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager