我在StackOverflow中解决了很多问题,但是我想详细说明如何使用永久性临时(PermTemp)表。(永久温度,那不是矛盾吗?)
在StackOverflow中,我有存储过程test.CreateSampleTable和test.GetMissingIntegers创建一个示例表,然后创建一个动态临时表以进行填充,然后再进行大的JOIN查找差异。
这次,让我们与永久表一起创建样本表。
这是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 ;
运行此命令后,这里是表及其内容:
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)
这是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 ;
现在,让我们导入新的记录记录表test.weekly_batch,之前使用过的某些键,以及其他全新的键:
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
让我们进行test.weekly_batch并将其安全地合并到test.id_key_table_keys中,并形成表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 ;
结果如下:
mysql> call test.importweeklybatch;
+--------+
| id_key |
+--------+
| 29 |
| 31 |
| 37 |
| 41 |
+--------+
4 rows in set (0.14 sec)
从这一点来看,只需将new_keys_to_load表用作要导入的新品牌密钥列表即可。由于new_keys_to_load小于PermTemp表,因此应始终在LEFT JOIN左侧使用new_keys_to_load。