J'ai à peu près abordé cela dans StackOverflow , mais je voudrais en savoir plus sur l'utilisation de la table temporaire permanente (PermTemp). ( température permanente, n'est-ce pas un oxymore ?)
Dans StackOverflow , j'avais la procédure stockée test.CreateSampleTable et test.GetMissingIntegers créer un exemple de table, puis créer une table temporaire dynamique à remplir avant de faire le grand JOIN pour trouver des différences.
Cette fois, créons la table d'exemple avec la table de table permanente.
Voici test.LoadSampleTables:
DELIMITER $$
DROP PROCEDURE IF EXISTS `LoadSampleTables` $$
CREATE DEFINER=`lwdba`@`127.0.0.1` PROCEDURE `LoadSampleTables`(maxinttoload INT)
BEGIN
DECLARE X,OKTOUSE,MAXLOOP INT;
DROP TABLE IF EXISTS test.id_key_table;
DROP TABLE IF EXISTS test.id_key_table_keys;
CREATE TABLE test.id_key_table (id_key INT(16)) ENGINE=MyISAM;
CREATE TABLE test.id_key_table_keys (id_key INT(16)) ENGINE=MyISAM;
SET X=1;
WHILE X <= maxinttoload DO
INSERT INTO test.id_key_table VALUES (X);
SET X = X + 1;
END WHILE;
ALTER TABLE test.id_key_table ADD PRIMARY KEY (id_key);
SET MAXLOOP = FLOOR(SQRT(maxinttoload));
SET X = 2;
WHILE X <= MAXLOOP DO
DELETE FROM test.id_key_table WHERE MOD(id_key,X) = 0 AND id_key > X;
SELECT MIN(id_key) INTO OKTOUSE FROM test.id_key_table WHERE id_key > X;
SET X = OKTOUSE;
END WHILE;
OPTIMIZE TABLE test.id_key_table;
INSERT INTO test.id_key_table_keys SELECT id_key FROM test.id_key_table;
ALTER TABLE test.id_key_table_keys ADD PRIMARY KEY (id_key);
OPTIMIZE TABLE test.id_key_table_keys;
END $$
DELIMITER ;
Après avoir exécuté ceci, voici les tableaux et leur contenu:
mysql> call test.loadsampletables(25);
+-------------------+----------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+-------------------+----------+----------+----------+
| test.id_key_table | optimize | status | OK |
+-------------------+----------+----------+----------+
1 row in set (0.20 sec)
+------------------------+----------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------------------+----------+----------+----------+
| test.id_key_table_keys | optimize | status | OK |
+------------------------+----------+----------+----------+
1 row in set (0.28 sec)
Query OK, 0 rows affected (0.29 sec)
mysql> select * from test.id_key_table;
+--------+
| id_key |
+--------+
| 1 |
| 2 |
| 3 |
| 5 |
| 7 |
| 11 |
| 13 |
| 17 |
| 19 |
| 23 |
+--------+
10 rows in set (0.00 sec)
mysql> select * from test.id_key_table_keys;
+--------+
| id_key |
+--------+
| 1 |
| 2 |
| 3 |
| 5 |
| 7 |
| 11 |
| 13 |
| 17 |
| 19 |
| 23 |
+--------+
10 rows in set (0.00 sec)
Voici les déclencheurs de la table PermTemp
mysql> DELIMITER $$
mysql>
mysql> CREATE TRIGGER test.AddPermTempKey AFTER INSERT ON test.id_key_table
-> FOR EACH ROW
-> BEGIN
-> INSERT IGNORE INTO test.id_key_table_keys VALUES (NEW.id_key);
-> END $$
Query OK, 0 rows affected (0.09 sec)
mysql>
mysql> CREATE TRIGGER test.DeletePermTempKey AFTER DELETE ON test.id_key_table
-> FOR EACH ROW
-> BEGIN
-> DELETE FROM test.id_key_table_keys WHERE id_key = OLD.id_key;
-> END $$
Query OK, 0 rows affected (0.08 sec)
mysql>
mysql> DELIMITER ;
Maintenant, permet d'importer un nouveau lot d'enregistrements, la table test.weekly_batch, certaines clés utilisées auparavant, d'autres clés flambant neuves:
mysql> CREATE TABLE test.weekly_batch (id_key INT(16)) ENGINE=MyISAM;
Query OK, 0 rows affected (0.04 sec)
mysql> INSERT INTO test.weekly_batch VALUES (17),(19),(23),(29),(31),(37),(41);
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE test.weekly_batch ADD PRIMARY KEY (id_key);
Query OK, 7 rows affected (0.08 sec)
Records: 7 Duplicates: 0 Warnings: 0
Prenons test.weekly_batch et fusionnons-le en toute sécurité dans test.id_key_table_keys et formons la table test.new_keys_to_load:
DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`ImportWeeklyBatch` $$
CREATE PROCEDURE `test`.`ImportWeeklyBatch` ()
TheStoredProcedure:BEGIN
DECLARE RCOUNT INT;
SELECT COUNT(1) INTO RCOUNT FROM information_schema.tables
WHERE table_schema='test' AND table_name='weekly_batch';
IF RCOUNT = 0 THEN
LEAVE TheStoredProcedure;
END IF;
SELECT COUNT(1) INTO RCOUNT FROM test.weekly_batch;
IF RCOUNT = 0 THEN
LEAVE TheStoredProcedure;
END IF;
DROP TABLE IF EXISTS test.new_keys_to_load;
CREATE TABLE test.new_keys_to_load (id_key INT(16));
INSERT INTO test.new_keys_to_load (id_key)
SELECT id_key FROM test.weekly_batch A
LEFT JOIN test.id_key_table_keys B USING (id_key)
WHERE B.id_key IS NULL;
SELECT * FROM test.new_keys_to_load;
END $$
DELIMITER ;
Voici le résultat:
mysql> call test.importweeklybatch;
+--------+
| id_key |
+--------+
| 29 |
| 31 |
| 37 |
| 41 |
+--------+
4 rows in set (0.14 sec)
À partir de ce moment, utilisez simplement la table new_keys_to_load comme liste des nouvelles clés fessées à importer. Étant donné que new_keys_to_load est plus petit que la table PermTemp, vous devez toujours utiliser new_keys_to_load sur le côté gauche de LEFT JOIN.