"t1, t2, t3 " +
"where t1.hundred = t2.hundred " +
"and t1.twenty = t2.twenty and " +
"t2.two = t3.two").close();
checkEstimatedRowCount(conn,1.606007503125E7);
RuntimeStatisticsParser rtsp =
SQLUtilities.getRuntimeStatisticsParser(s);
assertTrue(rtsp.findString("Table Scan ResultSet for T1", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 2, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T2", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 2, 3}", 1));
assertTrue(rtsp.findString(
"Hash Scan ResultSet for T3 using index T3_TWO_TWENTY", 1));
// t1 x t2 -> 16 * 10^4.
// x t3 -> 32 * 10^7
// additional pred -> 32 * 10^5
s.executeQuery("select t1.id from " +
"--DERBY-PROPERTIES joinOrder=fixed \n" +
"t1, t2, t3 " +
"where t1.hundred = t2.hundred " +
"and t2.two = t3.two and " +
"t1.hundred = t3.hundred").close();
checkEstimatedRowCount(conn,3212015.00625);
rtsp = SQLUtilities.getRuntimeStatisticsParser(s);
assertTrue(rtsp.findString("Table Scan ResultSet for T1", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T2", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T3", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
// variations on above query; try different join strategies
s.executeQuery("select t1.id from " +
"--DERBY-PROPERTIES joinOrder=fixed \n" +
"t1, t2, t3 --DERBY-PROPERTIES joinStrategy=hash \n" +
"where t1.hundred = t2.hundred " +
"and t2.two = t3.two " +
"and t1.hundred = t3.hundred").close();
checkEstimatedRowCount(conn,3212015.00625);
rtsp = SQLUtilities.getRuntimeStatisticsParser(s);
assertTrue(rtsp.findString("Table Scan ResultSet for T1", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T2", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T3", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
s.executeQuery("select t1.id from " +
"--DERBY-PROPERTIES joinOrder=fixed \n" +
"t1, t2, t3 --DERBY-PROPERTIES joinStrategy=nestedLoop \n" +
"where t1.hundred = t2.hundred " +
"and t2.two = t3.two " +
"and t1.hundred = t3.hundred").close();
checkEstimatedRowCount(conn,3212015.00625);
rtsp = SQLUtilities.getRuntimeStatisticsParser(s);
assertTrue(rtsp.findString("Table Scan ResultSet for T1", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T2", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
assertTrue(rtsp.usedSpecificIndexForIndexScan("T3", "T3_HUNDRED"));
assertTrue(rtsp.findString("Bit set of columns fetched=All", 1));
s.executeQuery("select t1.id from " +
"--DERBY-PROPERTIES joinOrder=fixed \n" +
"t1, t2 --DERBY-PROPERTIES joinStrategy=hash \n, t3 " +
"where t1.hundred = t2.hundred " +
"and t2.two = t3.two " +
"and t1.hundred = t3.hundred").close();
checkEstimatedRowCount(conn,3212015.00625);
rtsp = SQLUtilities.getRuntimeStatisticsParser(s);
assertTrue(rtsp.findString("Table Scan ResultSet for T1", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T2", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T3", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
s.executeQuery("select t1.id from " +
"--DERBY-PROPERTIES joinOrder=fixed \n" +
"t1, t2 --DERBY-PROPERTIES joinStrategy=hash \n, t3 " +
"where t1.hundred = t2.hundred " +
"and t2.two = t3.two " +
"and t1.hundred = t3.hundred").close();
checkEstimatedRowCount(conn,3212015.00625);
rtsp = SQLUtilities.getRuntimeStatisticsParser(s);
assertTrue(rtsp.findString("Table Scan ResultSet for T1", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T2", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T3", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
// duplicate predicates; this time t1.hundred=?
// will show up twice when t1 is optimized at the end
// selectivity should be same as above
s.executeQuery("select t1.id from " +
"--DERBY-PROPERTIES joinOrder=fixed \n" +
"t2, t3, t1 " +
"where t1.hundred = t2.hundred " +
"and t2.two = t3.two " +
"and t1.hundred = t3.hundred").close();
checkEstimatedRowCount(conn,3212015.00625);
rtsp = SQLUtilities.getRuntimeStatisticsParser(s);
assertTrue(rtsp.findString("Table Scan ResultSet for T2", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T3", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T1", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 3}", 1));
// variations on above query; try different join strategies
s.executeQuery("select t1.id from " +
"--DERBY-PROPERTIES joinOrder=fixed \n" +
"t3, t2, t1 --DERBY-PROPERTIES joinStrategy=hash \n" +
"where t1.hundred = t2.hundred " +
"and t2.two = t3.two " +
"and t1.hundred = t3.hundred").close();
checkEstimatedRowCount(conn,3212015.00625);
rtsp = SQLUtilities.getRuntimeStatisticsParser(s);
assertTrue(rtsp.findString("Table Scan ResultSet for T3", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T2", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T1", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 3}", 1));
s.executeQuery("select t1.id from " +
"--DERBY-PROPERTIES joinOrder=fixed \n" +
"t3, t2, t1 --DERBY-PROPERTIES joinStrategy=nestedLoop \n" +
"where t1.hundred = t2.hundred " +
"and t2.two = t3.two " +
"and t1.hundred = t3.hundred").close();
checkEstimatedRowCount(conn,3212015.00625);
rtsp = SQLUtilities.getRuntimeStatisticsParser(s);
assertTrue(rtsp.findString("Table Scan ResultSet for T3", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T2", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
assertTrue(rtsp.usedSpecificIndexForIndexScan("T1", "T1_HUNDRED"));
assertTrue(rtsp.findString("Bit set of columns fetched=All", 1));
s.executeQuery("select t1.id from " +
"--DERBY-PROPERTIES joinOrder=fixed \n" +
"t2, t3 --DERBY-PROPERTIES joinStrategy=nestedLoop \n, t1 " +
"where t1.hundred = t2.hundred " +
"and t2.two = t3.two " +
"and t1.hundred = t3.hundred").close();
checkEstimatedRowCount(conn,3212015.00625);
rtsp = SQLUtilities.getRuntimeStatisticsParser(s);
assertTrue(rtsp.findString("Table Scan ResultSet for T2", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
assertTrue(rtsp.usedSpecificIndexForIndexScan("T3", "T3_HUNDRED"));
assertTrue(rtsp.findString("Bit set of columns fetched=All", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T1", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 3}", 1));
s.executeQuery("select t1.id from " +
"--DERBY-PROPERTIES joinOrder=fixed \n" +
"t3, t2 --DERBY-PROPERTIES joinStrategy=hash \n, t1 " +
"where t1.hundred = t2.hundred " +
"and t2.two = t3.two " +
"and t1.hundred = t3.hundred").close();
checkEstimatedRowCount(conn,3212015.00625);
rtsp = SQLUtilities.getRuntimeStatisticsParser(s);
assertTrue(rtsp.findString("Table Scan ResultSet for T3", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T2", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T1", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 3}", 1));
// some more variations on the above theme
// some constant predicates thrown in.
// remember hundred is a char column
// -- for some reason if you give the constant
// as a numeric argument it doesn't recognize that
// as a constant start/stop value for the index
// The error is that the types must be comparable.
s.executeQuery("select t1.id from " +
"--DERBY-PROPERTIES joinOrder=fixed \n" +
"t2, t3, t1 " +
"where t1.hundred = t2.hundred " +
"and t2.two = t3.two " +
"and t1.hundred = t3.hundred " +
"and t1.hundred='1'").close();
checkEstimatedRowCount(conn,30458.025);
rtsp = SQLUtilities.getRuntimeStatisticsParser(s);
assertTrue(rtsp.usedSpecificIndexForIndexScan("T2", "T2_HUNDRED"));
assertTrue(rtsp.findString("Bit set of columns fetched=All", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T3", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
assertTrue(rtsp.usedSpecificIndexForIndexScan("T1", "T1_HUNDRED"));
assertTrue(rtsp.findString("Bit set of columns fetched=All", 1));
// we have t1.100=t2.100 and t1.100=t3.100, so
// t2.100=t3.100 is redundant.
// row count shouldn't factor in the redundant predicate.
// row count should be 3200000.0
s.executeQuery("select t1.id from " +
"--DERBY-PROPERTIES joinOrder=fixed \n" +
"t2, t3, t1 " +
"where t1.hundred = t2.hundred " +
"and t2.two = t3.two " +
"and t1.hundred = t3.hundred " +
"and t2.hundred = t3.hundred").close();
checkEstimatedRowCount(conn,3212015.00625);
rtsp = SQLUtilities.getRuntimeStatisticsParser(s);
assertTrue(rtsp.findString("Table Scan ResultSet for T2", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T3", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T1", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 3}", 1));
// slightly different join predicates-- use composite stats.
// t1 x t2 --> 16 * 10.4.
// x t3 --> 16 * 10.4 * 4000 * 1/40 = 16*10.6
s.executeQuery("select t1.id from " +
"--DERBY-PROPERTIES joinOrder=fixed \n" +
"t2, t3, t1 " +
"where t1.hundred = t2.hundred " +
"and t2.two = t3.two " +
"and t2.twenty = t3.twenty").close();
checkEstimatedRowCount(conn,1.606007503125E7);
rtsp = SQLUtilities.getRuntimeStatisticsParser(s);
assertTrue(rtsp.findString("Table Scan ResultSet for T2", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 2, 3}", 1));
assertTrue(rtsp.findString(
"Hash Scan ResultSet for T3 using index T3_TWO_TWENTY", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 1}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T1", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 3}", 1));
// same as above but muck around with join order.
s.executeQuery("select t1.id from " +
"--DERBY-PROPERTIES joinOrder=fixed \n" +
"t1, t2, t3 " +
"where t1.hundred = t2.hundred " +
"and t2.two = t3.two " +
"and t2.twenty = t3.twenty").close();
checkEstimatedRowCount(conn,1.606007503125E7);
rtsp = SQLUtilities.getRuntimeStatisticsParser(s);
assertTrue(rtsp.findString("Table Scan ResultSet for T1", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T2", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 2, 3}", 1));
assertTrue(rtsp.findString(
"Hash Scan ResultSet for T3 using index T3_TWO_TWENTY", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 1}", 1));
s.executeQuery("select t1.id from " +
"--DERBY-PROPERTIES joinOrder=fixed \n" +
"t2, t1, t3 " +
"where t1.hundred = t2.hundred " +
"and t2.two = t3.two " +
"and t2.twenty = t3.twenty").close();
checkEstimatedRowCount(conn,1.606007503125E7);
rtsp = SQLUtilities.getRuntimeStatisticsParser(s);
assertTrue(rtsp.findString("Table Scan ResultSet for T2", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 2, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T1", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 3}", 1));
assertTrue(rtsp.findString(
"Hash Scan ResultSet for T3 using index T3_TWO_TWENTY", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 1}", 1));
s.executeQuery("select t1.id from " +
"--DERBY-PROPERTIES joinOrder=fixed \n" +
"t1, t3, t2 " +
"where t1.hundred = t2.hundred " +
"and t2.two = t3.two " +
"and t2.twenty = t3.twenty").close();
checkEstimatedRowCount(conn,1.606007503125E7);
rtsp = SQLUtilities.getRuntimeStatisticsParser(s);
assertTrue(rtsp.findString("Table Scan ResultSet for T1", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 3}", 1));
assertTrue(rtsp.usedSpecificIndexForIndexScan("T3", "T3_TWO_TWENTY"));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 1}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T2", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 2, 3}", 1));
s.executeQuery("select t1.id from " +
"--DERBY-PROPERTIES joinOrder=fixed \n" +
"t3, t2, t1 " +
"where t1.hundred = t2.hundred " +
"and t2.two = t3.two " +
"and t2.twenty = t3.twenty").close();
checkEstimatedRowCount(conn,1.606007503125E7);
rtsp = SQLUtilities.getRuntimeStatisticsParser(s);
assertTrue(rtsp.usedSpecificIndexForIndexScan("T3", "T3_TWO_TWENTY"));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 1}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T2", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 2, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T1", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 3}", 1));
s.executeQuery("select t1.id from " +
"--DERBY-PROPERTIES joinOrder=fixed \n" +
"t3, t1, t2 " +
"where t1.hundred = t2.hundred " +
"and t2.two = t3.two " +
"and t2.twenty = t3.twenty").close();
checkEstimatedRowCount(conn,1.606007503125E7);
rtsp = SQLUtilities.getRuntimeStatisticsParser(s);
assertTrue(rtsp.usedSpecificIndexForIndexScan("T3", "T3_TWO_TWENTY"));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 1}", 1));
assertTrue(rtsp.findString("Table Scan ResultSet for T1", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T2", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 2, 3}", 1));
// and just for fun, what would we have gotten without statistics.
s.executeQuery("select t1.id from " +
"--DERBY-PROPERTIES useStatistics=false, joinOrder=fixed \n" +
"t3, t1, t2 " +
"where t1.hundred = t2.hundred " +
"and t2.two = t3.two " +
"and t2.twenty = t3.twenty").close();
checkEstimatedRowCount(conn,6.4240300125000015E7);
rtsp = SQLUtilities.getRuntimeStatisticsParser(s);
assertTrue(rtsp.usedSpecificIndexForIndexScan("T3", "T3_TWO_TWENTY"));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 1}", 1));
assertTrue(rtsp.findString("Table Scan ResultSet for T1", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={0, 3}", 1));
assertTrue(rtsp.findString("Hash Scan ResultSet for T2", 1));
assertTrue(rtsp.findString("Bit set of columns fetched={1, 2, 3}", 1));
}