开发者

Persistent/static variables inside a MySQL trigger

开发者 https://www.devze.com 2023-03-02 07:36 出处:网络
I have the following trigger on a blackhole table, that intercepts inserts and passes them on to other tables.

I have the following trigger on a blackhole table, that intercepts inserts and passes them on to other tables.

In order to speed things up I want to calculate an increasing value and passes that in my inserts.

DELIMITER $$

CREATE TRIGGER ai_blackhole_each AFTER INSERT ON `test.blackhole` FOR EACH ROW
BEGIN
  DECLARE calculated_id INTEGER;

  SET calculated_id = calc_id_for_previous_insert + 1;
  INSERT INTO example VALUES(new.field1, new.field2, calculated_id, ..);
END$$

DELIMITER ;

Can I have a static variable inside a trigger that k开发者_如何学编程eeps its value between firings?

Or is there a trick to achieve something like that efficiently?


Place an intermittent value in a MEMORY table starting at 0

use test
DROP TABLE IF EXISTS test.blackholecounter;
DROP TABLE IF EXISTS test.blackhole;
DROP TABLE IF EXISTS test.example;
CREATE TABLE test.blackholecounter (calc_id INT NOT NULL DEFAULT 0) ENGINE=MEMORY;
INSERT INTO test.blackholecounter VALUES (0);
CREATE TABLE test.blackhole
(
    field1 VARCHAR(20),
    field2 VARCHAR(20)
) ENGINE=BLACKHOLE;
CREATE TABLE test.example (field1 VARCHAR(20),
field2 VARCHAR(20),
calc_id INT);
DELIMITER $$
CREATE TRIGGER ai_blackhole_each AFTER INSERT ON test.blackhole
FOR EACH ROW
BEGIN
    DECLARE calculated_id INTEGER;
    SELECT calc_id INTO calculated_id FROM test.blackholecounter;
    UPDATE test.blackholecounter SET calc_id=calc_id+1;
    INSERT INTO test.example VALUES(new.field1, new.field2, calculated_id);
END
$$
DELIMITER ;
SELECT * FROM test.blackholecounter;
SELECT * FROM test.example;
INSERT INTO test.blackhole (field1,field2) VALUES ('rolando','edwards'),('pamela','edwards');
SELECT * FROM test.blackholecounter;
SELECT * FROM test.example;

Here is what I got when I pasted this into MySQL

mysql> use test
Database changed
mysql> DROP TABLE IF EXISTS test.blackholecounter;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS test.blackhole;
Query OK, 0 rows affected (0.01 sec)

mysql> DROP TABLE IF EXISTS test.example;
Query OK, 0 rows affected (0.06 sec)

mysql> CREATE TABLE test.blackholecounter (calc_id INT NOT NULL DEFAULT 0) ENGINE=MEMORY;
Query OK, 0 rows affected (0.05 sec)

mysql> INSERT INTO test.blackholecounter VALUES (0);
Query OK, 1 row affected (0.00 sec)

mysql> CREATE TABLE test.blackhole(field1 VARCHAR(20),field2 VARCHAR(20)) ENGINE=BLACKHOLE;
Query OK, 0 rows affected (0.06 sec)

mysql> CREATE TABLE test.example (field1 VARCHAR(20),field2 VARCHAR(20),calc_id INT);
Query OK, 0 rows affected (0.09 sec)

mysql> DELIMITER $$
mysql> CREATE TRIGGER ai_blackhole_each AFTER INSERT ON test.blackhole
    -> FOR EACH ROW
    -> BEGIN
    ->     DECLARE calculated_id INTEGER;
    ->     SELECT calc_id INTO calculated_id FROM test.blackholecounter;
    ->     UPDATE test.blackholecounter SET calc_id=calc_id+1;
    ->     INSERT INTO test.example VALUES(new.field1, new.field2, calculated_id);
    -> END
    -> $$
Query OK, 0 rows affected (0.11 sec)

mysql> DELIMITER ;
mysql> SELECT * FROM test.blackholecounter;
+---------+
| calc_id |
+---------+
|       0 |
+---------+
1 row in set (0.00 sec)

mysql> SELECT * FROM test.example;
Empty set (0.02 sec)

mysql> INSERT INTO test.blackhole (field1,field2) VALUES ('rolando','edwards'),('pamela','edwards');
Query OK, 2 rows affected (0.07 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM test.blackholecounter;
+---------+
| calc_id |
+---------+
|       2 |
+---------+
1 row in set (0.00 sec)

mysql> SELECT * FROM test.example;
+---------+---------+---------+
| field1  | field2  | calc_id |
+---------+---------+---------+
| rolando | edwards |       0 |
| pamela  | edwards |       1 |
+---------+---------+---------+
2 rows in set (0.00 sec)

You can start the initial value in the test.blackholecounter table with some other number or change the order when the increment happens in the trigger.

Give it a Try !!!

0

精彩评论

暂无评论...
验证码 换一张
取 消