1282
1282
# Test using INDEX and IGNORE INDEX
1285
explain select t2.fld3 from t2 where fld3 = 'honeysuckle';
1287
explain select fld3 from t2 ignore index (fld3) where fld3 = 'honeysuckle';
1288
explain select fld3 from t2 use index (fld1) where fld3 = 'honeysuckle';
1290
explain select fld3 from t2 use index (fld3) where fld3 = 'honeysuckle';
1291
explain select fld3 from t2 use index (fld1,fld3) where fld3 = 'honeysuckle';
1285
#explain select t2.fld3 from t2 where fld3 = 'honeysuckle';
1287
#explain select fld3 from t2 ignore index (fld3) where fld3 = 'honeysuckle';
1288
#explain select fld3 from t2 use index (fld1) where fld3 = 'honeysuckle';
1290
#explain select fld3 from t2 use index (fld3) where fld3 = 'honeysuckle';
1291
#explain select fld3 from t2 use index (fld1,fld3) where fld3 = 'honeysuckle';
1294
1294
# NOTE NOTE NOTE
1295
1295
# The next should give an error
1298
explain select fld3 from t2 ignore index (fld3,not_used);
1299
explain select fld3 from t2 use index (not_used);
1299
#explain select fld3 from t2 ignore index (fld3,not_used);
1301
#explain select fld3 from t2 use index (not_used);
1302
1304
# Test sorting with a used key (there is no need for sorting)
1305
1307
select t2.fld3 from t2 where fld3 >= 'honeysuckle' and fld3 <= 'honoring' order by fld3;
1306
explain select t2.fld3 from t2 where fld3 >= 'honeysuckle' and fld3 <= 'honoring' order by fld3;
1308
#explain select t2.fld3 from t2 where fld3 >= 'honeysuckle' and fld3 <= 'honoring' order by fld3;
1307
1309
select fld1,fld3 from t2 where fld3="Colombo" or fld3 = "nondecreasing" order by fld3;
1443
1443
# Here the last fld3 is optimized away from the order by
1446
explain select t3.t2nr,fld3 from t2,t3 where t2.companynr = 34 and t2.fld1=t3.t2nr order by t3.t2nr,fld3;
1446
#explain select t3.t2nr,fld3 from t2,t3 where t2.companynr = 34 and t2.fld1=t3.t2nr order by t3.t2nr,fld3;
1449
1449
# Some test with ORDER BY and limit
1452
explain select * from t3 as t1,t3 where t1.period=t3.period order by t3.period;
1453
explain select * from t3 as t1,t3 where t1.period=t3.period order by t3.period limit 10;
1454
explain select * from t3 as t1,t3 where t1.period=t3.period order by t1.period limit 10;
1452
#explain select * from t3 as t1,t3 where t1.period=t3.period order by t3.period;
1453
#explain select * from t3 as t1,t3 where t1.period=t3.period order by t3.period limit 10;
1454
#explain select * from t3 as t1,t3 where t1.period=t3.period order by t1.period limit 10;
1457
1457
# Search with a constant table.
1540
1540
select t2.companynr,companyname from t2 left join t4 using (companynr) where t4.companynr is null;
1541
1541
select count(*) from t2 left join t4 using (companynr) where t4.companynr is not null;
1542
explain select t2.companynr,companyname from t2 left join t4 using (companynr) where t4.companynr is null;
1543
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr is null;
1542
#explain select t2.companynr,companyname from t2 left join t4 using (companynr) where t4.companynr is null;
1543
#explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr is null;
1545
1545
select companynr,companyname from t2 left join t4 using (companynr) where companynr is null;
1546
1546
select count(*) from t2 left join t4 using (companynr) where companynr is not null;
1547
explain select companynr,companyname from t2 left join t4 using (companynr) where companynr is null;
1548
explain select companynr,companyname from t4 left join t2 using (companynr) where companynr is null;
1547
#explain select companynr,companyname from t2 left join t4 using (companynr) where companynr is null;
1548
#explain select companynr,companyname from t4 left join t2 using (companynr) where companynr is null;
1549
1549
delete from t2 where fld1=999999;
1552
1552
# Test left join optimization
1554
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0;
1555
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 or t2.companynr < 0;
1556
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 and t4.companynr > 0;
1558
explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0;
1559
explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 or companynr < 0;
1560
explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 and companynr > 0;
1561
# Following can't be optimized
1562
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 or t2.companynr is null;
1563
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 or t2.companynr < 0 or t4.companynr > 0;
1564
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where ifnull(t2.companynr,1)>0;
1566
explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 or companynr is null;
1567
explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 or companynr < 0 or companynr > 0;
1568
explain select companynr,companyname from t4 left join t2 using (companynr) where ifnull(companynr,1)>0;
1554
#explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0;
1555
#explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 or t2.companynr < 0;
1556
#explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 and t4.companynr > 0;
1558
#explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0;
1559
#explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 or companynr < 0;
1560
#explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 and companynr > 0;
1561
## Following can't be optimized
1562
#explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 or t2.companynr is null;
1563
#explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 or t2.companynr < 0 or t4.companynr > 0;
1564
#explain select t2.companynr,companyname from t4 left join t2 using (companynr) where ifnull(t2.companynr,1)>0;
1566
#explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 or companynr is null;
1567
#explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 or companynr < 0 or companynr > 0;
1568
#explain select companynr,companyname from t4 left join t2 using (companynr) where ifnull(companynr,1)>0;
1571
1571
# Joins with forms.
1574
1574
select distinct t2.companynr,t4.companynr from t2,t4 where t2.companynr=t4.companynr+1;
1575
explain select distinct t2.companynr,t4.companynr from t2,t4 where t2.companynr=t4.companynr+1;
1575
#explain select distinct t2.companynr,t4.companynr from t2,t4 where t2.companynr=t4.companynr+1;
1578
1578
# Search using 'or' with the same referens group.
1611
1611
select companynr,count(*),sum(fld1) from t2 group by companynr;
1612
1612
select companynr,count(*) from t2 group by companynr order by companynr desc limit 5;
1613
1613
select count(*),min(fld4),max(fld4),sum(fld1),avg(fld1),std(fld1),variance(fld1) from t2 where companynr = 34 and fld4<>"";
1614
explain extended select count(*),min(fld4),max(fld4),sum(fld1),avg(fld1),std(fld1),variance(fld1) from t2 where companynr = 34 and fld4<>"";
1614
#explain extended select count(*),min(fld4),max(fld4),sum(fld1),avg(fld1),std(fld1),variance(fld1) from t2 where companynr = 34 and fld4<>"";
1615
1615
select companynr,count(*),min(fld4),max(fld4),sum(fld1),avg(fld1),std(fld1),variance(fld1) from t2 group by companynr limit 3;
1616
1616
select companynr,t2nr,count(price),sum(price),min(price),max(price),avg(price) from t3 where companynr = 37 group by companynr,t2nr limit 10;
1617
1617
select /*! SQL_SMALL_RESULT */ companynr,t2nr,count(price),sum(price),min(price),max(price),avg(price) from t3 where companynr = 37 group by companynr,t2nr limit 10;
1910
1909
create table t2 (c integer, d integer, index(c), index(d));
1911
1910
insert into t1 values (1,2), (2,2), (3,2), (4,2);
1912
1911
insert into t2 values (1,3), (2,3), (3,4), (4,4);
1913
explain select * from t1 left join t2 on a=c where d in (4);
1912
#explain select * from t1 left join t2 on a=c where d in (4);
1914
1913
select * from t1 left join t2 on a=c where d in (4);
1915
explain select * from t1 left join t2 on a=c where d = 4;
1914
#explain select * from t1 left join t2 on a=c where d = 4;
1916
1915
select * from t1 left join t2 on a=c where d = 4;
1917
1916
drop table t1, t2;
2223
2222
CREATE TABLE t3 (a int, b int, PRIMARY KEY (a,b), KEY a (a), KEY b (b));
2224
2223
insert into t3 values (1,1),(1,2);
2225
2224
# must have "range checked" for t2
2226
explain select straight_join DISTINCT t2.a,t2.b, t1.c from t1, t3, t2
2227
where (t1.c=t2.a or (t1.c=t3.a and t2.a=t3.b)) and t1.b=556476786 and
2228
t2.b like '%%' order by t2.b limit 0,1;
2225
#explain select straight_join DISTINCT t2.a,t2.b, t1.c from t1, t3, t2 where (t1.c=t2.a or (t1.c=t3.a and t2.a=t3.b)) and t1.b=556476786 and t2.b like '%%' order by t2.b limit 0,1;
2229
2226
DROP TABLE t1,t2,t3;
2268
2265
INSERT INTO t1 VALUES (0.3762),(0.3845),(0.6158),(0.7941);
2269
2266
INSERT INTO t2 VALUES (1.3762),(1.3845),(1.6158),(1.7941);
2271
explain select max(key1) from t1 where key1 <= 0.6158;
2272
explain select max(key2) from t2 where key2 <= 1.6158;
2273
explain select min(key1) from t1 where key1 >= 0.3762;
2274
explain select min(key2) from t2 where key2 >= 1.3762;
2275
explain select max(key1), min(key2) from t1, t2
2276
where key1 <= 0.6158 and key2 >= 1.3762;
2277
explain select max(key1) from t1 where key1 <= 0.6158 and rand() + 0.5 >= 0.5;
2278
explain select min(key1) from t1 where key1 >= 0.3762 and rand() + 0.5 >= 0.5;
2268
#explain select max(key1) from t1 where key1 <= 0.6158;
2269
#explain select max(key2) from t2 where key2 <= 1.6158;
2270
#explain select min(key1) from t1 where key1 >= 0.3762;
2271
#explain select min(key2) from t2 where key2 >= 1.3762;
2272
#explain select max(key1), min(key2) from t1, t2 where key1 <= 0.6158 and key2 >= 1.3762;
2273
#explain select max(key1) from t1 where key1 <= 0.6158 and rand() + 0.5 >= 0.5;
2274
#explain select min(key1) from t1 where key1 >= 0.3762 and rand() + 0.5 >= 0.5;
2280
2276
select max(key1) from t1 where key1 <= 0.6158;
2281
2277
select max(key2) from t2 where key2 <= 1.6158;