1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
#!/usr/bin/env tarantool
test = require("sqltester")
test:plan(13)
--!./tcltestrunner.lua
-- 2012 November 9
--
-- The author disclaims copyright to this source code. In place of
-- a legal notice, here is a blessing:
--
-- May you do good and not evil.
-- May you find forgiveness for yourself and forgive others.
-- May you share freely, never taking more than you give.
--
-------------------------------------------------------------------------
--
-- Test cases for query planning decisions.
--
-- The tests in this file demonstrate the behaviour of the query planner
-- in determining the order in which joined tables are scanned.
--
-- Assume there are two tables being joined - t1 and t2. Each has a cost
-- if it is the outer loop, and a cost if it is the inner loop. As follows:
--
-- t1(outer) - cost of scanning t1 as the outer loop.
-- t1(inner) - cost of scanning t1 as the inner loop.
-- t2(outer) - cost of scanning t2 as the outer loop.
-- t2(inner) - cost of scanning t2 as the inner loop.
--
-- Depending on the order in which the planner nests the scans, the total
-- cost of the join query is one of:
--
-- t1(outer) * t2(inner)
-- t2(outer) * t1(inner)
--
-- The tests in this file attempt to verify that the planner nests joins in
-- the correct order when the following are true:
--
-- + (t1(outer) * t2(inner)) > (t1(inner) * t2(outer)
-- + t1(outer) < t2(outer)
--
-- In other words, when the best overall query plan has t2 as the outer loop,
-- but when the outer loop is considered independent of the inner, t1 is the
-- most efficient choice.
--
-- In order to make them more predictable, automatic indexes are turned off for
-- the tests in this file.
--
-- ["set","testdir",[["file","dirname",["argv0"]]]]
-- ["source",[["testdir"],"\/tester.tcl"]]
testprefix = "whereF"
test:do_execsql_test(
1.0,
[[
CREATE TABLE t1(a INT PRIMARY KEY, b INT, c INT);
CREATE TABLE t2(d INT PRIMARY KEY, e INT, f INT);
]], {
-- <1.0>
-- </1.0>
})
for tn, sql in ipairs({"SELECT * FROM t1, t2 WHERE t1.a=t2.e AND t2.d<t1.b AND t1.c!=10",
"SELECT * FROM t2, t1 WHERE t1.a=t2.e AND t2.d<t1.b AND t1.c!=10",
"SELECT * FROM t2 CROSS JOIN t1 WHERE t1.a=t2.e AND t2.d<t1.b AND t1.c!=10"}) do
--X(0, "X!foreach", [=[["tn sql","\n 1 \"SELECT * FROM t1, t2 WHERE t1.a=t2.e AND t2.d<t1.b AND t1.c!=10\"\n 2 \"SELECT * FROM t2, t1 WHERE t1.a=t2.e AND t2.d<t1.b AND t1.c!=10\"\n 3 \"SELECT * FROM t2 CROSS JOIN t1 WHERE t1.a=t2.e AND t2.d<t1.b AND t1.c!=10\"\n"]]=]) do
test:do_test(
"1."..tn,
function()
return test:execsql("EXPLAIN QUERY PLAN "..sql)
end, {
'/SCAN TABLE T2/',
'/SEARCH TABLE T1/'
})
end
test:do_execsql_test(
2.0,
[[
DROP TABLE t1;
DROP TABLE t2;
CREATE TABLE t1(a INT PRIMARY KEY, b INT, c INT);
CREATE TABLE t2(d INT PRIMARY KEY, e INT, f INT);
CREATE UNIQUE INDEX i2 ON t1(b);
]], {
-- <2.0>
-- </2.0>
})
-- for _ in X(0, "X!foreach", [=[["tn sql","\n 1 \"SELECT * FROM t1, t2 WHERE t1.a>? AND t2.d>t1.c AND t1.b=t2.e\"\n 2 \"SELECT * FROM t2, t1 WHERE t1.a>? AND t2.d>t1.c AND t1.b=t2.e\"\n 3 \"SELECT * FROM t2 CROSS JOIN t1 WHERE t1.a>? AND t2.d>t1.c AND t1.b=t2.e\"\n"]]=]) do
for tn, sql in ipairs({"SELECT * FROM t1, t2 WHERE t1.a>? AND t2.d>t1.c AND t1.b=t2.e",
"SELECT * FROM t2, t1 WHERE t1.a>? AND t2.d>t1.c AND t1.b=t2.e",
"SELECT * FROM t2 CROSS JOIN t1 WHERE t1.a>? AND t2.d>t1.c AND t1.b=t2.e"}) do
test:do_test(
"2."..tn,
function()
return test:execsql("EXPLAIN QUERY PLAN "..sql)
end, {
'/SCAN TABLE T2/',
'/SEARCH TABLE T1/'
})
end
test:do_execsql_test(
3.0,
[[
DROP TABLE t1;
DROP TABLE t2;
CREATE TABLE t1(a INT, b INT, c INT, PRIMARY KEY(a,b));
CREATE TABLE t2(d INT PRIMARY KEY, e INT, f INT);
]], {
-- <3.0>
-- </3.0>
})
-- Test plan changed because of changes in index representation
for tn, sql in ipairs({[[SELECT t1.a, t1.b, t2.d, t2.e FROM t1, t2
WHERE t2.d=t1.b AND t1.a=(t2.d+1) AND t1.b = (t2.e+1)]],
[[SELECT t1.a, t1.b, t2.d, t2.e FROM t2, t1
WHERE t2.d=t1.b AND t1.a=(t2.d+1) AND t1.b = (t2.e+1)]] }) do
test:do_test(
"3."..tn,
function()
return test:execsql("EXPLAIN QUERY PLAN "..sql)
end, {
'/SCAN TABLE T1/',
'/SEARCH TABLE T2/'
})
end
test:do_test(
"3.3",
function()
local sql = [[SELECT t1.a, t1.b, t2.d, t2.e FROM t2 CROSS JOIN t1
WHERE t2.d=t1.b AND t1.a=(t2.d+1) AND t1.b = (t2.e+1)]]
return test:execsql("EXPLAIN QUERY PLAN "..sql)
end, {
'/SCAN TABLE T2/',
'/SEARCH TABLE T1/'
})
test:do_execsql_test(
4.0,
[[
CREATE TABLE t4(a INT,b INT,c INT,d INT,e INT, PRIMARY KEY(a,b,c));
CREATE INDEX t4adc ON t4(a,d,c);
CREATE UNIQUE INDEX t4aebc ON t4(a,e,b,c);
EXPLAIN QUERY PLAN SELECT a FROM t4 WHERE a=? AND b=?;
]], {
-- <4.0>
"/A=. AND B=./"
-- </4.0>
})
test:finish_test()
|