diff --git a/mysql-test/r/plan_cache.result b/mysql-test/r/plan_cache.result new file mode 100644 index 0000000000000000000000000000000000000000..49475357d4ae5421ebfd481417b64833ce134c62 --- /dev/null +++ b/mysql-test/r/plan_cache.result @@ -0,0 +1,289 @@ +CREATE DATABASE plancache; +USE plancache; +DROP TABLE IF EXISTS t1; +CREATE TABLE t1 (a INT PRIMARY KEY, b VARCHAR(5)); +INSERT INTO t1 VALUES (1, "A1"); +INSERT INTO t1 VALUES (2, "A2"); +INSERT INTO t1 VALUES (3, "A3"); +INSERT INTO t1 VALUES (4, "A4"); +INSERT INTO t1 VALUES (5, "A5"); +INSERT INTO t1 VALUES (6, "A6"); +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +Variable_name Value +Cached_prepared_stmt_count 0 +SET GLOBAL plan_cache = on; +PREPARE stmt1 FROM 'SELECT b FROM t1 WHERE a = ?'; +set @a = 1; +EXECUTE stmt1 USING @a; +b +A1 +EXECUTE stmt1 USING @a; +b +A1 +EXECUTE stmt1 USING @a; +b +A1 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +Variable_name Value +Cached_prepared_stmt_count 1 +set @a = 2; +EXECUTE stmt1 USING @a; +b +A2 +SET GLOBAL plan_cache = off; +EXECUTE stmt1 USING @a; +b +A2 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +Variable_name Value +Cached_prepared_stmt_count 0 +SET GLOBAL plan_cache = on; +PREPARE stmt2 FROM 'SELECT DISTINCT b FROM t1 WHERE a = ?'; +set @a = 1; +EXECUTE stmt2 USING @a; +b +A1 +EXECUTE stmt2 USING @a; +b +A1 +EXECUTE stmt2 USING @a; +b +A1 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +Variable_name Value +Cached_prepared_stmt_count 0 +PREPARE stmt3 FROM 'SELECT b FROM t1'; +EXECUTE stmt3; +b +A1 +A2 +A3 +A4 +A5 +A6 +EXECUTE stmt3; +b +A1 +A2 +A3 +A4 +A5 +A6 +EXECUTE stmt3; +b +A1 +A2 +A3 +A4 +A5 +A6 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +Variable_name Value +Cached_prepared_stmt_count 0 +PREPARE stmt4 FROM 'SELECT b FROM t1 where t1.a > (SELECT AVG(t2.a) FROM t1 t2 where t1.a = t2.a)'; +EXECUTE stmt4; +b +EXECUTE stmt4; +b +EXECUTE stmt4; +b +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +Variable_name Value +Cached_prepared_stmt_count 0 +DROP TABLE IF EXISTS t2; +CREATE TABLE t2 (a INT PRIMARY KEY, b VARCHAR(5)) partition by hash(a) partitions 4; +INSERT INTO t2 VALUES (1, "A1"); +INSERT INTO t2 VALUES (2, "A2"); +INSERT INTO t2 VALUES (3, "A3"); +INSERT INTO t2 VALUES (4, "A4"); +INSERT INTO t2 VALUES (5, "A5"); +INSERT INTO t2 VALUES (6, "A6"); +PREPARE stmt5 FROM 'SELECT b FROM t2 WHERE a = ?'; +set @a = 1; +EXECUTE stmt5 USING @a; +b +A1 +EXECUTE stmt5 USING @a; +b +A1 +EXECUTE stmt5 USING @a; +b +A1 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +Variable_name Value +Cached_prepared_stmt_count 0 +PREPARE stmt6 FROM 'SELECT b FROM t1 WHERE a = ? and b = "A1"'; +set @a = 1; +EXECUTE stmt6 USING @a; +b +A1 +EXECUTE stmt6 USING @a; +b +A1 +EXECUTE stmt6 USING @a; +b +A1 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +Variable_name Value +Cached_prepared_stmt_count 0 +PREPARE stmt7 FROM 'SELECT b FROM t1 WHERE a = @a'; +EXECUTE stmt7; +b +A1 +EXECUTE stmt7; +b +A1 +EXECUTE stmt7; +b +A1 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +Variable_name Value +Cached_prepared_stmt_count 0 +PREPARE stmt8 FROM 'SELECT event_name, count_star FROM performance_schema.events_waits_summary_global_by_event_name WHERE event_name IN ("idle")'; +EXECUTE stmt8; +event_name count_star +idle 153 +EXECUTE stmt8; +event_name count_star +idle 155 +EXECUTE stmt8; +event_name count_star +idle 157 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +Variable_name Value +Cached_prepared_stmt_count 0 +set @a = 1; +EXECUTE stmt1 USING @a; +b +A1 +EXECUTE stmt1 USING @a; +b +A1 +EXECUTE stmt1 USING @a; +b +A1 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +Variable_name Value +Cached_prepared_stmt_count 1 +INSERT INTO t1 VALUES (7, "A7"); +INSERT INTO t1 VALUES (8, "A8"); +INSERT INTO t1 VALUES (9, "A9"); +INSERT INTO t1 VALUES (10, "A10"); +INSERT INTO t1 VALUES (11, "A11"); +INSERT INTO t1 VALUES (12, "A12"); +EXECUTE stmt1 USING @a; +b +A1 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +Variable_name Value +Cached_prepared_stmt_count 0 +EXECUTE stmt1 USING @a; +b +A1 +EXECUTE stmt1 USING @a; +b +A1 +EXECUTE stmt1 USING @a; +b +A1 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +Variable_name Value +Cached_prepared_stmt_count 1 +ALTER TABLE t1 ADD INDEX b(b); +EXECUTE stmt1 USING @a; +b +A1 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +Variable_name Value +Cached_prepared_stmt_count 0 +DROP TABLE IF EXISTS t3; +CREATE TABLE t3 (a INT, b VARCHAR(5), PRIMARY KEY(a, b)); +INSERT INTO t3 VALUES (1, "A1"); +INSERT INTO t3 VALUES (2, "A2"); +INSERT INTO t3 VALUES (3, "A3"); +INSERT INTO t3 VALUES (4, "A4"); +INSERT INTO t3 VALUES (5, "A5"); +INSERT INTO t3 VALUES (6, "A6"); +PREPARE stmt9 FROM 'SELECT b FROM t3 WHERE a = ? and b = "A1"'; +EXECUTE stmt9 USING @a; +b +A1 +EXECUTE stmt9 USING @a; +b +A1 +EXECUTE stmt9 USING @a; +b +A1 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +Variable_name Value +Cached_prepared_stmt_count 1 +SET character_set_client='latin1'; +EXECUTE stmt9 USING @a; +b +A1 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +Variable_name Value +Cached_prepared_stmt_count 0 +SET character_set_client=default; +EXECUTE stmt9 USING @a; +b +A1 +EXECUTE stmt9 USING @a; +b +A1 +EXECUTE stmt9 USING @a; +b +A1 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +Variable_name Value +Cached_prepared_stmt_count 1 +SET optimizer_switch='index_merge=off'; +EXECUTE stmt9 USING @a; +b +A1 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +Variable_name Value +Cached_prepared_stmt_count 0 +SET optimizer_switch=default; +DROP TABLE IF EXISTS t4; +CREATE TABLE t4 (a INT PRIMARY KEY, b DATE, INDEX idx(b)); +INSERT INTO t4 VALUES (1, '2005-01-01'); +INSERT INTO t4 VALUES (2, '2006-02-01'); +INSERT INTO t4 VALUES (3, '2007-03-01'); +INSERT INTO t4 VALUES (4, '2008-04-01'); +INSERT INTO t4 VALUES (5, '2009-05-01'); +INSERT INTO t4 VALUES (6, '2010-06-01'); +PREPARE stmt10 FROM 'SELECT * FROM t4 WHERE b BETWEEN ? AND ?'; +set @b1 = '2007-02-01'; +set @b2 = '2010-06-01'; +EXECUTE stmt10 USING @a1,@a2; +a b +EXECUTE stmt10 USING @a1,@a2; +a b +EXECUTE stmt10 USING @a1,@a2; +a b +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +Variable_name Value +Cached_prepared_stmt_count 0 +EXECUTE stmt10 USING @b1,@b2; +a b +3 2007-03-01 +4 2008-04-01 +5 2009-05-01 +6 2010-06-01 +EXECUTE stmt10 USING @b1,@b2; +a b +3 2007-03-01 +4 2008-04-01 +5 2009-05-01 +6 2010-06-01 +EXECUTE stmt10 USING @b1,@b2; +a b +3 2007-03-01 +4 2008-04-01 +5 2009-05-01 +6 2010-06-01 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +Variable_name Value +Cached_prepared_stmt_count 0 +DROP DATABASE plancache; diff --git a/mysql-test/t/plan_cache.test b/mysql-test/t/plan_cache.test new file mode 100644 index 0000000000000000000000000000000000000000..f8b0072b4ff0461cb5893c8291b74891edff4ca2 --- /dev/null +++ b/mysql-test/t/plan_cache.test @@ -0,0 +1,201 @@ +CREATE DATABASE plancache; +USE plancache; +--disable_warnings +DROP TABLE IF EXISTS t1; +--enable_warnings +CREATE TABLE t1 (a INT PRIMARY KEY, b VARCHAR(5)); +INSERT INTO t1 VALUES (1, "A1"); +INSERT INTO t1 VALUES (2, "A2"); +INSERT INTO t1 VALUES (3, "A3"); +INSERT INTO t1 VALUES (4, "A4"); +INSERT INTO t1 VALUES (5, "A5"); +INSERT INTO t1 VALUES (6, "A6"); + +# Test case 1: Turn on/off plan_cache. + +# initialize Cached_prepared_stmt_count should be 0 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +SET GLOBAL plan_cache = on; +PREPARE stmt1 FROM 'SELECT b FROM t1 WHERE a = ?'; +set @a = 1; +EXECUTE stmt1 USING @a; +EXECUTE stmt1 USING @a; +EXECUTE stmt1 USING @a; + +# stmt1 already been cached, Cached_prepared_stmt_count should be 1 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; + +# execute sql with plan cache +set @a = 2; +EXECUTE stmt1 USING @a; + +# turn off plan cache +SET GLOBAL plan_cache = off; +EXECUTE stmt1 USING @a; +# Cached_prepared_stmt_count should be 0 after plan_cache set off +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; + + + +# Test case 2: Plan cache support condition check. +SET GLOBAL plan_cache = on; + +# Test 2.1 SELECT DISTINCT +PREPARE stmt2 FROM 'SELECT DISTINCT b FROM t1 WHERE a = ?'; +set @a = 1; +EXECUTE stmt2 USING @a; +EXECUTE stmt2 USING @a; +EXECUTE stmt2 USING @a; +# Don't support distinct, Cached_prepared_stmt_count should be 0 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; + +# Test 2.2 FULL TABLE SELECT +PREPARE stmt3 FROM 'SELECT b FROM t1'; +EXECUTE stmt3; +EXECUTE stmt3; +EXECUTE stmt3; +# Don't support full table select, Cached_prepared_stmt_count should be 0 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; + +# Test 2.3 Subquery SELECT +PREPARE stmt4 FROM 'SELECT b FROM t1 where t1.a > (SELECT AVG(t2.a) FROM t1 t2 where t1.a = t2.a)'; +EXECUTE stmt4; +EXECUTE stmt4; +EXECUTE stmt4; +# Don't support subquery select, Cached_prepared_stmt_count should be 0 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; + +# Test 2.4 partitioned table +--disable_warnings +DROP TABLE IF EXISTS t2; +--enable_warnings +CREATE TABLE t2 (a INT PRIMARY KEY, b VARCHAR(5)) partition by hash(a) partitions 4; +INSERT INTO t2 VALUES (1, "A1"); +INSERT INTO t2 VALUES (2, "A2"); +INSERT INTO t2 VALUES (3, "A3"); +INSERT INTO t2 VALUES (4, "A4"); +INSERT INTO t2 VALUES (5, "A5"); +INSERT INTO t2 VALUES (6, "A6"); + +PREPARE stmt5 FROM 'SELECT b FROM t2 WHERE a = ?'; +set @a = 1; +EXECUTE stmt5 USING @a; +EXECUTE stmt5 USING @a; +EXECUTE stmt5 USING @a; +# Don't support partitioned table select, Cached_prepared_stmt_count should be 0 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; + +# Test 2.5 Don't support when the number of condition columns is greater than the number of index columns +PREPARE stmt6 FROM 'SELECT b FROM t1 WHERE a = ? and b = "A1"'; +set @a = 1; +EXECUTE stmt6 USING @a; +EXECUTE stmt6 USING @a; +EXECUTE stmt6 USING @a; +# Don't support when the number of condition columns is greater than the number of index columns, Cached_prepared_stmt_count should be 0 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; + +# Test 2.6 Don't support SQL like "WHERE col=@a" +PREPARE stmt7 FROM 'SELECT b FROM t1 WHERE a = @a'; +EXECUTE stmt7; +EXECUTE stmt7; +EXECUTE stmt7; +# Don't support SQL like "WHERE col=@a", Cached_prepared_stmt_count should be 0 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; + +# Test 2.7 Don't support performance_schema table +PREPARE stmt8 FROM 'SELECT event_name, count_star FROM performance_schema.events_waits_summary_global_by_event_name WHERE event_name IN ("idle")'; +EXECUTE stmt8; +EXECUTE stmt8; +EXECUTE stmt8; +# Don't support performance_schema table, Cached_prepared_stmt_count should be 0 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; + +# Test 2.8 Don't support when table stats changed +set @a = 1; +EXECUTE stmt1 USING @a; +EXECUTE stmt1 USING @a; +EXECUTE stmt1 USING @a; +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; + +INSERT INTO t1 VALUES (7, "A7"); +INSERT INTO t1 VALUES (8, "A8"); +INSERT INTO t1 VALUES (9, "A9"); +INSERT INTO t1 VALUES (10, "A10"); +INSERT INTO t1 VALUES (11, "A11"); +INSERT INTO t1 VALUES (12, "A12"); +EXECUTE stmt1 USING @a; +# Don't support when table stats changed, Cached_prepared_stmt_count should be 0 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; + +# Test 2.9 Don't support when table changed +EXECUTE stmt1 USING @a; +EXECUTE stmt1 USING @a; +EXECUTE stmt1 USING @a; +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; + +ALTER TABLE t1 ADD INDEX b(b); +EXECUTE stmt1 USING @a; +# Don't support when table changed, Cached_prepared_stmt_count should be 0 +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; + +# Test 2.10 Don't support when character set changed +--disable_warnings +DROP TABLE IF EXISTS t3; +--enable_warnings +CREATE TABLE t3 (a INT, b VARCHAR(5), PRIMARY KEY(a, b)); +INSERT INTO t3 VALUES (1, "A1"); +INSERT INTO t3 VALUES (2, "A2"); +INSERT INTO t3 VALUES (3, "A3"); +INSERT INTO t3 VALUES (4, "A4"); +INSERT INTO t3 VALUES (5, "A5"); +INSERT INTO t3 VALUES (6, "A6"); + +PREPARE stmt9 FROM 'SELECT b FROM t3 WHERE a = ? and b = "A1"'; +EXECUTE stmt9 USING @a; +EXECUTE stmt9 USING @a; +EXECUTE stmt9 USING @a; +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; + +SET character_set_client='latin1'; +EXECUTE stmt9 USING @a; +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; + +SET character_set_client=default; + +# Test 2.11 Don't support when optimizer_switch changed +EXECUTE stmt9 USING @a; +EXECUTE stmt9 USING @a; +EXECUTE stmt9 USING @a; +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; + +SET optimizer_switch='index_merge=off'; +EXECUTE stmt9 USING @a; +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +SET optimizer_switch=default; + +# Test 2.12 Normal index check +--disable_warnings +DROP TABLE IF EXISTS t4; +--enable_warnings +CREATE TABLE t4 (a INT PRIMARY KEY, b DATE, INDEX idx(b)); +INSERT INTO t4 VALUES (1, '2005-01-01'); +INSERT INTO t4 VALUES (2, '2006-02-01'); +INSERT INTO t4 VALUES (3, '2007-03-01'); +INSERT INTO t4 VALUES (4, '2008-04-01'); +INSERT INTO t4 VALUES (5, '2009-05-01'); +INSERT INTO t4 VALUES (6, '2010-06-01'); + +PREPARE stmt10 FROM 'SELECT * FROM t4 WHERE b BETWEEN ? AND ?'; +set @b1 = '2007-02-01'; +set @b2 = '2010-06-01'; +EXECUTE stmt10 USING @a1,@a2; +EXECUTE stmt10 USING @a1,@a2; +EXECUTE stmt10 USING @a1,@a2; +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; +EXECUTE stmt10 USING @b1,@b2; +EXECUTE stmt10 USING @b1,@b2; +EXECUTE stmt10 USING @b1,@b2; +SHOW GLOBAL STATUS LIKE 'Cached_prepared_stmt_count'; + +# Cleanup +DROP DATABASE plancache;