mariadb/mysql-test/main/optimizer_costs.test

190 lines
6.9 KiB
Text
Raw Normal View History

Added test cases for preceding test This includes all test changes from "Changing all cost calculation to be given in milliseconds" and forwards. Some of the things that caused changes in the result files: - As part of fixing tests, I added 'echo' to some comments to be able to easier find out where things where wrong. - MATERIALIZED has now a higher cost compared to X than before. Because of this some MATERIALIZED types have changed to DEPENDEND SUBQUERY. - Some test cases that required MATERIALIZED to repeat a bug was changed by adding more rows to force MATERIALIZED to happen. - 'Filtered' in SHOW EXPLAIN has in many case changed from 100.00 to something smaller. This is because now filtered also takes into account the smallest possible ref access and filters, even if they where not used. Another reason for 'Filtered' being smaller is that we now also take into account implicit filtering done for subqueries using FIRSTMATCH. (main.subselect_no_exists_to_in) This is caluculated in best_access_path() and stored in records_out. - Table orders has changed because more accurate costs. - 'index' and 'ALL' for small tables has changed to use 'range' or 'ref' because of optimizer_scan_setup_cost. - index can be changed to 'range' as 'range' optimizer assumes we don't have to read the blocks from disk that range optimizer has already read. This can be confusing in the case where there is no obvious where clause but instead there is a hidden 'key_column > NULL' added by the optimizer. (main.subselect_no_exists_to_in) - Scan on primary clustered key does not report 'Using Index' anymore (It's a table scan, not an index scan). - For derived tables, the number of rows is now 100 instead of 2, which can be seen in EXPLAIN. - More tests have "Using index for group by" as the cost of this optimization is now more correct (lower). - A primary key could be preferred for a normal key, even if it would access more rows, as it's faster to do 1 lokoup and 3 'index_next' on a clustered primary key than one lookup trough a secondary. (main.stat_tables_innodb) Notes: - There was a 4.7% more calls to best_extension_by_limited_search() in the main.greedy_optimizer test. However examining the test results it looked that the plans where slightly better (eq_ref where more chained together) so I assume this is ok. - I have verified a few test cases where there was notable/unexpected changes in the plan and in all cases the new optimizer plans where faster. (main.greedy_optimizer and some others)
2022-10-04 15:16:06 +02:00
#
# Test of optimizer_costs
#
--source include/have_innodb.inc
--source include/have_sequence.inc
select table_name,engine from information_schema.tables where table_name="optimizer_costs";
show create table information_schema.optimizer_costs;
let $start_engines=`select count(*) from information_schema.optimizer_costs`;
--vertical_results
select * from information_schema.optimizer_costs where engine in
("memory","innodb","aria","default") order by engine;
--horizontal_results
show variables like "optimizer%cost";
show variables like "optimizer_disk_read_ratio";
--echo #
--echo # Test change some 'default' variables
--echo #
SELECT @@optimizer_disk_read_ratio,@@optimizer_index_block_copy_cost;
SET global optimizer_disk_read_ratio=0.8;
SET global optimizer_index_block_copy_cost=0.1;
SELECT @@optimizer_disk_read_ratio,@@optimizer_index_block_copy_cost;
select optimizer_disk_read_ratio,optimizer_index_block_copy_cost from information_schema.optimizer_costs where engine='default';
SET global optimizer_disk_read_ratio=default;
SET global optimizer_index_block_copy_cost=default;
SELECT @@optimizer_disk_read_ratio,@@optimizer_index_block_copy_cost;
--echo #
--echo # Test change some 'engine' variables
--echo #
select @@MEMORY.optimizer_row_lookup_cost;
set @tmp=@@MEMORY.optimizer_row_lookup_cost;
set @@global.MEMORY.optimizer_row_lookup_cost=1;
select @@MEMORY.optimizer_row_lookup_cost;
set @@global.MEMORY.optimizer_row_lookup_cost=default;
select @@MEMORY.optimizer_row_lookup_cost;
set @@global.MEMORY.optimizer_row_lookup_cost=@tmp;
select @@MEMORY.optimizer_row_lookup_cost;
--echo #
--echo # Print variables with different syntaxes
--echo #
SHOW VARIABLES like "optimizer_row_lookup_cost";
SELECT @@optimizer_row_lookup_cost;
SELECT @@global.default.optimizer_row_lookup_cost;
SELECT @@global.default.`optimizer_row_lookup_cost`;
SELECT @@MEMORY.optimizer_row_lookup_cost;
SELECT @@memory.optimizer_row_lookup_cost;
SELECT @@InnoDB.optimizer_row_lookup_cost;
--echo #
--echo # Accessing not existing cost
--echo #
SELECT @@not_existing.optimizer_row_lookup_cost;
SELECT @@NOT_existing.optimizer_row_lookup_cost;
select engine from information_schema.optimizer_costs where engine like '%existing';
--echo #
--echo # Creating a new cost structure
--echo #
SET global new_engine.optimizer_disk_read_cost=100;
select * from information_schema.optimizer_costs where engine like 'new_engine';
select @@new_engine.optimizer_disk_read_cost, @@new_engine.optimizer_row_copy_cost;
--echo #
--echo # Errors
--echo #
--error ER_PARSE_ERROR
SELECT @@default.optimizer_disk_read_cost;
--error ER_WRONG_TYPE_FOR_VAR
set global Aria.optimizer_disk_read_cost=NULL;
set @tmp=@@Aria.optimizer_disk_read_cost;
SET global Aria.optimizer_disk_read_cost=-1;
select @@Aria.optimizer_disk_read_cost;
SET global Aria.optimizer_disk_read_cost=200000;
select @@Aria.optimizer_disk_read_cost;
set global Aria.optimizer_disk_read_cost=@tmp;
select @@Aria.optimizer_disk_read_cost;
--echo #
--echo # Test of cost of ref compared to table scan + join_cache
--echo #
create or replace table t1 (p int primary key, a char(10)) engine=myisam;
create or replace table t2 (p int primary key, i int, a char(10), key k2(a)) engine=myisam;
insert into t2 select seq,seq,'a' from seq_1_to_512;
insert into t1 select seq,'a' from seq_1_to_4;
explain select count(*) from t1, t2 where t1.p = t2.i;
insert into t1 select seq,'a' from seq_5_to_10;
explain select count(*) from t1, t2 where t1.p = t2.i;
drop table t1,t2;
--echo #
--echo # Test of optimizer_scan_setup_cost
--echo #
create table t1 (p int primary key, a char(10)) engine=myisam;
create table t2 (p int primary key, i int, a char(10), key k1(i), key k2(a)) engine=myisam;
insert into t1 values (2, 'qqqq'), (11, 'yyyy');
insert into t2 values (1, 2, 'qqqq'), (2, 2, 'pppp'),
(3, 2, 'yyyy'), (4, 3, 'zzzz');
set @org_myisam_disk_read_ratio=@@myisam.optimizer_disk_read_ratio;
set @@optimizer_scan_setup_cost=10,@@global.myisam.optimizer_disk_read_ratio=0.2;
flush tables;
explain select sum(t2.p+length(t1.a)) from t1, t2 where t1.p = t2.i;
set @@optimizer_scan_setup_cost=0.0, @@global.myisam.optimizer_disk_read_ratio=0.0;
flush tables;
explain select sum(t2.p+length(t1.a)) from t1, t2 where t1.p = t2.i;
set @@optimizer_scan_setup_cost=default,@@global.myisam.optimizer_disk_read_ratio=@org_myisam_disk_read_ratio;
flush tables;
drop table t1,t2;
--echo #
--echo # Test of group by optimization
--echo #
set @@optimizer_scan_setup_cost=0;
CREATE TABLE t1 (id INT NOT NULL, a DATE, KEY(id,a)) engine=myisam;
INSERT INTO t1 values (1,'2001-01-01'),(1,'2001-01-02'),
(1,'2001-01-03'),(1,'2001-01-04'),
(2,'2001-01-01'),(2,'2001-01-02'),
(2,'2001-01-03'),(2,'2001-01-04'),
(3,'2001-01-01'),(3,'2001-01-02'),
(3,'2001-01-03'),(3,'2001-01-04'),
(4,'2001-01-01'),(4,'2001-01-02'),
(4,'2001-01-03'),(4,'2001-01-04');
analyze table t1;
EXPLAIN SELECT id,MIN(a),MAX(a) FROM t1 WHERE a>=20010104e0 GROUP BY id;
insert into t1 values (3,'2001-01-03'),(3,'2001-01-04');
insert into t1 values (3,'2001-01-03'),(3,'2001-01-04');
insert into t1 values (3,'2001-01-03'),(3,'2001-01-04');
insert into t1 values (3,'2001-01-03'),(3,'2001-01-04');
analyze table t1;
EXPLAIN SELECT id,MIN(a),MAX(a) FROM t1 WHERE a>=20010104e0 GROUP BY id;
drop table t1;
set @@optimizer_scan_setup_cost=default;
--echo #
--echo # Test of straight join costs
--echo #
create table t1 (l_orderkey int(11) NOT NULL,
l_partkey int(11) DEFAULT NULL,
l_suppkey int(11) DEFAULT NULL,
PRIMARY KEY (l_orderkey)) engine=aria;
insert into t1 select seq,seq,seq from seq_1_to_1000;
explain select straight_join count(*) from seq_1_to_10000,t1 where seq=l_orderkey;
show status like "last_query_cost";
set @org_cost=@@aria.optimizer_key_next_find_cost;
# Set cost for t1 high so that we cannot use it for index scans
set global aria.optimizer_key_next_find_cost=1000;
flush tables;
explain select count(*) from seq_1_to_10000,t1 where seq=l_orderkey;
show status like "last_query_cost";
set global aria.optimizer_key_next_find_cost=@org_cost;
drop table t1;
--echo #
--echo # Testing distinct group optimization
--echo #
create table t1 (a int, b int, key(a,b));
insert into t1 select seq,seq from seq_1_to_1000;
explain select count(distinct a,b) from t1;
explain select count(distinct a,b) from t1 where a>100;
explain select count(distinct a,b) from t1 where a>800;
update t1 set a=mod(a,10);
analyze table t1;
explain select count(distinct a,b) from t1;
explain select count(distinct a,b) from t1 where a>1;
explain select count(distinct a,b) from t1 where a>8;
update t1 set b=mod(b,2);
analyze table t1;
explain select count(distinct a,b) from t1;
explain select count(distinct a,b) from t1 where a>1;
explain select count(distinct a,b) from t1 where a>8;
drop table t1;
--echo #
--echo # cleanup
--echo #
let $end_engines=`select count(*) from information_schema.optimizer_costs`;
let $diff=`select $end_engines - $start_engines`;
--echo "New cost structures: $diff (should be 1)"