mirror of
https://github.com/MariaDB/server.git
synced 2025-01-30 18:41:56 +01:00
dc681953cf
when the execution is very slow, under valgrind, the event might manage to fire more than once, making the test to fail
704 lines
25 KiB
Text
704 lines
25 KiB
Text
TRUNCATE TABLE performance_schema.events_statements_summary_by_program;
|
|
TRUNCATE TABLE performance_schema.events_statements_history_long;
|
|
################################################
|
|
# Quering PS statement summary and history_long#
|
|
################################################
|
|
# SET-UP
|
|
CREATE DATABASE stored_programs;
|
|
USE stored_programs;
|
|
CREATE TABLE t1(
|
|
i INT NOT NULL,
|
|
j INT
|
|
) engine=innodb;
|
|
CREATE TABLE t2(
|
|
name CHAR(16) NOT NULL DEFAULT '',
|
|
id INT NOT NULL
|
|
) engine=innodb;
|
|
CREATE TABLE t3(
|
|
d DATE,
|
|
n INT,
|
|
f DOUBLE,
|
|
s VARCHAR(32)
|
|
);
|
|
CREATE TABLE t4(
|
|
`k` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
|
`word` varchar(100) NOT NULL,
|
|
`mean` varchar(300) NOT NULL,
|
|
PRIMARY KEY (`k`)
|
|
);
|
|
############################
|
|
# Creating Stored Programs #
|
|
############################
|
|
# Stored Routine ( Procedure & Function )
|
|
CREATE PROCEDURE SampleProc1(x1 INT, x2 INT, y INT)
|
|
BEGIN
|
|
INSERT INTO t1 VALUES (x1, y);
|
|
INSERT INTO t1 VALUES (x2, y);
|
|
END|
|
|
CREATE PROCEDURE SampleProc2(x CHAR(16), y INT)
|
|
BEGIN
|
|
DECLARE z1, z2 INT;
|
|
SET z1 = y;
|
|
SET z2 = z1+2;
|
|
INSERT INTO t2 VALUES (x, z2);
|
|
END|
|
|
CREATE PROCEDURE SampleProc3()
|
|
BEGIN
|
|
DECLARE ld DATE;
|
|
DECLARE li INT;
|
|
DECLARE lf DOUBLE;
|
|
DECLARE ls VARCHAR(32);
|
|
SET ld = NULL, li = NULL, lf = NULL, ls = NULL;
|
|
INSERT INTO t3 VALUES (ld, li, lf, ls);
|
|
INSERT INTO t3 (n, f, s) VALUES ((ld IS NULL), 1, "ld is null"),
|
|
((li IS NULL), 1, "li is null"),
|
|
((li = 0), NULL, "li = 0"),
|
|
((lf IS NULL), 1, "lf is null"),
|
|
((lf = 0), NULL, "lf = 0"),
|
|
((ls IS NULL), 1, "ls is null");
|
|
END|
|
|
CREATE PROCEDURE SampleProc4()
|
|
BEGIN
|
|
DECLARE x INT;
|
|
SET x = 1;
|
|
WHILE x <= 2 DO
|
|
INSERT INTO t4(word, mean) VALUES('a','a mean');
|
|
SET x = x + 1;
|
|
END WHILE;
|
|
END|
|
|
CREATE FUNCTION append(s1 CHAR(8), s2 CHAR(8)) RETURNS CHAR(16)
|
|
RETURN concat(s1, s2)|
|
|
CREATE FUNCTION wt_avg(n1 INT, n2 INT, n3 INT, n4 INT)
|
|
RETURNS INT
|
|
DETERMINISTIC
|
|
BEGIN
|
|
DECLARE avg INT;
|
|
SET avg = (n1+n2+n3*2+n4*4)/8;
|
|
RETURN avg;
|
|
END|
|
|
CREATE FUNCTION fac(n INT UNSIGNED) RETURNS BIGINT UNSIGNED
|
|
BEGIN
|
|
DECLARE f BIGINT UNSIGNED DEFAULT 1;
|
|
WHILE n > 1 DO
|
|
SET f = f * n;
|
|
SET n = n - 1;
|
|
END WHILE;
|
|
RETURN f;
|
|
END|
|
|
# Triggers
|
|
CREATE TRIGGER trg1 BEFORE INSERT ON t1 FOR EACH ROW
|
|
BEGIN
|
|
if isnull(new.j) then
|
|
SET new.j:= new.i * 10;
|
|
END if;
|
|
END|
|
|
CREATE TRIGGER trg2 AFTER INSERT ON t2 FOR EACH ROW
|
|
BEGIN
|
|
UPDATE t1 SET i=new.id+i ;
|
|
END|
|
|
CREATE TRIGGER trg3 AFTER UPDATE ON t2 FOR EACH ROW
|
|
SET @change:= @change + new.id - old.id|
|
|
CREATE TRIGGER trg4 BEFORE DELETE ON t1 FOR EACH ROW
|
|
SET @del:= @del + 1|
|
|
CREATE TRIGGER trg5 AFTER DELETE ON t1 FOR EACH ROW
|
|
SET @del:= @del + 8 + old.j|
|
|
#####################
|
|
# Executing queries #
|
|
#####################
|
|
INSERT INTO t1 VALUES (10,20);
|
|
CALL SampleProc1(30,40,50);
|
|
SET @a=1;
|
|
SELECT @a;
|
|
@a
|
|
1
|
|
CALL SampleProc2("Jwalamukhi",34);
|
|
SELECT @a;
|
|
@a
|
|
1
|
|
CALL SampleProc3();
|
|
CALL SampleProc4();
|
|
SET @change=1;
|
|
SELECT @change;
|
|
@change
|
|
1
|
|
UPDATE t2 SET id=22 WHERE name="Jwalamukhi";
|
|
SELECT @change;
|
|
@change
|
|
-13
|
|
SET @del=1;
|
|
SELECT @del;
|
|
@del
|
|
1
|
|
DELETE FROM t1 WHERE i=76;
|
|
SELECT @del;
|
|
@del
|
|
60
|
|
SELECT wt_avg(1, 12, 1990, 1121990);
|
|
wt_avg(1, 12, 1990, 1121990)
|
|
561494
|
|
SELECT fac(5);
|
|
fac(5)
|
|
120
|
|
SELECT append("Bolly", "wood");
|
|
append("Bolly", "wood")
|
|
Bollywood
|
|
# Event
|
|
SET GLOBAL event_scheduler=ON;
|
|
CREATE TABLE table_t(a INT);
|
|
CREATE EVENT e1 ON SCHEDULE EVERY 2 SECOND ON COMPLETION NOT PRESERVE DO
|
|
BEGIN
|
|
INSERT INTO table_t VALUES(1);
|
|
END|
|
|
SELECT * FROM table_t;
|
|
a
|
|
1
|
|
SET GLOBAL event_scheduler=OFF;
|
|
SELECT OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME, COUNT_STAR, COUNT_STATEMENTS
|
|
FROM performance_schema.events_statements_summary_by_program
|
|
WHERE OBJECT_SCHEMA='stored_programs' ORDER BY OBJECT_NAME;
|
|
OBJECT_TYPE OBJECT_SCHEMA OBJECT_NAME COUNT_STAR COUNT_STATEMENTS
|
|
FUNCTION stored_programs append 1 1
|
|
EVENT stored_programs e1 1 1
|
|
FUNCTION stored_programs fac 1 19
|
|
PROCEDURE stored_programs sampleproc1 1 2
|
|
PROCEDURE stored_programs sampleproc2 1 5
|
|
PROCEDURE stored_programs sampleproc3 1 10
|
|
PROCEDURE stored_programs sampleproc4 1 11
|
|
TRIGGER stored_programs trg1 3 3
|
|
TRIGGER stored_programs trg2 1 1
|
|
TRIGGER stored_programs trg3 1 1
|
|
TRIGGER stored_programs trg4 1 1
|
|
TRIGGER stored_programs trg5 1 1
|
|
FUNCTION stored_programs wt_avg 1 3
|
|
SELECT EVENT_NAME, SQL_TEXT, CURRENT_SCHEMA, OBJECT_TYPE, OBJECT_SCHEMA,
|
|
OBJECT_NAME, NESTING_EVENT_TYPE, NESTING_EVENT_LEVEL FROM
|
|
performance_schema.events_statements_history_long WHERE
|
|
CURRENT_SCHEMA='stored_programs' AND
|
|
(SQL_TEXT not like '%count(*) = %' OR SQL_TEXT IS NULL)
|
|
ORDER BY OBJECT_NAME, NESTING_EVENT_LEVEL, SQL_TEXT;
|
|
EVENT_NAME SQL_TEXT CURRENT_SCHEMA OBJECT_TYPE OBJECT_SCHEMA OBJECT_NAME NESTING_EVENT_TYPE NESTING_EVENT_LEVEL
|
|
statement/scheduler/event NULL stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/call_procedure CALL SampleProc1(30,40,50) stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/call_procedure CALL SampleProc2("Jwalamukhi",34) stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/call_procedure CALL SampleProc3() stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/call_procedure CALL SampleProc4() stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/create_event CREATE EVENT e1 ON SCHEDULE EVERY 2 SECOND ON COMPLETION NOT PRESERVE DO
|
|
BEGIN
|
|
INSERT INTO table_t VALUES(1);
|
|
END stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/create_function CREATE FUNCTION append(s1 CHAR(8), s2 CHAR(8)) RETURNS CHAR(16)
|
|
RETURN concat(s1, s2) stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/create_function CREATE FUNCTION fac(n INT UNSIGNED) RETURNS BIGINT UNSIGNED
|
|
BEGIN
|
|
DECLARE f BIGINT UNSIGNED DEFAULT 1;
|
|
WHILE n > 1 DO
|
|
SET f = f * n;
|
|
SET n = n - 1;
|
|
END WHILE;
|
|
RETURN f;
|
|
END stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/create_function CREATE FUNCTION wt_avg(n1 INT, n2 INT, n3 INT, n4 INT)
|
|
RETURNS INT
|
|
DETERMINISTIC
|
|
BEGIN
|
|
DECLARE avg INT;
|
|
SET avg = (n1+n2+n3*2+n4*4)/8;
|
|
RETURN avg;
|
|
END stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/create_procedure CREATE PROCEDURE SampleProc1(x1 INT, x2 INT, y INT)
|
|
BEGIN
|
|
INSERT INTO t1 VALUES (x1, y);
|
|
INSERT INTO t1 VALUES (x2, y);
|
|
END stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/create_procedure CREATE PROCEDURE SampleProc2(x CHAR(16), y INT)
|
|
BEGIN
|
|
DECLARE z1, z2 INT;
|
|
SET z1 = y;
|
|
SET z2 = z1+2;
|
|
INSERT INTO t2 VALUES (x, z2);
|
|
END stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/create_procedure CREATE PROCEDURE SampleProc3()
|
|
BEGIN
|
|
DECLARE ld DATE;
|
|
DECLARE li INT;
|
|
DECLARE lf DOUBLE;
|
|
DECLARE ls VARCHAR(32);
|
|
SET ld = NULL, li = NULL, lf = NULL, ls = NULL;
|
|
INSERT INTO t3 VALUES (ld, li, lf, ls);
|
|
INSERT INTO t3 (n, f, s) VALUES ((ld IS NULL), 1, "ld is null"),
|
|
((li IS NULL), 1, "li is null"),
|
|
((li = 0), NULL, "li = 0"),
|
|
((lf IS NULL), 1, "lf is null"),
|
|
((lf = 0), NULL, "lf = 0"),
|
|
((ls IS NULL), 1, "ls is null");
|
|
END stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/create_procedure CREATE PROCEDURE SampleProc4()
|
|
BEGIN
|
|
DECLARE x INT;
|
|
SET x = 1;
|
|
WHILE x <= 2 DO
|
|
INSERT INTO t4(word, mean) VALUES('a','a mean');
|
|
SET x = x + 1;
|
|
END WHILE;
|
|
END stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/create_table CREATE TABLE t1(
|
|
i INT NOT NULL,
|
|
j INT
|
|
) engine=innodb stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/create_table CREATE TABLE t2(
|
|
name CHAR(16) NOT NULL DEFAULT '',
|
|
id INT NOT NULL
|
|
) engine=innodb stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/create_table CREATE TABLE t3(
|
|
d DATE,
|
|
n INT,
|
|
f DOUBLE,
|
|
s VARCHAR(32)
|
|
) stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/create_table CREATE TABLE t4(
|
|
`k` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
|
`word` varchar(100) NOT NULL,
|
|
`mean` varchar(300) NOT NULL,
|
|
PRIMARY KEY (`k`)
|
|
) stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/create_table CREATE TABLE table_t(a INT) stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/create_trigger CREATE TRIGGER trg1 BEFORE INSERT ON t1 FOR EACH ROW
|
|
BEGIN
|
|
if isnull(new.j) then
|
|
SET new.j:= new.i * 10;
|
|
END if;
|
|
END stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/create_trigger CREATE TRIGGER trg2 AFTER INSERT ON t2 FOR EACH ROW
|
|
BEGIN
|
|
UPDATE t1 SET i=new.id+i ;
|
|
END stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/create_trigger CREATE TRIGGER trg3 AFTER UPDATE ON t2 FOR EACH ROW
|
|
SET @change:= @change + new.id - old.id stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/create_trigger CREATE TRIGGER trg4 BEFORE DELETE ON t1 FOR EACH ROW
|
|
SET @del:= @del + 1 stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/create_trigger CREATE TRIGGER trg5 AFTER DELETE ON t1 FOR EACH ROW
|
|
SET @del:= @del + 8 + old.j stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/delete DELETE FROM t1 WHERE i=76 stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/insert INSERT INTO t1 VALUES (10,20) stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/select SELECT * FROM table_t stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/select SELECT 5 * 10 stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/select SELECT @a stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/select SELECT @a stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/select SELECT @change stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/select SELECT @change stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/select SELECT @del stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/select SELECT @del stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/select SELECT append("Bolly", "wood") stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/select SELECT fac(5) stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/select SELECT OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME, COUNT_STAR, COUNT_STATEMENTS
|
|
FROM performance_schema.events_statements_summary_by_program
|
|
WHERE OBJECT_SCHEMA='stored_programs' ORDER BY OBJECT_NAME stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/select SELECT wt_avg(1, 12, 1990, 1121990) stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/set_option SET @a=1 stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/set_option SET @change=1 stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/set_option SET @del=1 stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/set_option SET GLOBAL event_scheduler=OFF stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/set_option SET GLOBAL event_scheduler=ON stored_programs NULL NULL NULL NULL 0
|
|
statement/sql/update UPDATE t2 SET id=22 WHERE name="Jwalamukhi" stored_programs NULL NULL NULL NULL 0
|
|
statement/sp/freturn NULL stored_programs FUNCTION stored_programs append STATEMENT 1
|
|
statement/sp/stmt INSERT INTO table_t VALUES(1) stored_programs EVENT stored_programs e1 STATEMENT 1
|
|
statement/sp/set NULL stored_programs FUNCTION stored_programs fac STATEMENT 1
|
|
statement/sp/jump_if_not NULL stored_programs FUNCTION stored_programs fac STATEMENT 1
|
|
statement/sp/set NULL stored_programs FUNCTION stored_programs fac STATEMENT 1
|
|
statement/sp/set NULL stored_programs FUNCTION stored_programs fac STATEMENT 1
|
|
statement/sp/jump NULL stored_programs FUNCTION stored_programs fac STATEMENT 1
|
|
statement/sp/jump_if_not NULL stored_programs FUNCTION stored_programs fac STATEMENT 1
|
|
statement/sp/set NULL stored_programs FUNCTION stored_programs fac STATEMENT 1
|
|
statement/sp/set NULL stored_programs FUNCTION stored_programs fac STATEMENT 1
|
|
statement/sp/jump NULL stored_programs FUNCTION stored_programs fac STATEMENT 1
|
|
statement/sp/jump_if_not NULL stored_programs FUNCTION stored_programs fac STATEMENT 1
|
|
statement/sp/set NULL stored_programs FUNCTION stored_programs fac STATEMENT 1
|
|
statement/sp/set NULL stored_programs FUNCTION stored_programs fac STATEMENT 1
|
|
statement/sp/jump NULL stored_programs FUNCTION stored_programs fac STATEMENT 1
|
|
statement/sp/jump_if_not NULL stored_programs FUNCTION stored_programs fac STATEMENT 1
|
|
statement/sp/set NULL stored_programs FUNCTION stored_programs fac STATEMENT 1
|
|
statement/sp/set NULL stored_programs FUNCTION stored_programs fac STATEMENT 1
|
|
statement/sp/jump NULL stored_programs FUNCTION stored_programs fac STATEMENT 1
|
|
statement/sp/jump_if_not NULL stored_programs FUNCTION stored_programs fac STATEMENT 1
|
|
statement/sp/freturn NULL stored_programs FUNCTION stored_programs fac STATEMENT 1
|
|
statement/sp/stmt INSERT INTO t1 VALUES (x1, y) stored_programs PROCEDURE stored_programs sampleproc1 STATEMENT 1
|
|
statement/sp/stmt INSERT INTO t1 VALUES (x2, y) stored_programs PROCEDURE stored_programs sampleproc1 STATEMENT 1
|
|
statement/sp/set NULL stored_programs PROCEDURE stored_programs sampleproc2 STATEMENT 1
|
|
statement/sp/set NULL stored_programs PROCEDURE stored_programs sampleproc2 STATEMENT 1
|
|
statement/sp/set NULL stored_programs PROCEDURE stored_programs sampleproc2 STATEMENT 1
|
|
statement/sp/set NULL stored_programs PROCEDURE stored_programs sampleproc2 STATEMENT 1
|
|
statement/sp/stmt INSERT INTO t2 VALUES (x, z2) stored_programs PROCEDURE stored_programs sampleproc2 STATEMENT 1
|
|
statement/sp/set NULL stored_programs PROCEDURE stored_programs sampleproc3 STATEMENT 1
|
|
statement/sp/set NULL stored_programs PROCEDURE stored_programs sampleproc3 STATEMENT 1
|
|
statement/sp/set NULL stored_programs PROCEDURE stored_programs sampleproc3 STATEMENT 1
|
|
statement/sp/set NULL stored_programs PROCEDURE stored_programs sampleproc3 STATEMENT 1
|
|
statement/sp/set NULL stored_programs PROCEDURE stored_programs sampleproc3 STATEMENT 1
|
|
statement/sp/set NULL stored_programs PROCEDURE stored_programs sampleproc3 STATEMENT 1
|
|
statement/sp/set NULL stored_programs PROCEDURE stored_programs sampleproc3 STATEMENT 1
|
|
statement/sp/set NULL stored_programs PROCEDURE stored_programs sampleproc3 STATEMENT 1
|
|
statement/sp/stmt INSERT INTO t3 (n, f, s) VALUES ((ld IS NULL), 1, "ld is null"),
|
|
((li IS NULL), 1, "li is null"),
|
|
((li = 0), NULL, "li = 0"),
|
|
((lf IS NULL), 1, "lf is null"),
|
|
((lf = 0), NULL, "lf = 0"),
|
|
((ls IS NULL), 1, "ls is null") stored_programs PROCEDURE stored_programs sampleproc3 STATEMENT 1
|
|
statement/sp/stmt INSERT INTO t3 VALUES (ld, li, lf, ls) stored_programs PROCEDURE stored_programs sampleproc3 STATEMENT 1
|
|
statement/sp/set NULL stored_programs PROCEDURE stored_programs sampleproc4 STATEMENT 1
|
|
statement/sp/set NULL stored_programs PROCEDURE stored_programs sampleproc4 STATEMENT 1
|
|
statement/sp/jump_if_not NULL stored_programs PROCEDURE stored_programs sampleproc4 STATEMENT 1
|
|
statement/sp/set NULL stored_programs PROCEDURE stored_programs sampleproc4 STATEMENT 1
|
|
statement/sp/jump NULL stored_programs PROCEDURE stored_programs sampleproc4 STATEMENT 1
|
|
statement/sp/jump_if_not NULL stored_programs PROCEDURE stored_programs sampleproc4 STATEMENT 1
|
|
statement/sp/set NULL stored_programs PROCEDURE stored_programs sampleproc4 STATEMENT 1
|
|
statement/sp/jump NULL stored_programs PROCEDURE stored_programs sampleproc4 STATEMENT 1
|
|
statement/sp/jump_if_not NULL stored_programs PROCEDURE stored_programs sampleproc4 STATEMENT 1
|
|
statement/sp/stmt INSERT INTO t4(word, mean) VALUES('a','a mean') stored_programs PROCEDURE stored_programs sampleproc4 STATEMENT 1
|
|
statement/sp/stmt INSERT INTO t4(word, mean) VALUES('a','a mean') stored_programs PROCEDURE stored_programs sampleproc4 STATEMENT 1
|
|
statement/sp/jump_if_not NULL stored_programs TRIGGER stored_programs trg1 TRANSACTION 1
|
|
statement/sp/jump_if_not NULL stored_programs TRIGGER stored_programs trg1 TRANSACTION 2
|
|
statement/sp/jump_if_not NULL stored_programs TRIGGER stored_programs trg1 TRANSACTION 2
|
|
statement/sp/stmt UPDATE t1 SET i=new.id+i stored_programs TRIGGER stored_programs trg2 TRANSACTION 2
|
|
statement/sp/stmt SET @change:= @change + new.id - old.id stored_programs TRIGGER stored_programs trg3 TRANSACTION 1
|
|
statement/sp/stmt SET @del:= @del + 1 stored_programs TRIGGER stored_programs trg4 TRANSACTION 1
|
|
statement/sp/stmt SET @del:= @del + 8 + old.j stored_programs TRIGGER stored_programs trg5 TRANSACTION 1
|
|
statement/sp/set NULL stored_programs FUNCTION stored_programs wt_avg STATEMENT 1
|
|
statement/sp/set NULL stored_programs FUNCTION stored_programs wt_avg STATEMENT 1
|
|
statement/sp/freturn NULL stored_programs FUNCTION stored_programs wt_avg STATEMENT 1
|
|
# clean -up
|
|
TRUNCATE TABLE performance_schema.events_statements_summary_by_program;
|
|
TRUNCATE TABLE performance_schema.events_statements_history_long;
|
|
SELECT OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME, COUNT_STAR, COUNT_STATEMENTS
|
|
FROM performance_schema.events_statements_summary_by_program
|
|
WHERE OBJECT_SCHEMA='stored_programs' ORDER BY OBJECT_NAME;
|
|
OBJECT_TYPE OBJECT_SCHEMA OBJECT_NAME COUNT_STAR COUNT_STATEMENTS
|
|
FUNCTION stored_programs append 0 0
|
|
EVENT stored_programs e1 0 0
|
|
FUNCTION stored_programs fac 0 0
|
|
PROCEDURE stored_programs sampleproc1 0 0
|
|
PROCEDURE stored_programs sampleproc2 0 0
|
|
PROCEDURE stored_programs sampleproc3 0 0
|
|
PROCEDURE stored_programs sampleproc4 0 0
|
|
TRIGGER stored_programs trg1 0 0
|
|
TRIGGER stored_programs trg2 0 0
|
|
TRIGGER stored_programs trg3 0 0
|
|
TRIGGER stored_programs trg4 0 0
|
|
TRIGGER stored_programs trg5 0 0
|
|
FUNCTION stored_programs wt_avg 0 0
|
|
DROP PROCEDURE SampleProc1;
|
|
DROP PROCEDURE SampleProc2;
|
|
DROP PROCEDURE SampleProc3;
|
|
DROP PROCEDURE SampleProc4;
|
|
DROP FUNCTION wt_avg;
|
|
DROP FUNCTION fac;
|
|
DROP FUNCTION append;
|
|
DROP TRIGGER trg1;
|
|
DROP TRIGGER trg2;
|
|
DROP TRIGGER trg3;
|
|
DROP TRIGGER trg4;
|
|
DROP TRIGGER trg5;
|
|
DROP EVENT IF EXISTS e1;
|
|
DROP TABLE t1;
|
|
DROP TABLE t2;
|
|
DROP TABLE t3;
|
|
DROP TABLE t4;
|
|
DROP TABLE table_t;
|
|
DROP DATABASE stored_programs;
|
|
SELECT OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME, COUNT_STAR, COUNT_STATEMENTS
|
|
FROM performance_schema.events_statements_summary_by_program
|
|
WHERE OBJECT_SCHEMA='stored_programs' ORDER BY OBJECT_NAME;
|
|
OBJECT_TYPE OBJECT_SCHEMA OBJECT_NAME COUNT_STAR COUNT_STATEMENTS
|
|
# SET-UP
|
|
CREATE DATABASE stored_programs;
|
|
USE stored_programs;
|
|
CREATE TABLE t1(
|
|
i INT NOT NULL,
|
|
j INT
|
|
) engine=innodb;
|
|
CREATE TABLE t2(
|
|
name CHAR(16) NOT NULL DEFAULT '',
|
|
id INT NOT NULL
|
|
) engine=innodb;
|
|
CREATE TABLE t3(
|
|
d DATE,
|
|
n INT,
|
|
f DOUBLE,
|
|
s VARCHAR(32)
|
|
);
|
|
CREATE TABLE t4(
|
|
`k` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
|
`word` varchar(100) NOT NULL,
|
|
`mean` varchar(300) NOT NULL,
|
|
PRIMARY KEY (`k`)
|
|
);
|
|
############################
|
|
# Creating Stored Programs #
|
|
############################
|
|
# Stored Routine ( Procedure & Function )
|
|
CREATE PROCEDURE SampleProc1(x1 INT, x2 INT, y INT)
|
|
BEGIN
|
|
INSERT INTO t1 VALUES (x1, y);
|
|
INSERT INTO t1 VALUES (x2, y);
|
|
END|
|
|
CREATE PROCEDURE SampleProc2(x CHAR(16), y INT)
|
|
BEGIN
|
|
DECLARE z1, z2 INT;
|
|
SET z1 = y;
|
|
SET z2 = z1+2;
|
|
INSERT INTO t2 VALUES (x, z2);
|
|
END|
|
|
CREATE PROCEDURE SampleProc3()
|
|
BEGIN
|
|
DECLARE ld DATE;
|
|
DECLARE li INT;
|
|
DECLARE lf DOUBLE;
|
|
DECLARE ls VARCHAR(32);
|
|
SET ld = NULL, li = NULL, lf = NULL, ls = NULL;
|
|
INSERT INTO t3 VALUES (ld, li, lf, ls);
|
|
INSERT INTO t3 (n, f, s) VALUES ((ld IS NULL), 1, "ld is null"),
|
|
((li IS NULL), 1, "li is null"),
|
|
((li = 0), NULL, "li = 0"),
|
|
((lf IS NULL), 1, "lf is null"),
|
|
((lf = 0), NULL, "lf = 0"),
|
|
((ls IS NULL), 1, "ls is null");
|
|
END|
|
|
CREATE PROCEDURE SampleProc4()
|
|
BEGIN
|
|
DECLARE x INT;
|
|
SET x = 1;
|
|
WHILE x <= 2 DO
|
|
INSERT INTO t4(word, mean) VALUES('a','a mean');
|
|
SET x = x + 1;
|
|
END WHILE;
|
|
END|
|
|
CREATE FUNCTION append(s1 CHAR(8), s2 CHAR(8)) RETURNS CHAR(16)
|
|
RETURN concat(s1, s2)|
|
|
CREATE FUNCTION wt_avg(n1 INT, n2 INT, n3 INT, n4 INT)
|
|
RETURNS INT
|
|
DETERMINISTIC
|
|
BEGIN
|
|
DECLARE avg INT;
|
|
SET avg = (n1+n2+n3*2+n4*4)/8;
|
|
RETURN avg;
|
|
END|
|
|
CREATE FUNCTION fac(n INT UNSIGNED) RETURNS BIGINT UNSIGNED
|
|
BEGIN
|
|
DECLARE f BIGINT UNSIGNED DEFAULT 1;
|
|
WHILE n > 1 DO
|
|
SET f = f * n;
|
|
SET n = n - 1;
|
|
END WHILE;
|
|
RETURN f;
|
|
END|
|
|
# Triggers
|
|
CREATE TRIGGER trg1 BEFORE INSERT ON t1 FOR EACH ROW
|
|
BEGIN
|
|
if isnull(new.j) then
|
|
SET new.j:= new.i * 10;
|
|
END if;
|
|
END|
|
|
CREATE TRIGGER trg2 AFTER INSERT ON t2 FOR EACH ROW
|
|
BEGIN
|
|
UPDATE t1 SET i=new.id+i ;
|
|
END|
|
|
CREATE TRIGGER trg3 AFTER UPDATE ON t2 FOR EACH ROW
|
|
SET @change:= @change + new.id - old.id|
|
|
CREATE TRIGGER trg4 BEFORE DELETE ON t1 FOR EACH ROW
|
|
SET @del:= @del + 1|
|
|
CREATE TRIGGER trg5 AFTER DELETE ON t1 FOR EACH ROW
|
|
SET @del:= @del + 8 + old.j|
|
|
update performance_schema.setup_instruments set enabled='YES', timed='NO'
|
|
where name like "statement/sp/%" order by name;
|
|
TRUNCATE TABLE performance_schema.events_statements_summary_by_program;
|
|
TRUNCATE TABLE performance_schema.events_statements_history_long;
|
|
#####################
|
|
# Executing queries #
|
|
#####################
|
|
INSERT INTO t1 VALUES (10,20);
|
|
CALL SampleProc1(30,40,50);
|
|
SET @a=1;
|
|
SELECT @a;
|
|
@a
|
|
1
|
|
CALL SampleProc2("Jwalamukhi",34);
|
|
SELECT @a;
|
|
@a
|
|
1
|
|
CALL SampleProc3();
|
|
CALL SampleProc4();
|
|
SET @change=1;
|
|
SELECT @change;
|
|
@change
|
|
1
|
|
UPDATE t2 SET id=22 WHERE name="Jwalamukhi";
|
|
SELECT @change;
|
|
@change
|
|
-13
|
|
SET @del=1;
|
|
SELECT @del;
|
|
@del
|
|
1
|
|
DELETE FROM t1 WHERE i=76;
|
|
SELECT @del;
|
|
@del
|
|
60
|
|
SELECT wt_avg(1, 12, 1990, 1121990);
|
|
wt_avg(1, 12, 1990, 1121990)
|
|
561494
|
|
SELECT fac(5);
|
|
fac(5)
|
|
120
|
|
SELECT append("Bolly", "wood");
|
|
append("Bolly", "wood")
|
|
Bollywood
|
|
# Event
|
|
SET GLOBAL event_scheduler=ON;
|
|
CREATE TABLE table_t(a INT);
|
|
CREATE EVENT e1 ON SCHEDULE EVERY 2 SECOND ON COMPLETION NOT PRESERVE DO
|
|
BEGIN
|
|
INSERT INTO table_t VALUES(1);
|
|
END|
|
|
SELECT * FROM table_t;
|
|
a
|
|
1
|
|
SET GLOBAL event_scheduler=OFF;
|
|
SELECT EVENT_NAME, TIMER_START, TIMER_END, TIMER_WAIT FROM
|
|
performance_schema.events_statements_history_long WHERE
|
|
CURRENT_SCHEMA='stored_programs' AND EVENT_NAME like "statement/sp/%";
|
|
EVENT_NAME TIMER_START TIMER_END TIMER_WAIT
|
|
statement/sp/jump_if_not NULL NULL NULL
|
|
statement/sp/jump_if_not NULL NULL NULL
|
|
statement/sp/stmt NULL NULL NULL
|
|
statement/sp/jump_if_not NULL NULL NULL
|
|
statement/sp/stmt NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/stmt NULL NULL NULL
|
|
statement/sp/stmt NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/stmt NULL NULL NULL
|
|
statement/sp/stmt NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/jump_if_not NULL NULL NULL
|
|
statement/sp/stmt NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/jump NULL NULL NULL
|
|
statement/sp/jump_if_not NULL NULL NULL
|
|
statement/sp/stmt NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/jump NULL NULL NULL
|
|
statement/sp/jump_if_not NULL NULL NULL
|
|
statement/sp/stmt NULL NULL NULL
|
|
statement/sp/stmt NULL NULL NULL
|
|
statement/sp/stmt NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/freturn NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/jump_if_not NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/jump NULL NULL NULL
|
|
statement/sp/jump_if_not NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/jump NULL NULL NULL
|
|
statement/sp/jump_if_not NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/jump NULL NULL NULL
|
|
statement/sp/jump_if_not NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/set NULL NULL NULL
|
|
statement/sp/jump NULL NULL NULL
|
|
statement/sp/jump_if_not NULL NULL NULL
|
|
statement/sp/freturn NULL NULL NULL
|
|
statement/sp/freturn NULL NULL NULL
|
|
statement/sp/stmt NULL NULL NULL
|
|
update performance_schema.setup_instruments set enabled='YES', timed='YES'
|
|
where name like "statement/sp/%" order by name;
|
|
TRUNCATE TABLE performance_schema.events_statements_summary_by_program;
|
|
TRUNCATE TABLE performance_schema.events_statements_history_long;
|
|
DROP PROCEDURE SampleProc1;
|
|
DROP PROCEDURE SampleProc2;
|
|
DROP PROCEDURE SampleProc3;
|
|
DROP PROCEDURE SampleProc4;
|
|
DROP FUNCTION wt_avg;
|
|
DROP FUNCTION fac;
|
|
DROP FUNCTION append;
|
|
DROP TRIGGER trg1;
|
|
DROP TRIGGER trg2;
|
|
DROP TRIGGER trg3;
|
|
DROP TRIGGER trg4;
|
|
DROP TRIGGER trg5;
|
|
DROP EVENT IF EXISTS e1;
|
|
DROP TABLE t1;
|
|
DROP TABLE t2;
|
|
DROP TABLE t3;
|
|
DROP TABLE t4;
|
|
DROP TABLE table_t;
|
|
DROP DATABASE stored_programs;
|
|
# set-up
|
|
CREATE DATABASE sp;
|
|
USE sp;
|
|
CREATE TABLE t1(
|
|
a INT,
|
|
b INT
|
|
);
|
|
# let the creation of the following stored programs fail
|
|
CREATE PROCEDURE fail1(IN a INT OUT x CHAR(16))
|
|
SET a=1;
|
|
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'OUT x CHAR(16))
|
|
SET a=1' at line 1
|
|
CREATE FUNCTION fail2(a INT , b INT) RETURNS INT
|
|
x=SELECT COUNT(*) FROM t;
|
|
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '=SELECT COUNT(*) FROM t' at line 2
|
|
CREATE EVENT fail3 SCHEDULE EVERY MICROSECOND DO
|
|
DROP TABLE t;
|
|
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SCHEDULE EVERY MICROSECOND DO
|
|
DROP TABLE t' at line 1
|
|
# the below query on PS table doesn't show any rows
|
|
# as the creation of stored programs failed
|
|
SELECT OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME, COUNT_STAR, COUNT_STATEMENTS
|
|
FROM performance_schema.events_statements_summary_by_program
|
|
WHERE OBJECT_SCHEMA='sp';
|
|
OBJECT_TYPE OBJECT_SCHEMA OBJECT_NAME COUNT_STAR COUNT_STATEMENTS
|
|
# create few stored programs
|
|
CREATE PROCEDURE p(x1 INT, x2 INT)
|
|
BEGIN
|
|
INSERT INTO t1 VALUES (x1, x2);
|
|
END|
|
|
CREATE FUNCTION f(y1 INT, y2 INT) RETURNS INT
|
|
RETURN y1+y2;
|
|
CREATE TRIGGER trg AFTER INSERT ON t1 FOR EACH ROW
|
|
SET @neg=-1;
|
|
# execute the created stored programs such that they fail.
|
|
CALL p(7);
|
|
ERROR 42000: Incorrect number of arguments for PROCEDURE sp.p; expected 2, got 1
|
|
SELECT f("add",1,3);
|
|
ERROR 42000: Incorrect number of arguments for FUNCTION sp.f; expected 2, got 3
|
|
INSERT INTO t1;
|
|
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
|
|
# the below query on PS table doesn't expose any statistics as
|
|
# execution of the created stored porgrams failed.
|
|
SELECT OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME, COUNT_STAR, COUNT_STATEMENTS
|
|
FROM performance_schema.events_statements_summary_by_program
|
|
WHERE OBJECT_SCHEMA='sp';
|
|
OBJECT_TYPE OBJECT_SCHEMA OBJECT_NAME COUNT_STAR COUNT_STATEMENTS
|
|
PROCEDURE sp p 0 0
|
|
FUNCTION sp f 0 0
|
|
#clean-up
|
|
DROP PROCEDURE p;
|
|
DROP FUNCTION f;
|
|
DROP TRIGGER trg;
|
|
DROP TABLE t1;
|
|
DROP DATABASE sp;
|