Commit f960f233 authored by Igor Babaev's avatar Igor Babaev

Merge.

parents adb9fd95 c44c9207
......@@ -334,12 +334,12 @@ update t0 set key2=1, key3=1, key4=1, key5=1,key6=1,key7=1 where key7 < 500;
--replace_column 9 #
--replace_result "4,4,4,4,4,4,4" X "4,4,4,4,4,4" X "i6,i7" "i6,i7?" "i6" "i6,i7?"
explain select max(A.key1 + B.key1 + A.key2 + B.key2 + A.key3 + B.key3 + A.key4 + B.key4 + A.key5 + B.key5)
from t0 as A, t0 as B
from t0 as A straight_join t0 as B
where (A.key1 = 1 and A.key2 = 1 and A.key3 = 1 and A.key4=1 and A.key5=1 and A.key6=1 and A.key7 = 1 or A.key8=1)
and (B.key1 = 1 and B.key2 = 1 and B.key3 = 1 and B.key4=1 and B.key5=1 and B.key6=1 and B.key7 = 1 or B.key8=1);
select max(A.key1 + B.key1 + A.key2 + B.key2 + A.key3 + B.key3 + A.key4 + B.key4 + A.key5 + B.key5)
from t0 as A, t0 as B
from t0 as A straight_join t0 as B
where (A.key1 = 1 and A.key2 = 1 and A.key3 = 1 and A.key4=1 and A.key5=1 and A.key6=1 and A.key7 = 1 or A.key8=1)
and (B.key1 = 1 and B.key2 = 1 and B.key3 = 1 and B.key4=1 and B.key5=1 and B.key6=1 and B.key7 = 1 or B.key8=1);
......
This source diff could not be displayed because it is too large. You can view the blob instead.
CREATE TABLE Country (
Code char(3) NOT NULL default '',
Name char(52) NOT NULL default '',
SurfaceArea float(10,2) NOT NULL default '0.00',
Population int(11) NOT NULL default '0',
Capital int(11) default NULL,
PRIMARY KEY (Code),
UNIQUE INDEX (Name)
);
CREATE TABLE City (
ID int(11) NOT NULL auto_increment,
Name char(35) NOT NULL default '',
Country char(3) NOT NULL default '',
Population int(11) NOT NULL default '0',
PRIMARY KEY (ID),
INDEX (Population),
INDEX (Country)
);
CREATE TABLE CountryLanguage (
Country char(3) NOT NULL default '',
Language char(30) NOT NULL default '',
Percentage float(3,1) NOT NULL default '0.0',
PRIMARY KEY (Country, Language),
INDEX (Percentage)
);
......@@ -115,11 +115,11 @@ id select_type table type possible_keys key key_len ref rows Extra
explain select * from t0 where
(key1 < 3 or key2 < 3) and (key3 < 100);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t0 range i1,i2,i3 i3 4 NULL 95 Using where
1 SIMPLE t0 index_merge i1,i2,i3 i1,i2 4,4 NULL 6 Using sort_union(i1,i2); Using where
explain select * from t0 where
(key1 < 3 or key2 < 3) and (key3 < 1000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t0 ALL i1,i2,i3 NULL NULL NULL 1024 Using where
1 SIMPLE t0 index_merge i1,i2,i3 i1,i2 4,4 NULL 6 Using sort_union(i1,i2); Using where
explain select * from t0 where
((key1 < 4 or key2 < 4) and (key2 <5 or key3 < 4))
or
......@@ -259,7 +259,7 @@ explain
select * from t0,t1 where (t0.key1=t1.key1) and
(t0.key1=3 or t0.key2=4) and t1.key1<200;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t0 ALL i1,i2 NULL NULL NULL 1024 Using where
1 SIMPLE t0 index_merge i1,i2 i1,i2 4,4 NULL 2 Using union(i1,i2); Using where
1 SIMPLE t1 ref i1 i1 4 test.t0.key1 1
explain
select * from t0,t1 where (t0.key1=t1.key1) and
......@@ -374,14 +374,14 @@ max(A.key1 + B.key1 + A.key2 + B.key2 + A.key3 + B.key3 + A.key4 + B.key4 + A.ke
alter table t0 add filler1 char(200), add filler2 char(200), add filler3 char(200);
update t0 set key2=1, key3=1, key4=1, key5=1,key6=1,key7=1 where key7 < 500;
explain select max(A.key1 + B.key1 + A.key2 + B.key2 + A.key3 + B.key3 + A.key4 + B.key4 + A.key5 + B.key5)
from t0 as A, t0 as B
from t0 as A straight_join t0 as B
where (A.key1 = 1 and A.key2 = 1 and A.key3 = 1 and A.key4=1 and A.key5=1 and A.key6=1 and A.key7 = 1 or A.key8=1)
and (B.key1 = 1 and B.key2 = 1 and B.key3 = 1 and B.key4=1 and B.key5=1 and B.key6=1 and B.key7 = 1 or B.key8=1);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE A index_merge i1,i2,i3,i4,i5,i6,i7?,i8 i2,i3,i4,i5,i6,i7?,i8 X NULL # Using union(intersect(i2,i3,i4,i5,i6,i7?),i8); Using where
1 SIMPLE B index_merge i1,i2,i3,i4,i5,i6,i7?,i8 i2,i3,i4,i5,i6,i7?,i8 X NULL # Using union(intersect(i2,i3,i4,i5,i6,i7?),i8); Using where; Using join buffer
select max(A.key1 + B.key1 + A.key2 + B.key2 + A.key3 + B.key3 + A.key4 + B.key4 + A.key5 + B.key5)
from t0 as A, t0 as B
from t0 as A straight_join t0 as B
where (A.key1 = 1 and A.key2 = 1 and A.key3 = 1 and A.key4=1 and A.key5=1 and A.key6=1 and A.key7 = 1 or A.key8=1)
and (B.key1 = 1 and B.key2 = 1 and B.key3 = 1 and B.key4=1 and B.key5=1 and B.key6=1 and B.key7 = 1 or B.key8=1);
max(A.key1 + B.key1 + A.key2 + B.key2 + A.key3 + B.key3 + A.key4 + B.key4 + A.key5 + B.key5)
......
DROP TABLE IF EXISTS t1,t2,t3,t4;
DROP DATABASE IF EXISTS world;
set names utf8;
CREATE DATABASE world;
use world;
CREATE TABLE Country (
Code char(3) NOT NULL default '',
Name char(52) NOT NULL default '',
SurfaceArea float(10,2) NOT NULL default '0.00',
Population int(11) NOT NULL default '0',
Capital int(11) default NULL,
PRIMARY KEY (Code),
UNIQUE INDEX (Name)
);
CREATE TABLE City (
ID int(11) NOT NULL auto_increment,
Name char(35) NOT NULL default '',
Country char(3) NOT NULL default '',
Population int(11) NOT NULL default '0',
PRIMARY KEY (ID),
INDEX (Population),
INDEX (Country)
);
CREATE TABLE CountryLanguage (
Country char(3) NOT NULL default '',
Language char(30) NOT NULL default '',
Percentage float(3,1) NOT NULL default '0.0',
PRIMARY KEY (Country, Language),
INDEX (Percentage)
);
SELECT COUNT(*) FROM Country;
COUNT(*)
239
SELECT COUNT(*) FROM City;
COUNT(*)
4079
SELECT COUNT(*) FROM CountryLanguage;
COUNT(*)
984
CREATE INDEX Name ON City(Name);
EXPLAIN
SELECT * FROM City
WHERE (Population >= 100000 OR Name LIKE 'P%' OR Population < 100000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City ALL Population,Name NULL NULL NULL 4079 Using where
EXPLAIN
SELECT * FROM City
WHERE (Population >= 100000 OR Name LIKE 'P%') AND Country='CAN' OR
(Population < 100000 OR Name Like 'T%') AND Country='ARG';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population,Country,Name Country 3 NULL 104 Using where
EXPLAIN
SELECT * FROM City
WHERE Population < 200000 AND Name LIKE 'P%' AND
(Population > 300000 OR Name LIKE 'T%') AND
(Population < 100000 OR Name LIKE 'Pa%');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population,Name Name 35 NULL 135 Using where
EXPLAIN
SELECT * FROM City
WHERE Population > 100000 AND Name LIKE 'Aba%' OR
Country IN ('CAN', 'ARG') AND ID < 3800 OR
Country < 'U' AND Name LIKE 'Zhu%' OR
ID BETWEEN 3800 AND 3810;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge PRIMARY,Population,Country,Name Name,Country,PRIMARY 35,3,4 NULL 132 Using sort_union(Name,Country,PRIMARY); Using where
EXPLAIN
SELECT * FROM City
WHERE (Population > 101000 AND Population < 115000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population Population 4 NULL 459 Using where
EXPLAIN
SELECT * FROM City
WHERE (Population > 101000 AND Population < 103000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population Population 4 NULL 81 Using where
EXPLAIN
SELECT * FROM City
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'));
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge Country,Name Name,Country 35,3 NULL 172 Using sort_union(Name,Country); Using where
EXPLAIN
SELECT * FROM City
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'))
AND (Population > 101000 AND Population < 115000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge Population,Country,Name Name,Country 35,3 NULL 172 Using sort_union(Name,Country); Using where
EXPLAIN
SELECT * FROM City
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'))
AND (Population > 101000 AND Population < 103000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population,Country,Name Population 4 NULL 81 Using where
SELECT * FROM City USE INDEX ()
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'))
AND (Population > 101000 AND Population < 115000);
ID Name Country Population
403 Catanduva BRA 107761
412 Cachoeirinha BRA 103240
636 Bilbays EGY 113608
637 Mit Ghamr EGY 101801
701 Tarragona ESP 113016
702 Lleida (Lérida) ESP 112207
703 Jaén ESP 109247
704 Ourense (Orense) ESP 109120
705 Mataró ESP 104095
706 Algeciras ESP 103106
707 Marbella ESP 101144
759 Gonder ETH 112249
869 Cabuyao PHL 106630
870 Calapan PHL 105910
873 Cauayan PHL 103952
1844 Cape Breton CAN 114733
1847 Cambridge CAN 109186
2908 Cajamarca PER 108009
3003 Caen FRA 113987
3411 Ceyhan TUR 102412
3571 Calabozo VEN 107146
3786 Cam Ranh VNM 114041
3792 Tartu EST 101246
4002 Carrollton USA 109576
4027 Cape Coral USA 102286
4032 Cambridge USA 101355
SELECT * FROM City
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'))
AND (Population > 101000 AND Population < 115000);
ID Name Country Population
403 Catanduva BRA 107761
412 Cachoeirinha BRA 103240
636 Bilbays EGY 113608
637 Mit Ghamr EGY 101801
701 Tarragona ESP 113016
702 Lleida (Lérida) ESP 112207
703 Jaén ESP 109247
704 Ourense (Orense) ESP 109120
705 Mataró ESP 104095
706 Algeciras ESP 103106
707 Marbella ESP 101144
759 Gonder ETH 112249
869 Cabuyao PHL 106630
870 Calapan PHL 105910
873 Cauayan PHL 103952
1844 Cape Breton CAN 114733
1847 Cambridge CAN 109186
2908 Cajamarca PER 108009
3003 Caen FRA 113987
3411 Ceyhan TUR 102412
3571 Calabozo VEN 107146
3786 Cam Ranh VNM 114041
3792 Tartu EST 101246
4002 Carrollton USA 109576
4027 Cape Coral USA 102286
4032 Cambridge USA 101355
SELECT * FROM City USE INDEX ()
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'))
AND (Population > 101000 AND Population < 103000);
ID Name Country Population
637 Mit Ghamr EGY 101801
707 Marbella ESP 101144
3411 Ceyhan TUR 102412
3792 Tartu EST 101246
4027 Cape Coral USA 102286
4032 Cambridge USA 101355
SELECT * FROM City
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'))
AND (Population > 101000 AND Population < 103000);
ID Name Country Population
707 Marbella ESP 101144
3792 Tartu EST 101246
4032 Cambridge USA 101355
637 Mit Ghamr EGY 101801
4027 Cape Coral USA 102286
3411 Ceyhan TUR 102412
EXPLAIN
SELECT * FROM City WHERE (Name < 'Ac');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 13 Using where
EXPLAIN
SELECT * FROM City WHERE (Name < 'Bb');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 208 Using where
EXPLAIN
SELECT * FROM City WHERE (Country > 'A' AND Country < 'B');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Country Country 3 NULL 104 Using where
EXPLAIN
SELECT * FROM City WHERE (Name BETWEEN 'P' AND 'Pb');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 39 Using where
EXPLAIN
SELECT * FROM City WHERE (Name BETWEEN 'P' AND 'S');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 221 Using where
EXPLAIN
SELECT * FROM City WHERE (Population > 101000 AND Population < 110000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population Population 4 NULL 328 Using where
EXPLAIN
SELECT * FROM City WHERE (Population > 103000 AND Population < 104000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population Population 4 NULL 37 Using where
EXPLAIN
SELECT * FROM City
WHERE (Name < 'Ac' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'Pb' AND (Population > 101000 AND Population < 110000));
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population,Country,Name Name 35 NULL 52 Using where
EXPLAIN
SELECT * FROM City
WHERE (Name < 'Ac' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'S' AND (Population > 103000 AND Population < 104000));
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge Population,Country,Name Name,Population 35,4 NULL 50 Using sort_union(Name,Population); Using where
EXPLAIN
SELECT * FROM City
WHERE (Name < 'Bb' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'Pb' AND (Population > 101000 AND Population < 110000));
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge Population,Country,Name Country,Name 3,35 NULL 143 Using sort_union(Country,Name); Using where
EXPLAIN
SELECT * FROM City
WHERE (Name < 'Bb' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'S' AND (Population > 103000 AND Population < 104000));
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge Population,Country,Name Country,Population 3,4 NULL 141 Using sort_union(Country,Population); Using where
SELECT * FROM City USE INDEX ()
WHERE (Name < 'Ac' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'Pb' AND (Population > 101000 AND Population < 110000));
ID Name Country Population
65 Abu Dhabi ARE 398695
168 Pabna BGD 103277
189 Parakou BEN 103577
750 Paarl ZAF 105768
2865 Pak Pattan PAK 107800
SELECT * FROM City
WHERE (Name < 'Ac' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'Pb' AND (Population > 101000 AND Population < 110000));
ID Name Country Population
65 Abu Dhabi ARE 398695
750 Paarl ZAF 105768
168 Pabna BGD 103277
2865 Pak Pattan PAK 107800
189 Parakou BEN 103577
SELECT * FROM City USE INDEX ()
WHERE (Name < 'Ac' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'S' AND (Population > 103000 AND Population < 104000));
ID Name Country Population
65 Abu Dhabi ARE 398695
168 Pabna BGD 103277
189 Parakou BEN 103577
1003 Pemalang IDN 103500
2663 Río Bravo MEX 103901
SELECT * FROM City
WHERE (Name < 'Ac' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'S' AND (Population > 103000 AND Population < 104000));
ID Name Country Population
65 Abu Dhabi ARE 398695
168 Pabna BGD 103277
189 Parakou BEN 103577
1003 Pemalang IDN 103500
2663 Río Bravo MEX 103901
SELECT * FROM City USE INDEX ()
WHERE (Name < 'Bb' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'Pb' AND (Population > 101000 AND Population < 110000));
ID Name Country Population
55 Andorra la Vella AND 21189
65 Abu Dhabi ARE 398695
67 al-Ayn ARE 225970
68 Ajman ARE 114395
75 Almirante Brown ARG 538918
85 Avellaneda ARG 353046
96 Bahía Blanca ARG 239810
134 Adelaide AUS 978100
144 Baku AZE 1787800
168 Pabna BGD 103277
189 Parakou BEN 103577
750 Paarl ZAF 105768
2865 Pak Pattan PAK 107800
SELECT * FROM City
WHERE (Name < 'Bb' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'Pb' AND (Population > 101000 AND Population < 110000));
ID Name Country Population
55 Andorra la Vella AND 21189
65 Abu Dhabi ARE 398695
67 al-Ayn ARE 225970
68 Ajman ARE 114395
75 Almirante Brown ARG 538918
85 Avellaneda ARG 353046
96 Bahía Blanca ARG 239810
134 Adelaide AUS 978100
144 Baku AZE 1787800
168 Pabna BGD 103277
189 Parakou BEN 103577
750 Paarl ZAF 105768
2865 Pak Pattan PAK 107800
SELECT * FROM City USE INDEX ()
WHERE (Name < 'Bb' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'S' AND (Population > 103000 AND Population < 104000));
ID Name Country Population
55 Andorra la Vella AND 21189
65 Abu Dhabi ARE 398695
67 al-Ayn ARE 225970
68 Ajman ARE 114395
75 Almirante Brown ARG 538918
85 Avellaneda ARG 353046
96 Bahía Blanca ARG 239810
134 Adelaide AUS 978100
144 Baku AZE 1787800
168 Pabna BGD 103277
189 Parakou BEN 103577
1003 Pemalang IDN 103500
2663 Río Bravo MEX 103901
SELECT * FROM City
WHERE (Name < 'Bb' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'S' AND (Population > 103000 AND Population < 104000));
ID Name Country Population
55 Andorra la Vella AND 21189
65 Abu Dhabi ARE 398695
67 al-Ayn ARE 225970
68 Ajman ARE 114395
75 Almirante Brown ARG 538918
85 Avellaneda ARG 353046
96 Bahía Blanca ARG 239810
134 Adelaide AUS 978100
144 Baku AZE 1787800
168 Pabna BGD 103277
189 Parakou BEN 103577
1003 Pemalang IDN 103500
2663 Río Bravo MEX 103901
EXPLAIN
SELECT * FROM City WHERE (ID < 50) OR (ID BETWEEN 100 AND 110);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 61 Using where
EXPLAIN
SELECT * FROM City WHERE (ID < 200) OR (ID BETWEEN 300 AND 600);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 501 Using where
EXPLAIN
SELECT * FROM City WHERE (ID < 600) OR (ID BETWEEN 900 AND 1800);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City ALL PRIMARY NULL NULL NULL 4079 Using where
EXPLAIN
SELECT * FROM City WHERE Country > 'A' AND Country < 'ARG';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Country Country 3 NULL 19 Using where
EXPLAIN
SELECT * FROM City WHERE Name LIKE 'H%' OR Name LIKE 'P%' ;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 222 Using where
EXPLAIN
SELECT * FROM City WHERE Name LIKE 'Ha%' OR Name LIKE 'Pa%' ;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 72 Using where
EXPLAIN
SELECT * FROM City
WHERE ((ID < 50) AND (Name LIKE 'H%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 100 AND 110) AND
(Name LIKE 'P%' OR (Population > 103000 AND Population < 104000)));
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY,Population,Country,Name PRIMARY 4 NULL 61 Using where
EXPLAIN
SELECT * FROM City
WHERE ((ID < 800) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 900 AND 1800) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge PRIMARY,Population,Country,Name Name,Country,Population 35,3,4 NULL 128 Using sort_union(Name,Country,Population); Using where
EXPLAIN
SELECT * FROM City
WHERE ((ID < 600) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 300 AND 600) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge PRIMARY,Population,Country,Name Name,Country,Population 35,3,4 NULL 128 Using sort_union(Name,Country,Population); Using where
SELECT * FROM City USE INDEX ()
WHERE ((ID < 50) AND (Name LIKE 'H%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 100 AND 110) AND
(Name LIKE 'P%' OR (Population > 103000 AND Population < 104000)));
ID Name Country Population
1 Kabul AFG 1780000
2 Qandahar AFG 237500
3 Herat AFG 186800
4 Mazar-e-Sharif AFG 127800
7 Haag NLD 440900
16 Haarlem NLD 148772
25 Haarlemmermeer NLD 110722
31 Heerlen NLD 95052
33 Willemstad ANT 2345
34 Tirana ALB 270000
100 Paraná ARG 207041
102 Posadas ARG 201273
SELECT * FROM City
WHERE ((ID < 50) AND (Name LIKE 'H%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 100 AND 110) AND
(Name LIKE 'P%' OR (Population > 103000 AND Population < 104000)));
ID Name Country Population
1 Kabul AFG 1780000
2 Qandahar AFG 237500
3 Herat AFG 186800
4 Mazar-e-Sharif AFG 127800
7 Haag NLD 440900
16 Haarlem NLD 148772
25 Haarlemmermeer NLD 110722
31 Heerlen NLD 95052
33 Willemstad ANT 2345
34 Tirana ALB 270000
100 Paraná ARG 207041
102 Posadas ARG 201273
SELECT * FROM City USE INDEX()
WHERE ((ID < 800) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 900 AND 1800) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
ID Name Country Population
1 Kabul AFG 1780000
2 Qandahar AFG 237500
3 Herat AFG 186800
4 Mazar-e-Sharif AFG 127800
7 Haag NLD 440900
16 Haarlem NLD 148772
25 Haarlemmermeer NLD 110722
33 Willemstad ANT 2345
34 Tirana ALB 270000
55 Andorra la Vella AND 21189
56 Luanda AGO 2022000
57 Huambo AGO 163100
58 Lobito AGO 130000
59 Benguela AGO 128300
60 Namibe AGO 118200
61 South Hill AIA 961
62 The Valley AIA 595
64 Dubai ARE 669181
65 Abu Dhabi ARE 398695
66 Sharja ARE 320095
67 al-Ayn ARE 225970
68 Ajman ARE 114395
129 Oranjestad ABW 29034
191 Hamilton BMU 1200
528 Hartlepool GBR 92000
529 Halifax GBR 91069
914 Sekondi-Takoradi GHA 103653
943 Palembang IDN 1222764
950 Padang IDN 534474
983 Palu IDN 142800
984 Pasuruan IDN 134019
991 Pangkal Pinang IDN 124000
1003 Pemalang IDN 103500
1004 Klaten IDN 103300
1007 Palangka Raya IDN 99693
1020 Padang Sidempuan IDN 91200
1045 Patna IND 917243
1114 Panihati IND 275990
1129 Patiala IND 238368
1142 Panipat IND 215218
1159 Parbhani IND 190255
1231 Pali IND 136842
1263 Pathankot IND 123930
1265 Palghat (Palakkad) IND 123289
1293 Pallavaram IND 111866
1319 Tellicherry (Thalassery) IND 103579
1339 Palayankottai IND 97662
1345 Patan IND 96109
1436 Marv Dasht IRN 103579
1468 Palermo ITA 683794
1478 Padova ITA 211391
1484 Parma ITA 168717
1530 Kingston JAM 103962
1747 Toda JPN 103969
1748 Tajimi JPN 103171
1785 Ibb YEM 103300
SELECT * FROM City
WHERE ((ID < 800) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 900 AND 1800) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
ID Name Country Population
1 Kabul AFG 1780000
2 Qandahar AFG 237500
3 Herat AFG 186800
4 Mazar-e-Sharif AFG 127800
7 Haag NLD 440900
16 Haarlem NLD 148772
25 Haarlemmermeer NLD 110722
33 Willemstad ANT 2345
34 Tirana ALB 270000
55 Andorra la Vella AND 21189
56 Luanda AGO 2022000
57 Huambo AGO 163100
58 Lobito AGO 130000
59 Benguela AGO 128300
60 Namibe AGO 118200
61 South Hill AIA 961
62 The Valley AIA 595
64 Dubai ARE 669181
65 Abu Dhabi ARE 398695
66 Sharja ARE 320095
67 al-Ayn ARE 225970
68 Ajman ARE 114395
129 Oranjestad ABW 29034
191 Hamilton BMU 1200
528 Hartlepool GBR 92000
529 Halifax GBR 91069
914 Sekondi-Takoradi GHA 103653
943 Palembang IDN 1222764
950 Padang IDN 534474
983 Palu IDN 142800
984 Pasuruan IDN 134019
991 Pangkal Pinang IDN 124000
1003 Pemalang IDN 103500
1004 Klaten IDN 103300
1007 Palangka Raya IDN 99693
1020 Padang Sidempuan IDN 91200
1045 Patna IND 917243
1114 Panihati IND 275990
1129 Patiala IND 238368
1142 Panipat IND 215218
1159 Parbhani IND 190255
1231 Pali IND 136842
1263 Pathankot IND 123930
1265 Palghat (Palakkad) IND 123289
1293 Pallavaram IND 111866
1319 Tellicherry (Thalassery) IND 103579
1339 Palayankottai IND 97662
1345 Patan IND 96109
1436 Marv Dasht IRN 103579
1468 Palermo ITA 683794
1478 Padova ITA 211391
1484 Parma ITA 168717
1530 Kingston JAM 103962
1747 Toda JPN 103969
1748 Tajimi JPN 103171
1785 Ibb YEM 103300
SELECT * FROM City USE INDEX ()
WHERE ((ID < 200) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 300 AND 600) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
ID Name Country Population
1 Kabul AFG 1780000
2 Qandahar AFG 237500
3 Herat AFG 186800
4 Mazar-e-Sharif AFG 127800
7 Haag NLD 440900
16 Haarlem NLD 148772
25 Haarlemmermeer NLD 110722
33 Willemstad ANT 2345
34 Tirana ALB 270000
55 Andorra la Vella AND 21189
56 Luanda AGO 2022000
57 Huambo AGO 163100
58 Lobito AGO 130000
59 Benguela AGO 128300
60 Namibe AGO 118200
61 South Hill AIA 961
62 The Valley AIA 595
64 Dubai ARE 669181
65 Abu Dhabi ARE 398695
66 Sharja ARE 320095
67 al-Ayn ARE 225970
68 Ajman ARE 114395
129 Oranjestad ABW 29034
191 Hamilton BMU 1200
339 Passo Fundo BRA 166343
364 Parnaíba BRA 129756
372 Paranaguá BRA 126076
379 Palmas BRA 121919
386 Patos de Minas BRA 119262
411 Guaratinguetá BRA 103433
412 Cachoeirinha BRA 103240
413 Codó BRA 103153
424 Passos BRA 98570
430 Paulo Afonso BRA 97291
435 Parnamirim BRA 96210
448 Patos BRA 90519
451 Palhoça BRA 89465
517 Oldham GBR 103931
SELECT * FROM City
WHERE ((ID < 200) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 300 AND 600) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
ID Name Country Population
1 Kabul AFG 1780000
2 Qandahar AFG 237500
3 Herat AFG 186800
4 Mazar-e-Sharif AFG 127800
7 Haag NLD 440900
16 Haarlem NLD 148772
25 Haarlemmermeer NLD 110722
33 Willemstad ANT 2345
34 Tirana ALB 270000
55 Andorra la Vella AND 21189
56 Luanda AGO 2022000
57 Huambo AGO 163100
58 Lobito AGO 130000
59 Benguela AGO 128300
60 Namibe AGO 118200
61 South Hill AIA 961
62 The Valley AIA 595
64 Dubai ARE 669181
65 Abu Dhabi ARE 398695
66 Sharja ARE 320095
67 al-Ayn ARE 225970
68 Ajman ARE 114395
129 Oranjestad ABW 29034
191 Hamilton BMU 1200
339 Passo Fundo BRA 166343
364 Parnaíba BRA 129756
372 Paranaguá BRA 126076
379 Palmas BRA 121919
386 Patos de Minas BRA 119262
411 Guaratinguetá BRA 103433
412 Cachoeirinha BRA 103240
413 Codó BRA 103153
424 Passos BRA 98570
430 Paulo Afonso BRA 97291
435 Parnamirim BRA 96210
448 Patos BRA 90519
451 Palhoça BRA 89465
517 Oldham GBR 103931
EXPLAIN
SELECT * FROM City WHERE Population > 101000 AND Population < 102000;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population Population 4 NULL 39 Using where
EXPLAIN
SELECT * FROM City WHERE Population > 101000 AND Population < 110000;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population Population 4 NULL 328 Using where
EXPLAIN
SELECT * FROM City WHERE Country < 'C';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Country Country 3 NULL 436 Using where
EXPLAIN
SELECT * FROM City WHERE Country < 'AGO';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Country Country 3 NULL 6 Using where
EXPLAIN
SELECT * FROM City WHERE Name BETWEEN 'P' AND 'S';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 221 Using where
EXPLAIN
SELECT * FROM City WHERE Name BETWEEN 'P' AND 'Pb';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 39 Using where
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 3400 AND 3800;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 401 Using where
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 3790 AND 3800;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 11 Using where
EXPLAIN
SELECT * FROM City WHERE Name LIKE 'P%';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 135 Using where
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 102000) AND
(Country < 'C' OR Name BETWEEN 'P' AND 'S')) OR
((ID BETWEEN 3400 AND 3800) AND
(Country < 'AGO' OR Name LIKE 'Pa%'));
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge PRIMARY,Population,Country,Name Country,Name,Population 3,35,4 NULL 84 Using sort_union(Country,Name,Population); Using where
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 110000) AND
(Country < 'AGO' OR Name BETWEEN 'P' AND 'Pb')) OR
((ID BETWEEN 3790 AND 3800) AND
(Country < 'C' OR Name LIKE 'P%'));
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge PRIMARY,Population,Country,Name Country,Name,PRIMARY 3,35,4 NULL 56 Using sort_union(Country,Name,PRIMARY); Using where
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 102000) AND
(Country < 'C' OR Name BETWEEN 'P' AND 'S')) OR
((ID BETWEEN 3400 AND 3800) AND
(Country < 'AGO' OR Name LIKE 'Pa%'));
ID Name Country Population
169 Naogaon BGD 101266
205 Francistown BWA 101805
417 Itaituba BRA 101320
418 Araras BRA 101046
751 Potchefstroom ZAF 101817
2909 Puno PER 101578
3463 Pavlograd UKR 127000
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 102000) AND
(Country < 'C' OR Name BETWEEN 'P' AND 'S')) OR
((ID BETWEEN 3400 AND 3800) AND
(Country < 'AGO' OR Name LIKE 'Pa%'));
ID Name Country Population
169 Naogaon BGD 101266
205 Francistown BWA 101805
417 Itaituba BRA 101320
418 Araras BRA 101046
751 Potchefstroom ZAF 101817
2909 Puno PER 101578
3463 Pavlograd UKR 127000
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 110000) AND
(Country < 'AGO' OR Name BETWEEN 'P' AND 'Pb')) OR
((ID BETWEEN 3790 AND 3800) AND
(Country < 'C' OR Name LIKE 'P%'));
ID Name Country Population
168 Pabna BGD 103277
189 Parakou BEN 103577
750 Paarl ZAF 105768
2865 Pak Pattan PAK 107800
3797 Philadelphia USA 1517550
3798 Phoenix USA 1321045
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 110000) AND
(Country < 'AGO' OR Name BETWEEN 'P' AND 'Pb')) OR
((ID BETWEEN 3790 AND 3800) AND
(Country < 'C' OR Name LIKE 'P%'));
ID Name Country Population
168 Pabna BGD 103277
189 Parakou BEN 103577
750 Paarl ZAF 105768
2865 Pak Pattan PAK 107800
3797 Philadelphia USA 1517550
3798 Phoenix USA 1321045
CREATE INDEX CountryPopulation ON City(Country,Population);
EXPLAIN
SELECT * FROM City WHERE Name LIKE 'Pas%';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 5 Using where
EXPLAIN
SELECT * FROM City WHERE Name LIKE 'P%';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 135 Using where
EXPLAIN
SELECT * FROM City WHERE (Population > 101000 AND Population < 103000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population Population 4 NULL 81 Using where
EXPLAIN
SELECT * FROM City WHERE Country='USA';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City ref Country,CountryPopulation Country 3 const 267 Using where
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 103000) OR Name LIKE 'Pas%')
AND Country='USA';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge Population,Country,Name,CountryPopulation CountryPopulation,Name 7,35 NULL 15 Using sort_union(CountryPopulation,Name); Using where
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 103000) OR Name LIKE 'P%')
AND Country='USA';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City ref Population,Country,Name,CountryPopulation Country 3 const 267 Using where
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 103000) OR Name LIKE 'Pas%')
AND Country='USA';
ID Name Country Population
3943 Pasadena USA 141674
3953 Pasadena USA 133936
4023 Gary USA 102746
4024 Berkeley USA 102743
4025 Santa Clara USA 102361
4026 Green Bay USA 102313
4027 Cape Coral USA 102286
4028 Arvada USA 102153
4029 Pueblo USA 102121
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 103000) OR Name LIKE 'Pas%')
AND Country='USA';
ID Name Country Population
3943 Pasadena USA 141674
3953 Pasadena USA 133936
4023 Gary USA 102746
4024 Berkeley USA 102743
4025 Santa Clara USA 102361
4026 Green Bay USA 102313
4027 Cape Coral USA 102286
4028 Arvada USA 102153
4029 Pueblo USA 102121
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 103000) OR Name LIKE 'P%')
AND Country='USA';
ID Name Country Population
3797 Philadelphia USA 1517550
3798 Phoenix USA 1321045
3820 Portland USA 529121
3844 Pittsburgh USA 334563
3870 Plano USA 222030
3912 Providence USA 173618
3930 Pomona USA 149473
3932 Paterson USA 149222
3943 Pasadena USA 141674
3951 Pembroke Pines USA 137427
3953 Pasadena USA 133936
3967 Paradise USA 124682
3986 Palmdale USA 116670
3996 Peoria USA 112936
4007 Peoria USA 108364
4016 Provo USA 105166
4023 Gary USA 102746
4024 Berkeley USA 102743
4025 Santa Clara USA 102361
4026 Green Bay USA 102313
4027 Cape Coral USA 102286
4028 Arvada USA 102153
4029 Pueblo USA 102121
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
4035 Portsmouth USA 100565
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 103000) OR Name LIKE 'P%')
AND Country='USA';
ID Name Country Population
3797 Philadelphia USA 1517550
3798 Phoenix USA 1321045
3820 Portland USA 529121
3844 Pittsburgh USA 334563
3870 Plano USA 222030
3912 Providence USA 173618
3930 Pomona USA 149473
3932 Paterson USA 149222
3943 Pasadena USA 141674
3951 Pembroke Pines USA 137427
3953 Pasadena USA 133936
3967 Paradise USA 124682
3986 Palmdale USA 116670
3996 Peoria USA 112936
4007 Peoria USA 108364
4016 Provo USA 105166
4023 Gary USA 102746
4024 Berkeley USA 102743
4025 Santa Clara USA 102361
4026 Green Bay USA 102313
4027 Cape Coral USA 102286
4028 Arvada USA 102153
4029 Pueblo USA 102121
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
4035 Portsmouth USA 100565
CREATE INDEX CountryName ON City(Country,Name);
EXPLAIN
SELECT * FROM City WHERE Country='USA';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City ref Country,CountryPopulation,CountryName Country 3 const 267 Using where
EXPLAIN
SELECT * FROM City WHERE Country='BRA';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City ref Country,CountryPopulation,CountryName CountryName 3 const 221 Using where
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 3790 AND 3800;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 11 Using where
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 4025 AND 4035;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 11 Using where
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 4028 AND 4032;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 5 Using where
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 3500 AND 3800;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 301 Using where
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 4000 AND 4300;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 80 Using where
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 250 and 260 ;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 11 Using where
EXPLAIN
SELECT * FROM City WHERE (Population > 101000 AND Population < 102000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population Population 4 NULL 39 Using where
EXPLAIN
SELECT * FROM City WHERE (Population > 101000 AND Population < 103000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population Population 4 NULL 81 Using where
EXPLAIN
SELECT * FROM City WHERE Name LIKE 'Pa%';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 41 Using where
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4025 AND 4035);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge PRIMARY,Population,Country,Name,CountryPopulation,CountryName CountryPopulation,PRIMARY 7,4 NULL 14 Using sort_union(CountryPopulation,PRIMARY); Using where
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 103000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4028 AND 4032);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge PRIMARY,Population,Country,Name,CountryPopulation,CountryName CountryName,PRIMARY 38,4 NULL 11 Using sort_union(CountryName,PRIMARY); Using where
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 110000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name BETWEEN 'P' AND 'T' OR ID BETWEEN 4000 AND 4300);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City ref PRIMARY,Population,Country,Name,CountryPopulation,CountryName Country 3 const 267 Using where
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4025 AND 4035);
ID Name Country Population
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4025 AND 4035);
ID Name Country Population
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4028 AND 4032);
ID Name Country Population
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4028 AND 4032);
ID Name Country Population
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4025 AND 4035);
ID Name Country Population
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4025 AND 4035);
ID Name Country Population
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 and Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
OR (Name LIKE 'Pa%' OR ID BETWEEN 250 AND 260) AND Country='BRA';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge PRIMARY,Population,Country,Name,CountryPopulation,CountryName CountryPopulation,PRIMARY,CountryName 7,4,38 NULL 35 Using sort_union(CountryPopulation,PRIMARY,CountryName); Using where
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 and Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
OR (Name LIKE 'Pa%' OR ID BETWEEN 250 AND 260) AND Country='BRA';
ID Name Country Population
250 Mauá BRA 375055
251 Carapicuíba BRA 357552
252 Olinda BRA 354732
253 Campina Grande BRA 352497
254 São José do Rio Preto BRA 351944
255 Caxias do Sul BRA 349581
256 Moji das Cruzes BRA 339194
257 Diadema BRA 335078
258 Aparecida de Goiânia BRA 324662
259 Piracicaba BRA 319104
260 Cariacica BRA 319033
285 Paulista BRA 248473
339 Passo Fundo BRA 166343
364 Parnaíba BRA 129756
372 Paranaguá BRA 126076
379 Palmas BRA 121919
386 Patos de Minas BRA 119262
424 Passos BRA 98570
430 Paulo Afonso BRA 97291
435 Parnamirim BRA 96210
448 Patos BRA 90519
451 Palhoça BRA 89465
3793 New York USA 8008278
3794 Los Angeles USA 3694820
3795 Chicago USA 2896016
3796 Houston USA 1953631
3797 Philadelphia USA 1517550
3798 Phoenix USA 1321045
3799 San Diego USA 1223400
3800 Dallas USA 1188580
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
SELECT * FROM City
WHERE ((Population > 101000 and Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
OR (Name LIKE 'Pa%' OR ID BETWEEN 250 AND 260) AND Country='BRA';
ID Name Country Population
250 Mauá BRA 375055
251 Carapicuíba BRA 357552
252 Olinda BRA 354732
253 Campina Grande BRA 352497
254 São José do Rio Preto BRA 351944
255 Caxias do Sul BRA 349581
256 Moji das Cruzes BRA 339194
257 Diadema BRA 335078
258 Aparecida de Goiânia BRA 324662
259 Piracicaba BRA 319104
260 Cariacica BRA 319033
285 Paulista BRA 248473
339 Passo Fundo BRA 166343
364 Parnaíba BRA 129756
372 Paranaguá BRA 126076
379 Palmas BRA 121919
386 Patos de Minas BRA 119262
424 Passos BRA 98570
430 Paulo Afonso BRA 97291
435 Parnamirim BRA 96210
448 Patos BRA 90519
451 Palhoça BRA 89465
3793 New York USA 8008278
3794 Los Angeles USA 3694820
3795 Chicago USA 2896016
3796 Houston USA 1953631
3797 Philadelphia USA 1517550
3798 Phoenix USA 1321045
3799 San Diego USA 1223400
3800 Dallas USA 1188580
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 11000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name LIKE 'P%' OR ID BETWEEN 4000 AND 4300);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY,Population,Country,Name,CountryPopulation,CountryName CountryName 38 NULL 23 Using where
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 11000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name LIKE 'Pho%' OR ID BETWEEN 4000 AND 4300);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY,Population,Country,Name,CountryPopulation,CountryName Name 35 NULL 1 Using where
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 11000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name LIKE 'P%' OR ID BETWEEN 4000 AND 4300);
ID Name Country Population
3797 Philadelphia USA 1517550
3798 Phoenix USA 1321045
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 11000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name LIKE 'P%' OR ID BETWEEN 4000 AND 4300);
ID Name Country Population
3797 Philadelphia USA 1517550
3798 Phoenix USA 1321045
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 11000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name LIKE 'Pho%' OR ID BETWEEN 4000 AND 4300);
ID Name Country Population
3798 Phoenix USA 1321045
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 11000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name LIKE 'Pho%' OR ID BETWEEN 4000 AND 4300);
ID Name Country Population
3798 Phoenix USA 1321045
DROP INDEX Population ON City;
DROP INDEX Name ON City;
EXPLAIN
SELECT * FROM City
WHERE Country='USA' AND Population BETWEEN 101000 AND 102000 OR
Country='USA' AND Name LIKE 'Pa%';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge Country,CountryPopulation,CountryName CountryPopulation,CountryName 7,38 NULL 10 Using sort_union(CountryPopulation,CountryName); Using where
SELECT * FROM City USE INDEX()
WHERE Country='USA' AND Population BETWEEN 101000 AND 102000 OR
Country='USA' AND Name LIKE 'Pa%';
ID Name Country Population
3932 Paterson USA 149222
3943 Pasadena USA 141674
3953 Pasadena USA 133936
3967 Paradise USA 124682
3986 Palmdale USA 116670
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
SELECT * FROM City
WHERE Country='USA' AND Population BETWEEN 101000 AND 102000 OR
Country='USA' AND Name LIKE 'Pa%';
ID Name Country Population
3932 Paterson USA 149222
3943 Pasadena USA 141674
3953 Pasadena USA 133936
3967 Paradise USA 124682
3986 Palmdale USA 116670
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
EXPLAIN
SELECT * FROM City
WHERE Country='USA' AND
(Population BETWEEN 101000 AND 102000 OR Name LIKE 'Pa%');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge Country,CountryPopulation,CountryName CountryPopulation,CountryName 7,38 NULL 10 Using sort_union(CountryPopulation,CountryName); Using where
SELECT * FROM City
WHERE Country='USA' AND
(Population BETWEEN 101000 AND 102000 OR Name LIKE 'Pa%');
ID Name Country Population
3932 Paterson USA 149222
3943 Pasadena USA 141674
3953 Pasadena USA 133936
3967 Paradise USA 124682
3986 Palmdale USA 116670
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
SELECT * FROM City
WHERE Country='USA' AND
(Population BETWEEN 101000 AND 102000 OR Name LIKE 'Pa%');
ID Name Country Population
3932 Paterson USA 149222
3943 Pasadena USA 141674
3953 Pasadena USA 133936
3967 Paradise USA 124682
3986 Palmdale USA 116670
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
DROP DATABASE world;
use test;
CREATE TABLE t1 (
id int(10) unsigned NOT NULL auto_increment,
account_id int(10) unsigned NOT NULL,
first_name varchar(50) default NULL,
middle_name varchar(50) default NULL,
last_name varchar(100) default NULL,
home_address_1 varchar(150) default NULL,
home_city varchar(75) default NULL,
home_state char(2) default NULL,
home_postal_code varchar(50) default NULL,
home_county varchar(75) default NULL,
home_country char(3) default NULL,
work_address_1 varchar(150) default NULL,
work_city varchar(75) default NULL,
work_state char(2) default NULL,
work_postal_code varchar(50) default NULL,
work_county varchar(75) default NULL,
work_country char(3) default NULL,
login varchar(50) NOT NULL,
PRIMARY KEY (id),
KEY login (login,account_id),
KEY account_id (account_id),
KEY user_home_country_indx (home_country),
KEY user_work_country_indx (work_country),
KEY user_home_state_indx (home_state),
KEY user_work_state_indx (work_state),
KEY user_home_city_indx (home_city),
KEY user_work_city_indx (work_city),
KEY user_first_name_indx (first_name),
KEY user_last_name_indx (last_name)
);
insert into t1(account_id, login, home_state, work_state) values
(1, 'pw', 'ia', 'ia'), (1, 'pw', 'ia', 'ia'), (1, 'pw', 'ia', 'ia'),
(1, 'pw', 'ia', 'ia'), (1, 'pw', 'ia', 'ia'), (1, 'pw', 'ia', 'ia');
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
analyze table t1;
Table Op Msg_type Msg_text
test.t1 analyze status OK
select count(*) from t1 where account_id = 1;
count(*)
3072
select * from t1
where (home_state = 'ia' or work_state='ia') and account_id = 1;
id account_id first_name middle_name last_name home_address_1 home_city home_state home_postal_code home_county home_country work_address_1 work_city work_state work_postal_code work_county work_country login
1 1 NULL NULL NULL NULL NULL ia NULL NULL NULL NULL NULL ia NULL NULL NULL pw
2 1 NULL NULL NULL NULL NULL ia NULL NULL NULL NULL NULL ia NULL NULL NULL pw
3 1 NULL NULL NULL NULL NULL ia NULL NULL NULL NULL NULL ia NULL NULL NULL pw
4 1 NULL NULL NULL NULL NULL ia NULL NULL NULL NULL NULL ia NULL NULL NULL pw
5 1 NULL NULL NULL NULL NULL ia NULL NULL NULL NULL NULL ia NULL NULL NULL pw
6 1 NULL NULL NULL NULL NULL ia NULL NULL NULL NULL NULL ia NULL NULL NULL pw
explain
select * from t1
where (home_state = 'ia' or work_state='ia') and account_id = 1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge account_id,user_home_state_indx,user_work_state_indx user_home_state_indx,user_work_state_indx 3,3 NULL 6 Using union(user_home_state_indx,user_work_state_indx); Using where
drop table t1;
CREATE TABLE t1 (
c1 int(11) NOT NULL auto_increment,
c2 decimal(10,0) default NULL,
c3 decimal(10,0) default NULL,
c4 decimal(10,0) default NULL,
c5 decimal(10,0) default NULL,
cp decimal(1,0) default NULL,
ce decimal(10,0) default NULL,
cdata char(20),
PRIMARY KEY (c1),
KEY k1 (c2,c3,cp,ce),
KEY k2 (c4,c5,cp,ce)
);
insert into t1 (c2, c3, c4, c5, cp) values(1,1,1,1,1);
insert into t1 (c2, c3, c4, c5, cp) values(2,1,1,1,4);
insert into t1 (c2, c3, c4, c5, cp) values(2,1,2,1,1);
insert into t1 (c2, c3, c4, c5, cp) values(2,1,3,1,4);
insert into t1 (c2, c3, c4, c5, cp) values(3,1,4,1,4);
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
analyze table t1;
Table Op Msg_type Msg_text
test.t1 analyze status OK
explain
select * from t1 where (c2=1 and c3=1) or (c4=2 and c5=1);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge k1,k2 k1,k2 12,12 NULL 2 Using sort_union(k1,k2); Using where
explain
select * from t1
where (c2=1 and c3=1 and cp=1) or (c4=2 and c5=1 and cp=1);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge k1,k2 k1,k2 14,14 NULL 2 Using sort_union(k1,k2); Using where
explain
select * from t1
where ((c2=1 and c3=1) or (c4=2 and c5=1)) and cp=1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge k1,k2 k1,k2 14,14 NULL 2 Using sort_union(k1,k2); Using where
select * from t1
where (c2=1 and c3=1 and cp=1) or (c4=2 and c5=1 and cp=1);
c1 c2 c3 c4 c5 cp ce cdata
1 1 1 1 1 1 NULL NULL
3 2 1 2 1 1 NULL NULL
select * from t1
where ((c2=1 and c3=1) or (c4=2 and c5=1)) and cp=1;
c1 c2 c3 c4 c5 cp ce cdata
1 1 1 1 1 1 NULL NULL
3 2 1 2 1 1 NULL NULL
drop table t1;
create table t1 (
c1 int auto_increment primary key,
c2 char(20),
c3 char (20),
c4 int
);
alter table t1 add key k1 (c2);
alter table t1 add key k2 (c3);
alter table t1 add key k3 (c4);
insert into t1 values(null, 'a', 'b', 0);
insert into t1 values(null, 'c', 'b', 0);
insert into t1 values(null, 'a', 'd', 0);
insert into t1 values(null, 'ccc', 'qqq', 0);
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3,c4) select c2,c3,1 from t1 where c2 != 'a';
insert into t1 (c2,c3,c4) select c2,c3,2 from t1 where c2 != 'a';
insert into t1 (c2,c3,c4) select c2,c3,3 from t1 where c2 != 'a';
insert into t1 (c2,c3,c4) select c2,c3,4 from t1 where c2 != 'a';
analyze table t1;
Table Op Msg_type Msg_text
test.t1 analyze status OK
select count(*) from t1 where (c2='e' OR c3='q');
count(*)
0
select count(*) from t1 where c4 != 0;
count(*)
3840
explain
select distinct c1 from t1 where (c2='e' OR c3='q');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge k1,k2 k1,k2 21,21 NULL 2 Using union(k1,k2); Using where
explain
select distinct c1 from t1 where (c4!= 0) AND (c2='e' OR c3='q');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge k1,k2,k3 k1,k2 21,21 NULL 2 Using union(k1,k2); Using where
drop table t1;
create table t1 (
id int unsigned auto_increment primary key,
c1 char(12),
c2 char(15),
c3 char(1)
);
insert into t1 (c3) values ('1'), ('2');
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
update t1 set c1=lpad(id+1000, 12, ' '), c2=lpad(id+10000, 15, ' ');
alter table t1 add unique index (c1), add unique index (c2), add index (c3);
analyze table t1;
Table Op Msg_type Msg_text
test.t1 analyze status OK
explain
select * from t1 where (c1=' 100000' or c2=' 2000000');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge c1,c2 c1,c2 13,16 NULL 2 Using union(c1,c2); Using where
explain
select * from t1 where (c1=' 100000' or c2=' 2000000') and c3='2';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge c1,c2,c3 c1,c2 13,16 NULL 2 Using union(c1,c2); Using where
select * from t1 where (c1=' 100000' or c2=' 2000000');
id c1 c2 c3
select * from t1 where (c1=' 100000' or c2=' 2000000') and c3='2';
id c1 c2 c3
drop table t1;
CREATE TABLE t1 (
a smallint DEFAULT NULL,
pk int NOT NULL AUTO_INCREMENT PRIMARY KEY,
b varchar(10) DEFAULT NULL,
c varchar(64) DEFAULT NULL,
INDEX idx1 (a),
INDEX idx2 (b),
INDEX idx3 (c)
);
SELECT COUNT(*) FROM t1 IGNORE INDEX (idx2,idx3)
WHERE c = 'i' OR b IN ( 'Arkansas' , 'd' , 'pdib' , 'can' ) OR
(pk BETWEEN 120 AND 79 + 255 OR a IN ( 4 , 179 , 1 ) ) AND a > 8 ;
COUNT(*)
5
SELECT COUNT(*) FROM t1
WHERE c = 'i' OR b IN ( 'Arkansas' , 'd' , 'pdib' , 'can' ) OR
(pk BETWEEN 120 AND 79 + 255 OR a IN ( 4 , 179 , 1 ) ) AND a > 8 ;
COUNT(*)
5
EXPLAIN
SELECT COUNT(*) FROM t1
WHERE c = 'i' OR b IN ( 'Arkansas' , 'd' , 'pdib' , 'can' ) OR
(pk BETWEEN 120 AND 79 + 255 OR a IN ( 4 , 179 , 1 ) ) AND a > 8 ;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge PRIMARY,idx1,idx2,idx3 idx3,idx2,PRIMARY,idx1 67,13,4,3 NULL 8 Using sort_union(idx3,idx2,PRIMARY,idx1); Using where
DROP TABLE t1;
CREATE TABLE t1 (
f1 int, f2 int, f3 int, f4 int, f5 int,
PRIMARY KEY (f4), KEY (f1), KEY (f2), KEY (f3)
) ;
INSERT INTO t1 VALUES (0,0,NULL,9,5), (0,0,1,9425,NULL);
SELECT f5 FROM t1
WHERE f2 != 1 OR f1 IS NULL OR f4 = 4 OR
f2 AND (f4 BETWEEN 6 AND 255 OR f3 IS NULL);
f5
5
NULL
DROP TABLE t1;
CREATE TABLE t1 (
f1 int, f2 int, f3 int, f4 int,
PRIMARY KEY (f1), KEY (f3), KEY (f4)
);
INSERT INTO t1 VALUES (9,0,2,6), (9930,0,0,NULL);
SET SESSION optimizer_switch='index_merge_intersection=off';
SET SESSION optimizer_switch='index_merge_sort_union=off';
SET SESSION optimizer_switch='index_merge_union=off';
EXPLAIN
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL PRIMARY,f3,f4 NULL NULL NULL 2 Using where
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
f1 f2 f3 f4
9 0 2 6
SET SESSION optimizer_switch='index_merge_union=on';
EXPLAIN
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL PRIMARY,f3,f4 NULL NULL NULL 2 Using where
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
f1 f2 f3 f4
9 0 2 6
INSERT INTO t1 VALUES
(93,0,3,6), (9933,0,3,3), (94,0,4,6), (9934,0,4,4),
(95,0,5,6), (9935,0,5,5), (96,0,6,6), (9936,0,6,6),
(97,0,7,6), (9937,0,7,7), (98,0,8,6), (9938,0,8,8),
(99,0,9,6), (9939,0,9,9);
SET SESSION optimizer_switch='index_merge_union=off';
EXPLAIN
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL PRIMARY,f3,f4 NULL NULL NULL 16 Using where
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
f1 f2 f3 f4
9 0 2 6
SET SESSION optimizer_switch='index_merge_union=on';
EXPLAIN
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge PRIMARY,f3,f4 f3,PRIMARY,f3 5,4,5 NULL 3 Using union(f3,PRIMARY,f3); Using where
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
f1 f2 f3 f4
9 0 2 6
SET SESSION optimizer_switch=DEFAULT;
DROP TABLE t1;
SET SESSION STORAGE_ENGINE='InnoDB';
DROP TABLE IF EXISTS t1,t2,t3,t4;
DROP DATABASE IF EXISTS world;
set names utf8;
CREATE DATABASE world;
use world;
CREATE TABLE Country (
Code char(3) NOT NULL default '',
Name char(52) NOT NULL default '',
SurfaceArea float(10,2) NOT NULL default '0.00',
Population int(11) NOT NULL default '0',
Capital int(11) default NULL,
PRIMARY KEY (Code),
UNIQUE INDEX (Name)
);
CREATE TABLE City (
ID int(11) NOT NULL auto_increment,
Name char(35) NOT NULL default '',
Country char(3) NOT NULL default '',
Population int(11) NOT NULL default '0',
PRIMARY KEY (ID),
INDEX (Population),
INDEX (Country)
);
CREATE TABLE CountryLanguage (
Country char(3) NOT NULL default '',
Language char(30) NOT NULL default '',
Percentage float(3,1) NOT NULL default '0.0',
PRIMARY KEY (Country, Language),
INDEX (Percentage)
);
SELECT COUNT(*) FROM Country;
COUNT(*)
239
SELECT COUNT(*) FROM City;
COUNT(*)
4079
SELECT COUNT(*) FROM CountryLanguage;
COUNT(*)
984
CREATE INDEX Name ON City(Name);
EXPLAIN
SELECT * FROM City
WHERE (Population >= 100000 OR Name LIKE 'P%' OR Population < 100000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City ALL Population,Name NULL NULL NULL 4079 Using where
EXPLAIN
SELECT * FROM City
WHERE (Population >= 100000 OR Name LIKE 'P%') AND Country='CAN' OR
(Population < 100000 OR Name Like 'T%') AND Country='ARG';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population,Country,Name Country 3 NULL 106 Using where
EXPLAIN
SELECT * FROM City
WHERE Population < 200000 AND Name LIKE 'P%' AND
(Population > 300000 OR Name LIKE 'T%') AND
(Population < 100000 OR Name LIKE 'Pa%');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population,Name Name 35 NULL 235 Using where
EXPLAIN
SELECT * FROM City
WHERE Population > 100000 AND Name LIKE 'Aba%' OR
Country IN ('CAN', 'ARG') AND ID < 3800 OR
Country < 'U' AND Name LIKE 'Zhu%' OR
ID BETWEEN 3800 AND 3810;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge PRIMARY,Population,Country,Name Name,Country,PRIMARY 35,3,4 NULL 125 Using sort_union(Name,Country,PRIMARY); Using where
EXPLAIN
SELECT * FROM City
WHERE (Population > 101000 AND Population < 115000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population Population 4 NULL 458 Using where
EXPLAIN
SELECT * FROM City
WHERE (Population > 101000 AND Population < 103000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population Population 4 NULL 80 Using where
EXPLAIN
SELECT * FROM City
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'));
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge Country,Name Name,Country 35,3 NULL 213 Using sort_union(Name,Country); Using where
EXPLAIN
SELECT * FROM City
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'))
AND (Population > 101000 AND Population < 115000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge Population,Country,Name Name,Country 35,3 NULL 213 Using sort_union(Name,Country); Using where
EXPLAIN
SELECT * FROM City
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'))
AND (Population > 101000 AND Population < 103000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population,Country,Name Population 4 NULL 80 Using where
SELECT * FROM City USE INDEX ()
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'))
AND (Population > 101000 AND Population < 115000);
ID Name Country Population
403 Catanduva BRA 107761
412 Cachoeirinha BRA 103240
636 Bilbays EGY 113608
637 Mit Ghamr EGY 101801
701 Tarragona ESP 113016
702 Lleida (Lérida) ESP 112207
703 Jaén ESP 109247
704 Ourense (Orense) ESP 109120
705 Mataró ESP 104095
706 Algeciras ESP 103106
707 Marbella ESP 101144
759 Gonder ETH 112249
869 Cabuyao PHL 106630
870 Calapan PHL 105910
873 Cauayan PHL 103952
1844 Cape Breton CAN 114733
1847 Cambridge CAN 109186
2908 Cajamarca PER 108009
3003 Caen FRA 113987
3411 Ceyhan TUR 102412
3571 Calabozo VEN 107146
3786 Cam Ranh VNM 114041
3792 Tartu EST 101246
4002 Carrollton USA 109576
4027 Cape Coral USA 102286
4032 Cambridge USA 101355
SELECT * FROM City
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'))
AND (Population > 101000 AND Population < 115000);
ID Name Country Population
403 Catanduva BRA 107761
412 Cachoeirinha BRA 103240
636 Bilbays EGY 113608
637 Mit Ghamr EGY 101801
701 Tarragona ESP 113016
702 Lleida (Lérida) ESP 112207
703 Jaén ESP 109247
704 Ourense (Orense) ESP 109120
705 Mataró ESP 104095
706 Algeciras ESP 103106
707 Marbella ESP 101144
759 Gonder ETH 112249
869 Cabuyao PHL 106630
870 Calapan PHL 105910
873 Cauayan PHL 103952
1844 Cape Breton CAN 114733
1847 Cambridge CAN 109186
2908 Cajamarca PER 108009
3003 Caen FRA 113987
3411 Ceyhan TUR 102412
3571 Calabozo VEN 107146
3786 Cam Ranh VNM 114041
3792 Tartu EST 101246
4002 Carrollton USA 109576
4027 Cape Coral USA 102286
4032 Cambridge USA 101355
SELECT * FROM City USE INDEX ()
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'))
AND (Population > 101000 AND Population < 103000);
ID Name Country Population
637 Mit Ghamr EGY 101801
707 Marbella ESP 101144
3411 Ceyhan TUR 102412
3792 Tartu EST 101246
4027 Cape Coral USA 102286
4032 Cambridge USA 101355
SELECT * FROM City
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'))
AND (Population > 101000 AND Population < 103000);
ID Name Country Population
707 Marbella ESP 101144
3792 Tartu EST 101246
4032 Cambridge USA 101355
637 Mit Ghamr EGY 101801
4027 Cape Coral USA 102286
3411 Ceyhan TUR 102412
EXPLAIN
SELECT * FROM City WHERE (Name < 'Ac');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 23 Using where
EXPLAIN
SELECT * FROM City WHERE (Name < 'Bb');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 373 Using where
EXPLAIN
SELECT * FROM City WHERE (Country > 'A' AND Country < 'B');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Country Country 3 NULL 106 Using where
EXPLAIN
SELECT * FROM City WHERE (Name BETWEEN 'P' AND 'Pb');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 71 Using where
EXPLAIN
SELECT * FROM City WHERE (Name BETWEEN 'P' AND 'S');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 384 Using where
EXPLAIN
SELECT * FROM City WHERE (Population > 101000 AND Population < 110000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population Population 4 NULL 327 Using where
EXPLAIN
SELECT * FROM City WHERE (Population > 103000 AND Population < 104000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population Population 4 NULL 36 Using where
EXPLAIN
SELECT * FROM City
WHERE (Name < 'Ac' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'Pb' AND (Population > 101000 AND Population < 110000));
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population,Country,Name Name 35 NULL 94 Using where
EXPLAIN
SELECT * FROM City
WHERE (Name < 'Ac' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'S' AND (Population > 103000 AND Population < 104000));
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge Population,Country,Name Name,Population 35,4 NULL 59 Using sort_union(Name,Population); Using where
EXPLAIN
SELECT * FROM City
WHERE (Name < 'Bb' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'Pb' AND (Population > 101000 AND Population < 110000));
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge Population,Country,Name Country,Name 3,35 NULL 177 Using sort_union(Country,Name); Using where
EXPLAIN
SELECT * FROM City
WHERE (Name < 'Bb' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'S' AND (Population > 103000 AND Population < 104000));
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge Population,Country,Name Country,Population 3,4 NULL 142 Using sort_union(Country,Population); Using where
SELECT * FROM City USE INDEX ()
WHERE (Name < 'Ac' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'Pb' AND (Population > 101000 AND Population < 110000));
ID Name Country Population
65 Abu Dhabi ARE 398695
168 Pabna BGD 103277
189 Parakou BEN 103577
750 Paarl ZAF 105768
2865 Pak Pattan PAK 107800
SELECT * FROM City
WHERE (Name < 'Ac' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'Pb' AND (Population > 101000 AND Population < 110000));
ID Name Country Population
65 Abu Dhabi ARE 398695
750 Paarl ZAF 105768
168 Pabna BGD 103277
2865 Pak Pattan PAK 107800
189 Parakou BEN 103577
SELECT * FROM City USE INDEX ()
WHERE (Name < 'Ac' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'S' AND (Population > 103000 AND Population < 104000));
ID Name Country Population
65 Abu Dhabi ARE 398695
168 Pabna BGD 103277
189 Parakou BEN 103577
1003 Pemalang IDN 103500
2663 Río Bravo MEX 103901
SELECT * FROM City
WHERE (Name < 'Ac' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'S' AND (Population > 103000 AND Population < 104000));
ID Name Country Population
65 Abu Dhabi ARE 398695
168 Pabna BGD 103277
189 Parakou BEN 103577
1003 Pemalang IDN 103500
2663 Río Bravo MEX 103901
SELECT * FROM City USE INDEX ()
WHERE (Name < 'Bb' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'Pb' AND (Population > 101000 AND Population < 110000));
ID Name Country Population
55 Andorra la Vella AND 21189
65 Abu Dhabi ARE 398695
67 al-Ayn ARE 225970
68 Ajman ARE 114395
75 Almirante Brown ARG 538918
85 Avellaneda ARG 353046
96 Bahía Blanca ARG 239810
134 Adelaide AUS 978100
144 Baku AZE 1787800
168 Pabna BGD 103277
189 Parakou BEN 103577
750 Paarl ZAF 105768
2865 Pak Pattan PAK 107800
SELECT * FROM City
WHERE (Name < 'Bb' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'Pb' AND (Population > 101000 AND Population < 110000));
ID Name Country Population
55 Andorra la Vella AND 21189
65 Abu Dhabi ARE 398695
67 al-Ayn ARE 225970
68 Ajman ARE 114395
75 Almirante Brown ARG 538918
85 Avellaneda ARG 353046
96 Bahía Blanca ARG 239810
134 Adelaide AUS 978100
144 Baku AZE 1787800
168 Pabna BGD 103277
189 Parakou BEN 103577
750 Paarl ZAF 105768
2865 Pak Pattan PAK 107800
SELECT * FROM City USE INDEX ()
WHERE (Name < 'Bb' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'S' AND (Population > 103000 AND Population < 104000));
ID Name Country Population
55 Andorra la Vella AND 21189
65 Abu Dhabi ARE 398695
67 al-Ayn ARE 225970
68 Ajman ARE 114395
75 Almirante Brown ARG 538918
85 Avellaneda ARG 353046
96 Bahía Blanca ARG 239810
134 Adelaide AUS 978100
144 Baku AZE 1787800
168 Pabna BGD 103277
189 Parakou BEN 103577
1003 Pemalang IDN 103500
2663 Río Bravo MEX 103901
SELECT * FROM City
WHERE (Name < 'Bb' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'S' AND (Population > 103000 AND Population < 104000));
ID Name Country Population
55 Andorra la Vella AND 21189
65 Abu Dhabi ARE 398695
67 al-Ayn ARE 225970
68 Ajman ARE 114395
75 Almirante Brown ARG 538918
85 Avellaneda ARG 353046
96 Bahía Blanca ARG 239810
134 Adelaide AUS 978100
144 Baku AZE 1787800
168 Pabna BGD 103277
189 Parakou BEN 103577
1003 Pemalang IDN 103500
2663 Río Bravo MEX 103901
EXPLAIN
SELECT * FROM City WHERE (ID < 50) OR (ID BETWEEN 100 AND 110);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 60 Using where
EXPLAIN
SELECT * FROM City WHERE (ID < 200) OR (ID BETWEEN 300 AND 600);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 1142 Using where
EXPLAIN
SELECT * FROM City WHERE (ID < 600) OR (ID BETWEEN 900 AND 1800);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 2950 Using where
EXPLAIN
SELECT * FROM City WHERE Country > 'A' AND Country < 'ARG';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Country Country 3 NULL 19 Using where
EXPLAIN
SELECT * FROM City WHERE Name LIKE 'H%' OR Name LIKE 'P%' ;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 394 Using where
EXPLAIN
SELECT * FROM City WHERE Name LIKE 'Ha%' OR Name LIKE 'Pa%' ;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 133 Using where
EXPLAIN
SELECT * FROM City
WHERE ((ID < 50) AND (Name LIKE 'H%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 100 AND 110) AND
(Name LIKE 'P%' OR (Population > 103000 AND Population < 104000)));
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY,Population,Country,Name PRIMARY 4 NULL 60 Using where
EXPLAIN
SELECT * FROM City
WHERE ((ID < 800) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 900 AND 1800) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge PRIMARY,Population,Country,Name Name,Country,Population 35,3,4 NULL 188 Using sort_union(Name,Country,Population); Using where
EXPLAIN
SELECT * FROM City
WHERE ((ID < 600) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 300 AND 600) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY,Population,Country,Name PRIMARY 4 NULL 1242 Using where
SELECT * FROM City USE INDEX ()
WHERE ((ID < 50) AND (Name LIKE 'H%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 100 AND 110) AND
(Name LIKE 'P%' OR (Population > 103000 AND Population < 104000)));
ID Name Country Population
1 Kabul AFG 1780000
2 Qandahar AFG 237500
3 Herat AFG 186800
4 Mazar-e-Sharif AFG 127800
7 Haag NLD 440900
16 Haarlem NLD 148772
25 Haarlemmermeer NLD 110722
31 Heerlen NLD 95052
33 Willemstad ANT 2345
34 Tirana ALB 270000
100 Paraná ARG 207041
102 Posadas ARG 201273
SELECT * FROM City
WHERE ((ID < 50) AND (Name LIKE 'H%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 100 AND 110) AND
(Name LIKE 'P%' OR (Population > 103000 AND Population < 104000)));
ID Name Country Population
1 Kabul AFG 1780000
2 Qandahar AFG 237500
3 Herat AFG 186800
4 Mazar-e-Sharif AFG 127800
7 Haag NLD 440900
16 Haarlem NLD 148772
25 Haarlemmermeer NLD 110722
31 Heerlen NLD 95052
33 Willemstad ANT 2345
34 Tirana ALB 270000
100 Paraná ARG 207041
102 Posadas ARG 201273
SELECT * FROM City USE INDEX()
WHERE ((ID < 800) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 900 AND 1800) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
ID Name Country Population
1 Kabul AFG 1780000
2 Qandahar AFG 237500
3 Herat AFG 186800
4 Mazar-e-Sharif AFG 127800
7 Haag NLD 440900
16 Haarlem NLD 148772
25 Haarlemmermeer NLD 110722
33 Willemstad ANT 2345
34 Tirana ALB 270000
55 Andorra la Vella AND 21189
56 Luanda AGO 2022000
57 Huambo AGO 163100
58 Lobito AGO 130000
59 Benguela AGO 128300
60 Namibe AGO 118200
61 South Hill AIA 961
62 The Valley AIA 595
64 Dubai ARE 669181
65 Abu Dhabi ARE 398695
66 Sharja ARE 320095
67 al-Ayn ARE 225970
68 Ajman ARE 114395
129 Oranjestad ABW 29034
191 Hamilton BMU 1200
528 Hartlepool GBR 92000
529 Halifax GBR 91069
914 Sekondi-Takoradi GHA 103653
943 Palembang IDN 1222764
950 Padang IDN 534474
983 Palu IDN 142800
984 Pasuruan IDN 134019
991 Pangkal Pinang IDN 124000
1003 Pemalang IDN 103500
1004 Klaten IDN 103300
1007 Palangka Raya IDN 99693
1020 Padang Sidempuan IDN 91200
1045 Patna IND 917243
1114 Panihati IND 275990
1129 Patiala IND 238368
1142 Panipat IND 215218
1159 Parbhani IND 190255
1231 Pali IND 136842
1263 Pathankot IND 123930
1265 Palghat (Palakkad) IND 123289
1293 Pallavaram IND 111866
1319 Tellicherry (Thalassery) IND 103579
1339 Palayankottai IND 97662
1345 Patan IND 96109
1436 Marv Dasht IRN 103579
1468 Palermo ITA 683794
1478 Padova ITA 211391
1484 Parma ITA 168717
1530 Kingston JAM 103962
1747 Toda JPN 103969
1748 Tajimi JPN 103171
1785 Ibb YEM 103300
SELECT * FROM City
WHERE ((ID < 800) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 900 AND 1800) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
ID Name Country Population
1 Kabul AFG 1780000
2 Qandahar AFG 237500
3 Herat AFG 186800
4 Mazar-e-Sharif AFG 127800
7 Haag NLD 440900
16 Haarlem NLD 148772
25 Haarlemmermeer NLD 110722
33 Willemstad ANT 2345
34 Tirana ALB 270000
55 Andorra la Vella AND 21189
56 Luanda AGO 2022000
57 Huambo AGO 163100
58 Lobito AGO 130000
59 Benguela AGO 128300
60 Namibe AGO 118200
61 South Hill AIA 961
62 The Valley AIA 595
64 Dubai ARE 669181
65 Abu Dhabi ARE 398695
66 Sharja ARE 320095
67 al-Ayn ARE 225970
68 Ajman ARE 114395
129 Oranjestad ABW 29034
191 Hamilton BMU 1200
528 Hartlepool GBR 92000
529 Halifax GBR 91069
914 Sekondi-Takoradi GHA 103653
943 Palembang IDN 1222764
950 Padang IDN 534474
983 Palu IDN 142800
984 Pasuruan IDN 134019
991 Pangkal Pinang IDN 124000
1003 Pemalang IDN 103500
1004 Klaten IDN 103300
1007 Palangka Raya IDN 99693
1020 Padang Sidempuan IDN 91200
1045 Patna IND 917243
1114 Panihati IND 275990
1129 Patiala IND 238368
1142 Panipat IND 215218
1159 Parbhani IND 190255
1231 Pali IND 136842
1263 Pathankot IND 123930
1265 Palghat (Palakkad) IND 123289
1293 Pallavaram IND 111866
1319 Tellicherry (Thalassery) IND 103579
1339 Palayankottai IND 97662
1345 Patan IND 96109
1436 Marv Dasht IRN 103579
1468 Palermo ITA 683794
1478 Padova ITA 211391
1484 Parma ITA 168717
1530 Kingston JAM 103962
1747 Toda JPN 103969
1748 Tajimi JPN 103171
1785 Ibb YEM 103300
SELECT * FROM City USE INDEX ()
WHERE ((ID < 200) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 300 AND 600) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
ID Name Country Population
1 Kabul AFG 1780000
2 Qandahar AFG 237500
3 Herat AFG 186800
4 Mazar-e-Sharif AFG 127800
7 Haag NLD 440900
16 Haarlem NLD 148772
25 Haarlemmermeer NLD 110722
33 Willemstad ANT 2345
34 Tirana ALB 270000
55 Andorra la Vella AND 21189
56 Luanda AGO 2022000
57 Huambo AGO 163100
58 Lobito AGO 130000
59 Benguela AGO 128300
60 Namibe AGO 118200
61 South Hill AIA 961
62 The Valley AIA 595
64 Dubai ARE 669181
65 Abu Dhabi ARE 398695
66 Sharja ARE 320095
67 al-Ayn ARE 225970
68 Ajman ARE 114395
129 Oranjestad ABW 29034
191 Hamilton BMU 1200
339 Passo Fundo BRA 166343
364 Parnaíba BRA 129756
372 Paranaguá BRA 126076
379 Palmas BRA 121919
386 Patos de Minas BRA 119262
411 Guaratinguetá BRA 103433
412 Cachoeirinha BRA 103240
413 Codó BRA 103153
424 Passos BRA 98570
430 Paulo Afonso BRA 97291
435 Parnamirim BRA 96210
448 Patos BRA 90519
451 Palhoça BRA 89465
517 Oldham GBR 103931
SELECT * FROM City
WHERE ((ID < 200) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 300 AND 600) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
ID Name Country Population
1 Kabul AFG 1780000
2 Qandahar AFG 237500
3 Herat AFG 186800
4 Mazar-e-Sharif AFG 127800
7 Haag NLD 440900
16 Haarlem NLD 148772
25 Haarlemmermeer NLD 110722
33 Willemstad ANT 2345
34 Tirana ALB 270000
55 Andorra la Vella AND 21189
56 Luanda AGO 2022000
57 Huambo AGO 163100
58 Lobito AGO 130000
59 Benguela AGO 128300
60 Namibe AGO 118200
61 South Hill AIA 961
62 The Valley AIA 595
64 Dubai ARE 669181
65 Abu Dhabi ARE 398695
66 Sharja ARE 320095
67 al-Ayn ARE 225970
68 Ajman ARE 114395
129 Oranjestad ABW 29034
191 Hamilton BMU 1200
339 Passo Fundo BRA 166343
364 Parnaíba BRA 129756
372 Paranaguá BRA 126076
379 Palmas BRA 121919
386 Patos de Minas BRA 119262
411 Guaratinguetá BRA 103433
412 Cachoeirinha BRA 103240
413 Codó BRA 103153
424 Passos BRA 98570
430 Paulo Afonso BRA 97291
435 Parnamirim BRA 96210
448 Patos BRA 90519
451 Palhoça BRA 89465
517 Oldham GBR 103931
EXPLAIN
SELECT * FROM City WHERE Population > 101000 AND Population < 102000;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population Population 4 NULL 38 Using where
EXPLAIN
SELECT * FROM City WHERE Population > 101000 AND Population < 110000;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population Population 4 NULL 327 Using where
EXPLAIN
SELECT * FROM City WHERE Country < 'C';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Country Country 3 NULL 446 Using where
EXPLAIN
SELECT * FROM City WHERE Country < 'AGO';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Country Country 3 NULL 5 Using where
EXPLAIN
SELECT * FROM City WHERE Name BETWEEN 'P' AND 'S';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 384 Using where
EXPLAIN
SELECT * FROM City WHERE Name BETWEEN 'P' AND 'Pb';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 71 Using where
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 3400 AND 3800;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 944 Using where
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 3790 AND 3800;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 11 Using where
EXPLAIN
SELECT * FROM City WHERE Name LIKE 'P%';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 235 Using where
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 102000) AND
(Country < 'C' OR Name BETWEEN 'P' AND 'S')) OR
((ID BETWEEN 3400 AND 3800) AND
(Country < 'AGO' OR Name LIKE 'Pa%'));
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge PRIMARY,Population,Country,Name Country,Name,Population 3,35,4 NULL 114 Using sort_union(Country,Name,Population); Using where
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 110000) AND
(Country < 'AGO' OR Name BETWEEN 'P' AND 'Pb')) OR
((ID BETWEEN 3790 AND 3800) AND
(Country < 'C' OR Name LIKE 'P%'));
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge PRIMARY,Population,Country,Name Country,Name,PRIMARY 3,35,4 NULL 87 Using sort_union(Country,Name,PRIMARY); Using where
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 102000) AND
(Country < 'C' OR Name BETWEEN 'P' AND 'S')) OR
((ID BETWEEN 3400 AND 3800) AND
(Country < 'AGO' OR Name LIKE 'Pa%'));
ID Name Country Population
169 Naogaon BGD 101266
205 Francistown BWA 101805
417 Itaituba BRA 101320
418 Araras BRA 101046
751 Potchefstroom ZAF 101817
2909 Puno PER 101578
3463 Pavlograd UKR 127000
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 102000) AND
(Country < 'C' OR Name BETWEEN 'P' AND 'S')) OR
((ID BETWEEN 3400 AND 3800) AND
(Country < 'AGO' OR Name LIKE 'Pa%'));
ID Name Country Population
169 Naogaon BGD 101266
205 Francistown BWA 101805
417 Itaituba BRA 101320
418 Araras BRA 101046
751 Potchefstroom ZAF 101817
2909 Puno PER 101578
3463 Pavlograd UKR 127000
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 110000) AND
(Country < 'AGO' OR Name BETWEEN 'P' AND 'Pb')) OR
((ID BETWEEN 3790 AND 3800) AND
(Country < 'C' OR Name LIKE 'P%'));
ID Name Country Population
168 Pabna BGD 103277
189 Parakou BEN 103577
750 Paarl ZAF 105768
2865 Pak Pattan PAK 107800
3797 Philadelphia USA 1517550
3798 Phoenix USA 1321045
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 110000) AND
(Country < 'AGO' OR Name BETWEEN 'P' AND 'Pb')) OR
((ID BETWEEN 3790 AND 3800) AND
(Country < 'C' OR Name LIKE 'P%'));
ID Name Country Population
168 Pabna BGD 103277
189 Parakou BEN 103577
750 Paarl ZAF 105768
2865 Pak Pattan PAK 107800
3797 Philadelphia USA 1517550
3798 Phoenix USA 1321045
CREATE INDEX CountryPopulation ON City(Country,Population);
EXPLAIN
SELECT * FROM City WHERE Name LIKE 'Pas%';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 8 Using where
EXPLAIN
SELECT * FROM City WHERE Name LIKE 'P%';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 235 Using where
EXPLAIN
SELECT * FROM City WHERE (Population > 101000 AND Population < 103000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population Population 4 NULL 80 Using where
EXPLAIN
SELECT * FROM City WHERE Country='USA';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City ref Country,CountryPopulation Country 3 const 274 Using where
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 103000) OR Name LIKE 'Pas%')
AND Country='USA';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge Population,Country,Name,CountryPopulation CountryPopulation,Name 7,35 NULL 17 Using sort_union(CountryPopulation,Name); Using where
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 103000) OR Name LIKE 'P%')
AND Country='USA';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City ref Population,Country,Name,CountryPopulation Country 3 const 274 Using where
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 103000) OR Name LIKE 'Pas%')
AND Country='USA';
ID Name Country Population
3943 Pasadena USA 141674
3953 Pasadena USA 133936
4023 Gary USA 102746
4024 Berkeley USA 102743
4025 Santa Clara USA 102361
4026 Green Bay USA 102313
4027 Cape Coral USA 102286
4028 Arvada USA 102153
4029 Pueblo USA 102121
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 103000) OR Name LIKE 'Pas%')
AND Country='USA';
ID Name Country Population
3943 Pasadena USA 141674
3953 Pasadena USA 133936
4023 Gary USA 102746
4024 Berkeley USA 102743
4025 Santa Clara USA 102361
4026 Green Bay USA 102313
4027 Cape Coral USA 102286
4028 Arvada USA 102153
4029 Pueblo USA 102121
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 103000) OR Name LIKE 'P%')
AND Country='USA';
ID Name Country Population
3797 Philadelphia USA 1517550
3798 Phoenix USA 1321045
3820 Portland USA 529121
3844 Pittsburgh USA 334563
3870 Plano USA 222030
3912 Providence USA 173618
3930 Pomona USA 149473
3932 Paterson USA 149222
3943 Pasadena USA 141674
3951 Pembroke Pines USA 137427
3953 Pasadena USA 133936
3967 Paradise USA 124682
3986 Palmdale USA 116670
3996 Peoria USA 112936
4007 Peoria USA 108364
4016 Provo USA 105166
4023 Gary USA 102746
4024 Berkeley USA 102743
4025 Santa Clara USA 102361
4026 Green Bay USA 102313
4027 Cape Coral USA 102286
4028 Arvada USA 102153
4029 Pueblo USA 102121
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
4035 Portsmouth USA 100565
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 103000) OR Name LIKE 'P%')
AND Country='USA';
ID Name Country Population
3797 Philadelphia USA 1517550
3798 Phoenix USA 1321045
3820 Portland USA 529121
3844 Pittsburgh USA 334563
3870 Plano USA 222030
3912 Providence USA 173618
3930 Pomona USA 149473
3932 Paterson USA 149222
3943 Pasadena USA 141674
3951 Pembroke Pines USA 137427
3953 Pasadena USA 133936
3967 Paradise USA 124682
3986 Palmdale USA 116670
3996 Peoria USA 112936
4007 Peoria USA 108364
4016 Provo USA 105166
4023 Gary USA 102746
4024 Berkeley USA 102743
4025 Santa Clara USA 102361
4026 Green Bay USA 102313
4027 Cape Coral USA 102286
4028 Arvada USA 102153
4029 Pueblo USA 102121
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
4035 Portsmouth USA 100565
CREATE INDEX CountryName ON City(Country,Name);
EXPLAIN
SELECT * FROM City WHERE Country='USA';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City ref Country,CountryPopulation,CountryName Country 3 const 274 Using where
EXPLAIN
SELECT * FROM City WHERE Country='BRA';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City ref Country,CountryPopulation,CountryName Country 3 const 250 Using where
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 3790 AND 3800;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 11 Using where
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 4025 AND 4035;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 11 Using where
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 4028 AND 4032;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 5 Using where
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 3500 AND 3800;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 300 Using where
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 4000 AND 4300;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 80 Using where
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 250 and 260 ;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY PRIMARY 4 NULL 11 Using where
EXPLAIN
SELECT * FROM City WHERE (Population > 101000 AND Population < 102000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population Population 4 NULL 38 Using where
EXPLAIN
SELECT * FROM City WHERE (Population > 101000 AND Population < 103000);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Population Population 4 NULL 80 Using where
EXPLAIN
SELECT * FROM City WHERE Name LIKE 'Pa%';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range Name Name 35 NULL 71 Using where
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4025 AND 4035);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge PRIMARY,Population,Country,Name,CountryPopulation,CountryName CountryPopulation,PRIMARY 7,4 NULL 13 Using sort_union(CountryPopulation,PRIMARY); Using where
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 103000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4028 AND 4032);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge PRIMARY,Population,Country,Name,CountryPopulation,CountryName CountryName,PRIMARY 38,4 NULL 10 Using sort_union(CountryName,PRIMARY); Using where
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 110000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name BETWEEN 'P' AND 'T' OR ID BETWEEN 4000 AND 4300);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City ref PRIMARY,Population,Country,Name,CountryPopulation,CountryName Country 3 const 274 Using where
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4025 AND 4035);
ID Name Country Population
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4025 AND 4035);
ID Name Country Population
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4028 AND 4032);
ID Name Country Population
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4028 AND 4032);
ID Name Country Population
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4025 AND 4035);
ID Name Country Population
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4025 AND 4035);
ID Name Country Population
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 and Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
OR (Name LIKE 'Pa%' OR ID BETWEEN 250 AND 260) AND Country='BRA';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge PRIMARY,Population,Country,Name,CountryPopulation,CountryName CountryPopulation,CountryName,PRIMARY 7,38,4 NULL 35 Using sort_union(CountryPopulation,CountryName,PRIMARY); Using where
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 and Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
OR (Name LIKE 'Pa%' OR ID BETWEEN 250 AND 260) AND Country='BRA';
ID Name Country Population
250 Mauá BRA 375055
251 Carapicuíba BRA 357552
252 Olinda BRA 354732
253 Campina Grande BRA 352497
254 São José do Rio Preto BRA 351944
255 Caxias do Sul BRA 349581
256 Moji das Cruzes BRA 339194
257 Diadema BRA 335078
258 Aparecida de Goiânia BRA 324662
259 Piracicaba BRA 319104
260 Cariacica BRA 319033
285 Paulista BRA 248473
339 Passo Fundo BRA 166343
364 Parnaíba BRA 129756
372 Paranaguá BRA 126076
379 Palmas BRA 121919
386 Patos de Minas BRA 119262
424 Passos BRA 98570
430 Paulo Afonso BRA 97291
435 Parnamirim BRA 96210
448 Patos BRA 90519
451 Palhoça BRA 89465
3793 New York USA 8008278
3794 Los Angeles USA 3694820
3795 Chicago USA 2896016
3796 Houston USA 1953631
3797 Philadelphia USA 1517550
3798 Phoenix USA 1321045
3799 San Diego USA 1223400
3800 Dallas USA 1188580
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
SELECT * FROM City
WHERE ((Population > 101000 and Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
OR (Name LIKE 'Pa%' OR ID BETWEEN 250 AND 260) AND Country='BRA';
ID Name Country Population
285 Paulista BRA 248473
339 Passo Fundo BRA 166343
364 Parnaíba BRA 129756
372 Paranaguá BRA 126076
379 Palmas BRA 121919
386 Patos de Minas BRA 119262
424 Passos BRA 98570
430 Paulo Afonso BRA 97291
435 Parnamirim BRA 96210
448 Patos BRA 90519
451 Palhoça BRA 89465
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
250 Mauá BRA 375055
251 Carapicuíba BRA 357552
252 Olinda BRA 354732
253 Campina Grande BRA 352497
254 São José do Rio Preto BRA 351944
255 Caxias do Sul BRA 349581
256 Moji das Cruzes BRA 339194
257 Diadema BRA 335078
258 Aparecida de Goiânia BRA 324662
259 Piracicaba BRA 319104
260 Cariacica BRA 319033
3793 New York USA 8008278
3794 Los Angeles USA 3694820
3795 Chicago USA 2896016
3796 Houston USA 1953631
3797 Philadelphia USA 1517550
3798 Phoenix USA 1321045
3799 San Diego USA 1223400
3800 Dallas USA 1188580
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 11000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name LIKE 'P%' OR ID BETWEEN 4000 AND 4300);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY,Population,Country,Name,CountryPopulation,CountryName CountryName 38 NULL 18 Using where
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 11000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name LIKE 'Pho%' OR ID BETWEEN 4000 AND 4300);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City range PRIMARY,Population,Country,Name,CountryPopulation,CountryName Name 35 NULL 1 Using where
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 11000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name LIKE 'P%' OR ID BETWEEN 4000 AND 4300);
ID Name Country Population
3797 Philadelphia USA 1517550
3798 Phoenix USA 1321045
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 11000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name LIKE 'P%' OR ID BETWEEN 4000 AND 4300);
ID Name Country Population
3797 Philadelphia USA 1517550
3798 Phoenix USA 1321045
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 11000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name LIKE 'Pho%' OR ID BETWEEN 4000 AND 4300);
ID Name Country Population
3798 Phoenix USA 1321045
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 11000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name LIKE 'Pho%' OR ID BETWEEN 4000 AND 4300);
ID Name Country Population
3798 Phoenix USA 1321045
DROP INDEX Population ON City;
DROP INDEX Name ON City;
EXPLAIN
SELECT * FROM City
WHERE Country='USA' AND Population BETWEEN 101000 AND 102000 OR
Country='USA' AND Name LIKE 'Pa%';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge Country,CountryPopulation,CountryName CountryPopulation,CountryName 7,38 NULL 8 Using sort_union(CountryPopulation,CountryName); Using where
SELECT * FROM City USE INDEX()
WHERE Country='USA' AND Population BETWEEN 101000 AND 102000 OR
Country='USA' AND Name LIKE 'Pa%';
ID Name Country Population
3932 Paterson USA 149222
3943 Pasadena USA 141674
3953 Pasadena USA 133936
3967 Paradise USA 124682
3986 Palmdale USA 116670
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
SELECT * FROM City
WHERE Country='USA' AND Population BETWEEN 101000 AND 102000 OR
Country='USA' AND Name LIKE 'Pa%';
ID Name Country Population
3932 Paterson USA 149222
3943 Pasadena USA 141674
3953 Pasadena USA 133936
3967 Paradise USA 124682
3986 Palmdale USA 116670
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
EXPLAIN
SELECT * FROM City
WHERE Country='USA' AND
(Population BETWEEN 101000 AND 102000 OR Name LIKE 'Pa%');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE City index_merge Country,CountryPopulation,CountryName CountryPopulation,CountryName 7,38 NULL 8 Using sort_union(CountryPopulation,CountryName); Using where
SELECT * FROM City
WHERE Country='USA' AND
(Population BETWEEN 101000 AND 102000 OR Name LIKE 'Pa%');
ID Name Country Population
3932 Paterson USA 149222
3943 Pasadena USA 141674
3953 Pasadena USA 133936
3967 Paradise USA 124682
3986 Palmdale USA 116670
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
SELECT * FROM City
WHERE Country='USA' AND
(Population BETWEEN 101000 AND 102000 OR Name LIKE 'Pa%');
ID Name Country Population
3932 Paterson USA 149222
3943 Pasadena USA 141674
3953 Pasadena USA 133936
3967 Paradise USA 124682
3986 Palmdale USA 116670
4030 Sandy USA 101853
4031 Athens-Clarke County USA 101489
4032 Cambridge USA 101355
DROP DATABASE world;
use test;
CREATE TABLE t1 (
id int(10) unsigned NOT NULL auto_increment,
account_id int(10) unsigned NOT NULL,
first_name varchar(50) default NULL,
middle_name varchar(50) default NULL,
last_name varchar(100) default NULL,
home_address_1 varchar(150) default NULL,
home_city varchar(75) default NULL,
home_state char(2) default NULL,
home_postal_code varchar(50) default NULL,
home_county varchar(75) default NULL,
home_country char(3) default NULL,
work_address_1 varchar(150) default NULL,
work_city varchar(75) default NULL,
work_state char(2) default NULL,
work_postal_code varchar(50) default NULL,
work_county varchar(75) default NULL,
work_country char(3) default NULL,
login varchar(50) NOT NULL,
PRIMARY KEY (id),
KEY login (login,account_id),
KEY account_id (account_id),
KEY user_home_country_indx (home_country),
KEY user_work_country_indx (work_country),
KEY user_home_state_indx (home_state),
KEY user_work_state_indx (work_state),
KEY user_home_city_indx (home_city),
KEY user_work_city_indx (work_city),
KEY user_first_name_indx (first_name),
KEY user_last_name_indx (last_name)
);
insert into t1(account_id, login, home_state, work_state) values
(1, 'pw', 'ia', 'ia'), (1, 'pw', 'ia', 'ia'), (1, 'pw', 'ia', 'ia'),
(1, 'pw', 'ia', 'ia'), (1, 'pw', 'ia', 'ia'), (1, 'pw', 'ia', 'ia');
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
analyze table t1;
Table Op Msg_type Msg_text
test.t1 analyze status OK
select count(*) from t1 where account_id = 1;
count(*)
3072
select * from t1
where (home_state = 'ia' or work_state='ia') and account_id = 1;
id account_id first_name middle_name last_name home_address_1 home_city home_state home_postal_code home_county home_country work_address_1 work_city work_state work_postal_code work_county work_country login
1 1 NULL NULL NULL NULL NULL ia NULL NULL NULL NULL NULL ia NULL NULL NULL pw
2 1 NULL NULL NULL NULL NULL ia NULL NULL NULL NULL NULL ia NULL NULL NULL pw
3 1 NULL NULL NULL NULL NULL ia NULL NULL NULL NULL NULL ia NULL NULL NULL pw
4 1 NULL NULL NULL NULL NULL ia NULL NULL NULL NULL NULL ia NULL NULL NULL pw
5 1 NULL NULL NULL NULL NULL ia NULL NULL NULL NULL NULL ia NULL NULL NULL pw
6 1 NULL NULL NULL NULL NULL ia NULL NULL NULL NULL NULL ia NULL NULL NULL pw
explain
select * from t1
where (home_state = 'ia' or work_state='ia') and account_id = 1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge account_id,user_home_state_indx,user_work_state_indx user_home_state_indx,user_work_state_indx 3,3 NULL 10 Using union(user_home_state_indx,user_work_state_indx); Using where
drop table t1;
CREATE TABLE t1 (
c1 int(11) NOT NULL auto_increment,
c2 decimal(10,0) default NULL,
c3 decimal(10,0) default NULL,
c4 decimal(10,0) default NULL,
c5 decimal(10,0) default NULL,
cp decimal(1,0) default NULL,
ce decimal(10,0) default NULL,
cdata char(20),
PRIMARY KEY (c1),
KEY k1 (c2,c3,cp,ce),
KEY k2 (c4,c5,cp,ce)
);
insert into t1 (c2, c3, c4, c5, cp) values(1,1,1,1,1);
insert into t1 (c2, c3, c4, c5, cp) values(2,1,1,1,4);
insert into t1 (c2, c3, c4, c5, cp) values(2,1,2,1,1);
insert into t1 (c2, c3, c4, c5, cp) values(2,1,3,1,4);
insert into t1 (c2, c3, c4, c5, cp) values(3,1,4,1,4);
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
analyze table t1;
Table Op Msg_type Msg_text
test.t1 analyze status OK
explain
select * from t1 where (c2=1 and c3=1) or (c4=2 and c5=1);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge k1,k2 k1,k2 12,12 NULL 2 Using sort_union(k1,k2); Using where
explain
select * from t1
where (c2=1 and c3=1 and cp=1) or (c4=2 and c5=1 and cp=1);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge k1,k2 k1,k2 14,14 NULL 2 Using sort_union(k1,k2); Using where
explain
select * from t1
where ((c2=1 and c3=1) or (c4=2 and c5=1)) and cp=1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge k1,k2 k1,k2 14,14 NULL 2 Using sort_union(k1,k2); Using where
select * from t1
where (c2=1 and c3=1 and cp=1) or (c4=2 and c5=1 and cp=1);
c1 c2 c3 c4 c5 cp ce cdata
1 1 1 1 1 1 NULL NULL
3 2 1 2 1 1 NULL NULL
select * from t1
where ((c2=1 and c3=1) or (c4=2 and c5=1)) and cp=1;
c1 c2 c3 c4 c5 cp ce cdata
1 1 1 1 1 1 NULL NULL
3 2 1 2 1 1 NULL NULL
drop table t1;
create table t1 (
c1 int auto_increment primary key,
c2 char(20),
c3 char (20),
c4 int
);
alter table t1 add key k1 (c2);
alter table t1 add key k2 (c3);
alter table t1 add key k3 (c4);
insert into t1 values(null, 'a', 'b', 0);
insert into t1 values(null, 'c', 'b', 0);
insert into t1 values(null, 'a', 'd', 0);
insert into t1 values(null, 'ccc', 'qqq', 0);
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3,c4) select c2,c3,1 from t1 where c2 != 'a';
insert into t1 (c2,c3,c4) select c2,c3,2 from t1 where c2 != 'a';
insert into t1 (c2,c3,c4) select c2,c3,3 from t1 where c2 != 'a';
insert into t1 (c2,c3,c4) select c2,c3,4 from t1 where c2 != 'a';
analyze table t1;
Table Op Msg_type Msg_text
test.t1 analyze status OK
select count(*) from t1 where (c2='e' OR c3='q');
count(*)
0
select count(*) from t1 where c4 != 0;
count(*)
3840
explain
select distinct c1 from t1 where (c2='e' OR c3='q');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge k1,k2 k1,k2 21,21 NULL 2 Using union(k1,k2); Using where
explain
select distinct c1 from t1 where (c4!= 0) AND (c2='e' OR c3='q');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge k1,k2,k3 k1,k2 21,21 NULL 2 Using union(k1,k2); Using where
drop table t1;
create table t1 (
id int unsigned auto_increment primary key,
c1 char(12),
c2 char(15),
c3 char(1)
);
insert into t1 (c3) values ('1'), ('2');
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
update t1 set c1=lpad(id+1000, 12, ' '), c2=lpad(id+10000, 15, ' ');
alter table t1 add unique index (c1), add unique index (c2), add index (c3);
analyze table t1;
Table Op Msg_type Msg_text
test.t1 analyze status OK
explain
select * from t1 where (c1=' 100000' or c2=' 2000000');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge c1,c2 c1,c2 13,16 NULL 2 Using union(c1,c2); Using where
explain
select * from t1 where (c1=' 100000' or c2=' 2000000') and c3='2';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge c1,c2,c3 c1,c2 13,16 NULL 2 Using union(c1,c2); Using where
select * from t1 where (c1=' 100000' or c2=' 2000000');
id c1 c2 c3
select * from t1 where (c1=' 100000' or c2=' 2000000') and c3='2';
id c1 c2 c3
drop table t1;
CREATE TABLE t1 (
a smallint DEFAULT NULL,
pk int NOT NULL AUTO_INCREMENT PRIMARY KEY,
b varchar(10) DEFAULT NULL,
c varchar(64) DEFAULT NULL,
INDEX idx1 (a),
INDEX idx2 (b),
INDEX idx3 (c)
);
SELECT COUNT(*) FROM t1 IGNORE INDEX (idx2,idx3)
WHERE c = 'i' OR b IN ( 'Arkansas' , 'd' , 'pdib' , 'can' ) OR
(pk BETWEEN 120 AND 79 + 255 OR a IN ( 4 , 179 , 1 ) ) AND a > 8 ;
COUNT(*)
5
SELECT COUNT(*) FROM t1
WHERE c = 'i' OR b IN ( 'Arkansas' , 'd' , 'pdib' , 'can' ) OR
(pk BETWEEN 120 AND 79 + 255 OR a IN ( 4 , 179 , 1 ) ) AND a > 8 ;
COUNT(*)
5
EXPLAIN
SELECT COUNT(*) FROM t1
WHERE c = 'i' OR b IN ( 'Arkansas' , 'd' , 'pdib' , 'can' ) OR
(pk BETWEEN 120 AND 79 + 255 OR a IN ( 4 , 179 , 1 ) ) AND a > 8 ;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge PRIMARY,idx1,idx2,idx3 idx3,idx2,idx1,PRIMARY 67,13,3,4 NULL 9 Using sort_union(idx3,idx2,idx1,PRIMARY); Using where
DROP TABLE t1;
CREATE TABLE t1 (
f1 int, f2 int, f3 int, f4 int, f5 int,
PRIMARY KEY (f4), KEY (f1), KEY (f2), KEY (f3)
) ;
INSERT INTO t1 VALUES (0,0,NULL,9,5), (0,0,1,9425,NULL);
SELECT f5 FROM t1
WHERE f2 != 1 OR f1 IS NULL OR f4 = 4 OR
f2 AND (f4 BETWEEN 6 AND 255 OR f3 IS NULL);
f5
5
NULL
DROP TABLE t1;
CREATE TABLE t1 (
f1 int, f2 int, f3 int, f4 int,
PRIMARY KEY (f1), KEY (f3), KEY (f4)
);
INSERT INTO t1 VALUES (9,0,2,6), (9930,0,0,NULL);
SET SESSION optimizer_switch='index_merge_intersection=off';
SET SESSION optimizer_switch='index_merge_sort_union=off';
SET SESSION optimizer_switch='index_merge_union=off';
EXPLAIN
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL PRIMARY,f3,f4 NULL NULL NULL 2 Using where
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
f1 f2 f3 f4
9 0 2 6
SET SESSION optimizer_switch='index_merge_union=on';
EXPLAIN
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge PRIMARY,f3,f4 PRIMARY,f3 4,5 NULL 2 Using union(PRIMARY,f3); Using where
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
f1 f2 f3 f4
9 0 2 6
INSERT INTO t1 VALUES
(93,0,3,6), (9933,0,3,3), (94,0,4,6), (9934,0,4,4),
(95,0,5,6), (9935,0,5,5), (96,0,6,6), (9936,0,6,6),
(97,0,7,6), (9937,0,7,7), (98,0,8,6), (9938,0,8,8),
(99,0,9,6), (9939,0,9,9);
SET SESSION optimizer_switch='index_merge_union=off';
EXPLAIN
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL PRIMARY,f3,f4 NULL NULL NULL 16 Using where
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
f1 f2 f3 f4
9 0 2 6
SET SESSION optimizer_switch='index_merge_union=on';
EXPLAIN
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index_merge PRIMARY,f3,f4 PRIMARY,f3 4,5 NULL 2 Using union(PRIMARY,f3); Using where
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
f1 f2 f3 f4
9 0 2 6
SET SESSION optimizer_switch=DEFAULT;
DROP TABLE t1;
SET SESSION STORAGE_ENGINE=DEFAULT;
--disable_warnings
DROP TABLE IF EXISTS t1,t2,t3,t4;
DROP DATABASE IF EXISTS world;
--enable_warnings
set names utf8;
CREATE DATABASE world;
use world;
--source include/world_schema.inc
--disable_query_log
--disable_result_log
--disable_warnings
--source include/world.inc
--enable_warnings
--enable_result_log
--enable_query_log
SELECT COUNT(*) FROM Country;
SELECT COUNT(*) FROM City;
SELECT COUNT(*) FROM CountryLanguage;
CREATE INDEX Name ON City(Name);
--disable_query_log
--disable_result_log
--disable_warnings
ANALYZE TABLE City;
--enable_warnings
--enable_result_log
--enable_query_log
# The following 4 queries are added for code coverage
#the exptected # of rows differ on 32-bit and 64-bit platforms for innodb
--replace_column 9 4079
EXPLAIN
SELECT * FROM City
WHERE (Population >= 100000 OR Name LIKE 'P%' OR Population < 100000);
EXPLAIN
SELECT * FROM City
WHERE (Population >= 100000 OR Name LIKE 'P%') AND Country='CAN' OR
(Population < 100000 OR Name Like 'T%') AND Country='ARG';
EXPLAIN
SELECT * FROM City
WHERE Population < 200000 AND Name LIKE 'P%' AND
(Population > 300000 OR Name LIKE 'T%') AND
(Population < 100000 OR Name LIKE 'Pa%');
EXPLAIN
SELECT * FROM City
WHERE Population > 100000 AND Name LIKE 'Aba%' OR
Country IN ('CAN', 'ARG') AND ID < 3800 OR
Country < 'U' AND Name LIKE 'Zhu%' OR
ID BETWEEN 3800 AND 3810;
# The output of the next 3 commands tells us about selectivities
# of the conditions utilized in 2 queries following after them
EXPLAIN
SELECT * FROM City
WHERE (Population > 101000 AND Population < 115000);
EXPLAIN
SELECT * FROM City
WHERE (Population > 101000 AND Population < 103000);
EXPLAIN
SELECT * FROM City
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'));
# The pattern of the WHERE condition used in the following 2 queries is
# (range(key1) OR range(key2)) AND range(key3)
# Varying values of the constants in the second conjunct of the condition
# we can get either a plan with range index scan for key3 or a plan with
# an index merge retrieval over key2 and key3
EXPLAIN
SELECT * FROM City
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'))
AND (Population > 101000 AND Population < 115000);
EXPLAIN
SELECT * FROM City
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'))
AND (Population > 101000 AND Population < 103000);
# The following 4 queries check that the plans
# for the previous 2 plans are valid
SELECT * FROM City USE INDEX ()
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'))
AND (Population > 101000 AND Population < 115000);
SELECT * FROM City
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'))
AND (Population > 101000 AND Population < 115000);
SELECT * FROM City USE INDEX ()
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'))
AND (Population > 101000 AND Population < 103000);
SELECT * FROM City
WHERE ((Name > 'Ca' AND Name < 'Cf') OR (Country > 'E' AND Country < 'F'))
AND (Population > 101000 AND Population < 103000);
# The output of the next 7 commands tells us about selectivities
# of the conditions utilized in 4 queries following after them
EXPLAIN
SELECT * FROM City WHERE (Name < 'Ac');
EXPLAIN
SELECT * FROM City WHERE (Name < 'Bb');
EXPLAIN
SELECT * FROM City WHERE (Country > 'A' AND Country < 'B');
EXPLAIN
SELECT * FROM City WHERE (Name BETWEEN 'P' AND 'Pb');
EXPLAIN
SELECT * FROM City WHERE (Name BETWEEN 'P' AND 'S');
EXPLAIN
SELECT * FROM City WHERE (Population > 101000 AND Population < 110000);
EXPLAIN
SELECT * FROM City WHERE (Population > 103000 AND Population < 104000);
# The pattern of the WHERE condition used in the following 4 queries is
# (range1(key1) AND range(key2)) OR (range2(key1) AND range(key3)
# Varying values of the constants in the range conjuncts of the condition
# we can get:
# 1. a plan with range index over key1
# index merge retrievals over:
# 2. key1 and key3
# 3. key2 and key1
# 4. key2 and key3
EXPLAIN
SELECT * FROM City
WHERE (Name < 'Ac' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'Pb' AND (Population > 101000 AND Population < 110000));
EXPLAIN
SELECT * FROM City
WHERE (Name < 'Ac' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'S' AND (Population > 103000 AND Population < 104000));
EXPLAIN
SELECT * FROM City
WHERE (Name < 'Bb' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'Pb' AND (Population > 101000 AND Population < 110000));
EXPLAIN
SELECT * FROM City
WHERE (Name < 'Bb' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'S' AND (Population > 103000 AND Population < 104000));
# The following 8 queries check that the plans
# for the previous 4 plans are valid
SELECT * FROM City USE INDEX ()
WHERE (Name < 'Ac' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'Pb' AND (Population > 101000 AND Population < 110000));
SELECT * FROM City
WHERE (Name < 'Ac' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'Pb' AND (Population > 101000 AND Population < 110000));
SELECT * FROM City USE INDEX ()
WHERE (Name < 'Ac' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'S' AND (Population > 103000 AND Population < 104000));
SELECT * FROM City
WHERE (Name < 'Ac' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'S' AND (Population > 103000 AND Population < 104000));
SELECT * FROM City USE INDEX ()
WHERE (Name < 'Bb' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'Pb' AND (Population > 101000 AND Population < 110000));
SELECT * FROM City
WHERE (Name < 'Bb' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'Pb' AND (Population > 101000 AND Population < 110000));
SELECT * FROM City USE INDEX ()
WHERE (Name < 'Bb' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'S' AND (Population > 103000 AND Population < 104000));
SELECT * FROM City
WHERE (Name < 'Bb' AND (Country > 'A' AND Country < 'B')) OR
(Name BETWEEN 'P' AND 'S' AND (Population > 103000 AND Population < 104000));
# The output of the next 6 commands tells us about selectivities
# of the conditions utilized in 3 queries following after them
EXPLAIN
SELECT * FROM City WHERE (ID < 50) OR (ID BETWEEN 100 AND 110);
EXPLAIN
SELECT * FROM City WHERE (ID < 200) OR (ID BETWEEN 300 AND 600);
EXPLAIN
SELECT * FROM City WHERE (ID < 600) OR (ID BETWEEN 900 AND 1800);
EXPLAIN
SELECT * FROM City WHERE Country > 'A' AND Country < 'ARG';
EXPLAIN
SELECT * FROM City WHERE Name LIKE 'H%' OR Name LIKE 'P%' ;
EXPLAIN
SELECT * FROM City WHERE Name LIKE 'Ha%' OR Name LIKE 'Pa%' ;
# The pattern of the WHERE condition used in the following 3 queries is
# (range1(key1) AND (range1(key2) OR range(key3)) OR
# (range2(key1) AND (range2(key2) OR range(key4))
# Varying values of the constants in the range predicates of the condition
# we can get:
# 1. a plan with range index over key1
# 2. an index merge retrieval over key1, key2 and key3
EXPLAIN
SELECT * FROM City
WHERE ((ID < 50) AND (Name LIKE 'H%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 100 AND 110) AND
(Name LIKE 'P%' OR (Population > 103000 AND Population < 104000)));
EXPLAIN
SELECT * FROM City
WHERE ((ID < 800) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 900 AND 1800) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
EXPLAIN
SELECT * FROM City
WHERE ((ID < 600) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 300 AND 600) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
# The following 6 queries check that the plans
# for the previous 3 plans are valid
SELECT * FROM City USE INDEX ()
WHERE ((ID < 50) AND (Name LIKE 'H%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 100 AND 110) AND
(Name LIKE 'P%' OR (Population > 103000 AND Population < 104000)));
SELECT * FROM City
WHERE ((ID < 50) AND (Name LIKE 'H%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 100 AND 110) AND
(Name LIKE 'P%' OR (Population > 103000 AND Population < 104000)));
SELECT * FROM City USE INDEX()
WHERE ((ID < 800) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 900 AND 1800) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
SELECT * FROM City
WHERE ((ID < 800) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 900 AND 1800) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
SELECT * FROM City USE INDEX ()
WHERE ((ID < 200) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 300 AND 600) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
SELECT * FROM City
WHERE ((ID < 200) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 300 AND 600) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
# The output of the next 8 commands tells us about selectivities
# of the conditions utilized in 2 queries following after them
EXPLAIN
SELECT * FROM City WHERE Population > 101000 AND Population < 102000;
EXPLAIN
SELECT * FROM City WHERE Population > 101000 AND Population < 110000;
EXPLAIN
SELECT * FROM City WHERE Country < 'C';
EXPLAIN
SELECT * FROM City WHERE Country < 'AGO';
EXPLAIN
SELECT * FROM City WHERE Name BETWEEN 'P' AND 'S';
EXPLAIN
SELECT * FROM City WHERE Name BETWEEN 'P' AND 'Pb';
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 3400 AND 3800;
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 3790 AND 3800;
EXPLAIN
SELECT * FROM City WHERE Name LIKE 'P%';
# The pattern of the WHERE condition used in the following 2 queries is
# (range(key1) AND (range1(key2) OR range1(key3)) OR
# (range(key4) AND (range2(key2) OR range2(key3))
# Varying values of the constants in the range predicates of the condition
# we can get:
# index merge retrievals over:
# 1. key1, key2 and key3
# 2. key4, key2 and key3
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 102000) AND
(Country < 'C' OR Name BETWEEN 'P' AND 'S')) OR
((ID BETWEEN 3400 AND 3800) AND
(Country < 'AGO' OR Name LIKE 'Pa%'));
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 110000) AND
(Country < 'AGO' OR Name BETWEEN 'P' AND 'Pb')) OR
((ID BETWEEN 3790 AND 3800) AND
(Country < 'C' OR Name LIKE 'P%'));
# The following 4 queries check that the plans
# for the previous 2 plans are valid
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 102000) AND
(Country < 'C' OR Name BETWEEN 'P' AND 'S')) OR
((ID BETWEEN 3400 AND 3800) AND
(Country < 'AGO' OR Name LIKE 'Pa%'));
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 102000) AND
(Country < 'C' OR Name BETWEEN 'P' AND 'S')) OR
((ID BETWEEN 3400 AND 3800) AND
(Country < 'AGO' OR Name LIKE 'Pa%'));
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 110000) AND
(Country < 'AGO' OR Name BETWEEN 'P' AND 'Pb')) OR
((ID BETWEEN 3790 AND 3800) AND
(Country < 'C' OR Name LIKE 'P%'));
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 110000) AND
(Country < 'AGO' OR Name BETWEEN 'P' AND 'Pb')) OR
((ID BETWEEN 3790 AND 3800) AND
(Country < 'C' OR Name LIKE 'P%'));
CREATE INDEX CountryPopulation ON City(Country,Population);
--disable_query_log
--disable_result_log
--disable_warnings
ANALYZE TABLE City;
--enable_warnings
--enable_result_log
--enable_query_log
# The output of the next 4 commands tells us about selectivities
# of the conditions utilized in 2 queries following after them
EXPLAIN
SELECT * FROM City WHERE Name LIKE 'Pas%';
EXPLAIN
SELECT * FROM City WHERE Name LIKE 'P%';
EXPLAIN
SELECT * FROM City WHERE (Population > 101000 AND Population < 103000);
EXPLAIN
SELECT * FROM City WHERE Country='USA';
# The pattern of the WHERE condition used in the following 3 queries is
# (range(key1_p2) OR (range(key2)) AND key1_p1=c
# Varying values of the constants in the range predicates of the condition
# we can get:
# 1. a plan with range index over key1_p1
# 2. an index merge retrieval over: key1 and key2
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 103000) OR Name LIKE 'Pas%')
AND Country='USA';
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 103000) OR Name LIKE 'P%')
AND Country='USA';
# The following 4 queries check that the plans
# for the previous 2 plans are valid
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 103000) OR Name LIKE 'Pas%')
AND Country='USA';
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 103000) OR Name LIKE 'Pas%')
AND Country='USA';
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 103000) OR Name LIKE 'P%')
AND Country='USA';
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 103000) OR Name LIKE 'P%')
AND Country='USA';
CREATE INDEX CountryName ON City(Country,Name);
--disable_query_log
--disable_result_log
--disable_warnings
ANALYZE TABLE City;
--enable_warnings
--enable_result_log
--enable_query_log
# The output of the next 11 commands tells us about selectivities
# of the conditions utilized in 3 queries following after them
EXPLAIN
SELECT * FROM City WHERE Country='USA';
EXPLAIN
SELECT * FROM City WHERE Country='BRA';
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 3790 AND 3800;
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 4025 AND 4035;
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 4028 AND 4032;
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 3500 AND 3800;
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 4000 AND 4300;
EXPLAIN
SELECT * FROM City WHERE ID BETWEEN 250 and 260 ;
EXPLAIN
SELECT * FROM City WHERE (Population > 101000 AND Population < 102000);
EXPLAIN
SELECT * FROM City WHERE (Population > 101000 AND Population < 103000);
EXPLAIN
SELECT * FROM City WHERE Name LIKE 'Pa%';
# The pattern of the WHERE condition used in the following 3 queries is
# (range(key1_p2) OR range1(key3)) AND
# range(key1|2_p1=c) AND
# (range(key2_p2) OR range2(key3))
# Varying values of the constants in the range conjuncts of the condition
# we can get:
# 1. a plan with range index over key1|2_p1
# index merge retrievals over:
# 2. key1 and key3
# 3. key2 and key3
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4025 AND 4035);
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 103000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4028 AND 4032);
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 110000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name BETWEEN 'P' AND 'T' OR ID BETWEEN 4000 AND 4300);
# The following 6 queries check that the plans
# for the previous 3 plans are valid
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4025 AND 4035);
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4025 AND 4035);
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4028 AND 4032);
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4028 AND 4032);
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4025 AND 4035);
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
AND (Name LIKE 'Pa%' OR ID BETWEEN 4025 AND 4035);
# The pattern of the WHERE condition used in the following query is
# (range(key1_p2) OR range1(key3)) AND range(key1|2_p1=c1) AND
# (range(key2_p2) OR range1(key3)) AND range(key1|2_p1=c2)
# We get an index merge retrieval over key1, key2 and key3 for it
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 and Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
OR (Name LIKE 'Pa%' OR ID BETWEEN 250 AND 260) AND Country='BRA';
# The following 2 queries check that the plans
# for the previous plan is valid
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 and Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
OR (Name LIKE 'Pa%' OR ID BETWEEN 250 AND 260) AND Country='BRA';
SELECT * FROM City
WHERE ((Population > 101000 and Population < 102000) OR
ID BETWEEN 3790 AND 3800) AND Country='USA'
OR (Name LIKE 'Pa%' OR ID BETWEEN 250 AND 260) AND Country='BRA';
# The pattern of the WHERE condition used in the following query is
# (impossible_range(key1_p2) OR range1(key3)) AND
# range(key1|2_p1=c1) AND
# (range(key2_p2) OR range2(key3))
# where range1(key3) and range2(key3) are disjoint
# Varying values of the constant in range predicates we get plans:
# 1. with an index scan over key2
# 2. with an index scan over key4=key2_p2
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 11000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name LIKE 'P%' OR ID BETWEEN 4000 AND 4300);
EXPLAIN
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 11000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name LIKE 'Pho%' OR ID BETWEEN 4000 AND 4300);
# The following 4 queries check that the plans
# for the previous 2 plans are valid
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 11000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name LIKE 'P%' OR ID BETWEEN 4000 AND 4300);
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 11000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name LIKE 'P%' OR ID BETWEEN 4000 AND 4300);
SELECT * FROM City USE INDEX ()
WHERE ((Population > 101000 AND Population < 11000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name LIKE 'Pho%' OR ID BETWEEN 4000 AND 4300);
SELECT * FROM City
WHERE ((Population > 101000 AND Population < 11000) OR
ID BETWEEN 3500 AND 3800) AND Country='USA'
AND (Name LIKE 'Pho%' OR ID BETWEEN 4000 AND 4300);
DROP INDEX Population ON City;
DROP INDEX Name ON City;
# The pattern of the WHERE condition used in the following query is
# (key1|2_p1=c AND range(key1_p2)) OR (key1|2_p1=c AND range(key2_p2))
# We get an index merge retrieval over key1, key2 for it
EXPLAIN
SELECT * FROM City
WHERE Country='USA' AND Population BETWEEN 101000 AND 102000 OR
Country='USA' AND Name LIKE 'Pa%';
# The following 2 queries check that the plans
# for the previous plan is valid
SELECT * FROM City USE INDEX()
WHERE Country='USA' AND Population BETWEEN 101000 AND 102000 OR
Country='USA' AND Name LIKE 'Pa%';
SELECT * FROM City
WHERE Country='USA' AND Population BETWEEN 101000 AND 102000 OR
Country='USA' AND Name LIKE 'Pa%';
# The pattern of the WHERE condition used in the following query is
# key1|2_p1=c AND (range(key1_p2) OR range(key2_p2))
# We get an index merge retrieval over key1, key2 for it
EXPLAIN
SELECT * FROM City
WHERE Country='USA' AND
(Population BETWEEN 101000 AND 102000 OR Name LIKE 'Pa%');
# The following 2 queries check that the plans
# for the previous plan is valid
SELECT * FROM City
WHERE Country='USA' AND
(Population BETWEEN 101000 AND 102000 OR Name LIKE 'Pa%');
SELECT * FROM City
WHERE Country='USA' AND
(Population BETWEEN 101000 AND 102000 OR Name LIKE 'Pa%');
DROP DATABASE world;
use test;
#
# Bug #17259: a bad range scan and a good index merge plan
#
CREATE TABLE t1 (
id int(10) unsigned NOT NULL auto_increment,
account_id int(10) unsigned NOT NULL,
first_name varchar(50) default NULL,
middle_name varchar(50) default NULL,
last_name varchar(100) default NULL,
home_address_1 varchar(150) default NULL,
home_city varchar(75) default NULL,
home_state char(2) default NULL,
home_postal_code varchar(50) default NULL,
home_county varchar(75) default NULL,
home_country char(3) default NULL,
work_address_1 varchar(150) default NULL,
work_city varchar(75) default NULL,
work_state char(2) default NULL,
work_postal_code varchar(50) default NULL,
work_county varchar(75) default NULL,
work_country char(3) default NULL,
login varchar(50) NOT NULL,
PRIMARY KEY (id),
KEY login (login,account_id),
KEY account_id (account_id),
KEY user_home_country_indx (home_country),
KEY user_work_country_indx (work_country),
KEY user_home_state_indx (home_state),
KEY user_work_state_indx (work_state),
KEY user_home_city_indx (home_city),
KEY user_work_city_indx (work_city),
KEY user_first_name_indx (first_name),
KEY user_last_name_indx (last_name)
);
insert into t1(account_id, login, home_state, work_state) values
(1, 'pw', 'ia', 'ia'), (1, 'pw', 'ia', 'ia'), (1, 'pw', 'ia', 'ia'),
(1, 'pw', 'ia', 'ia'), (1, 'pw', 'ia', 'ia'), (1, 'pw', 'ia', 'ia');
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
insert into t1(account_id, login, home_state, work_state)
select 1, 'pw', 'ak', 'ak' from t1;
analyze table t1;
select count(*) from t1 where account_id = 1;
select * from t1
where (home_state = 'ia' or work_state='ia') and account_id = 1;
explain
select * from t1
where (home_state = 'ia' or work_state='ia') and account_id = 1;
drop table t1;
#
# Bug #17673: no index merge plan if the condition for the last used
# index component is factored out of the or formula
#
CREATE TABLE t1 (
c1 int(11) NOT NULL auto_increment,
c2 decimal(10,0) default NULL,
c3 decimal(10,0) default NULL,
c4 decimal(10,0) default NULL,
c5 decimal(10,0) default NULL,
cp decimal(1,0) default NULL,
ce decimal(10,0) default NULL,
cdata char(20),
PRIMARY KEY (c1),
KEY k1 (c2,c3,cp,ce),
KEY k2 (c4,c5,cp,ce)
);
insert into t1 (c2, c3, c4, c5, cp) values(1,1,1,1,1);
insert into t1 (c2, c3, c4, c5, cp) values(2,1,1,1,4);
insert into t1 (c2, c3, c4, c5, cp) values(2,1,2,1,1);
insert into t1 (c2, c3, c4, c5, cp) values(2,1,3,1,4);
insert into t1 (c2, c3, c4, c5, cp) values(3,1,4,1,4);
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
insert into t1 (c2, c3, c4, c5, cp)
select c2, c3, c4, c5, cp from t1 where cp = 4;
analyze table t1;
explain
select * from t1 where (c2=1 and c3=1) or (c4=2 and c5=1);
explain
select * from t1
where (c2=1 and c3=1 and cp=1) or (c4=2 and c5=1 and cp=1);
explain
select * from t1
where ((c2=1 and c3=1) or (c4=2 and c5=1)) and cp=1;
select * from t1
where (c2=1 and c3=1 and cp=1) or (c4=2 and c5=1 and cp=1);
select * from t1
where ((c2=1 and c3=1) or (c4=2 and c5=1)) and cp=1;
drop table t1;
#
# Bug #23322: a bad range scan and a good index merge plan
#
create table t1 (
c1 int auto_increment primary key,
c2 char(20),
c3 char (20),
c4 int
);
alter table t1 add key k1 (c2);
alter table t1 add key k2 (c3);
alter table t1 add key k3 (c4);
insert into t1 values(null, 'a', 'b', 0);
insert into t1 values(null, 'c', 'b', 0);
insert into t1 values(null, 'a', 'd', 0);
insert into t1 values(null, 'ccc', 'qqq', 0);
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3) select c2,c3 from t1 where c2 != 'a';
insert into t1 (c2,c3,c4) select c2,c3,1 from t1 where c2 != 'a';
insert into t1 (c2,c3,c4) select c2,c3,2 from t1 where c2 != 'a';
insert into t1 (c2,c3,c4) select c2,c3,3 from t1 where c2 != 'a';
insert into t1 (c2,c3,c4) select c2,c3,4 from t1 where c2 != 'a';
analyze table t1;
select count(*) from t1 where (c2='e' OR c3='q');
select count(*) from t1 where c4 != 0;
explain
select distinct c1 from t1 where (c2='e' OR c3='q');
explain
select distinct c1 from t1 where (c4!= 0) AND (c2='e' OR c3='q');
drop table t1;
#
# Bug #30151: a bad range scan and a good index merge plan
#
create table t1 (
id int unsigned auto_increment primary key,
c1 char(12),
c2 char(15),
c3 char(1)
);
insert into t1 (c3) values ('1'), ('2');
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
insert into t1 (c3) select c3 from t1;
update t1 set c1=lpad(id+1000, 12, ' '), c2=lpad(id+10000, 15, ' ');
alter table t1 add unique index (c1), add unique index (c2), add index (c3);
analyze table t1;
explain
select * from t1 where (c1=' 100000' or c2=' 2000000');
explain
select * from t1 where (c1=' 100000' or c2=' 2000000') and c3='2';
select * from t1 where (c1=' 100000' or c2=' 2000000');
select * from t1 where (c1=' 100000' or c2=' 2000000') and c3='2';
drop table t1;
#
# Bug #637978: invalid index merge access plan causes to wrong results
#
CREATE TABLE t1 (
a smallint DEFAULT NULL,
pk int NOT NULL AUTO_INCREMENT PRIMARY KEY,
b varchar(10) DEFAULT NULL,
c varchar(64) DEFAULT NULL,
INDEX idx1 (a),
INDEX idx2 (b),
INDEX idx3 (c)
);
--disable_query_log
--disable_result_log
INSERT INTO t1 VALUES
(30371,99001,'dl','e'),(3,99002,'Ohio','t'),(9,99003,'Delaware','xb'),
(0,99004,'Pennsylvan','i'),(-199,99005,'y','d'),(0,99006,'with','Rhode Island'),
(3,99007,'km','qkmiimdxbdljsejtsfrvwlrgacinbmfuosflnenlpomkmvbig'),
(22860,99008,'ovqkmiimdx','uovqkmiimdxbdljsejtsfrvwlrgacinbmfuosflnenlpomkmvbig'),
(212,99009,'f','p'),(NULL,99010,'i','k'),(20426,99011,'Vermont','New York'),
(0,99012,'Oregon','w'),(31831,99013,'s','isrcijpuovqkmiimdxbdljsejtsfrvwl'),
(123,99014,'t','p'),(32767,99015,'q','Maine'),
(NULL,99016,'know','qqqpisrcijpuovqkmiimdxbdljsejtsfrvwlrgacinbmfuosflnenlpo'),
(1,99017,'going','North Carolina'),(-717,99018,'ad','Indiana'),
(32767,99019,'Maryland','aa'),(31280,99020,'Nebraska','Colorado'),
(0,99021,'q','Ohio'),
(5989,99022,'rovaadtqqq','lrovaadtqqqpisrcijpuovqkmiimdxbdljsejtsfrvwlrgacinb'),
(89,99023,'n','Pennsylvania'),(0,99024,'Florida','c'),(97,99025,'Maine','y'),
(149,99026,'xaemnl','Idaho'),(NULL,99027,'h','y'),(26276,99028,'going','New York'),
(242,99029,'bdhxaemnlr','sbdhxaemnlrovaadtqqqpisrcijpuovqkmiimdxb'),
(32767,99030,'if','a'),(26581,99031,'Arizona','q'),(45,99032,'ysazsbdhxa','f'),
(0,99033,'qv','s'),(NULL,99034,'Louisiana','lqvfysazsbdhxaemnlrovaadtqqqpisrc'),
(160,99035,'Connecticu','x'),(23241,99036,'lx','q'),(0,99037,'u','Colorado'),
(-19141,99038,'w','h'),(218,99039,'s','uo'),(4,99040,'Montana','Oklahoma'),
(97,99041,'r','ls'),(32767,99042,'q','v'),(7,99043,'mlsuownlnl','did'),
(NULL,99044,'ui','i'),(2,99045,'to','I\'ll'),(0,99046,'Nevada','g'),
(3251,99047,'y','New York'),(0,99048,'wyttuimlsu','you\'re'),
(7,99049,'he','South Carolina'),(32767,99050,'s','right'),
(172,99051,'Arizona','e'),(0,99052,'x','lxmvwyttuimlsuownlnlxklq'),
(NULL,99053,'f','wfjlxmvwyttuimlsuownlnlxklqvfysazs'),(44,99054,'s','n'),
(-17561,99055,'me','wm'),(88,99056,'y','my'),(7313,99057,'jx','New Hampshire'),
(63,99058,'zl','South Carolina'),(9,99059,'ma','Illinois'),
(6,99060,'lamazljxpg','like'),(17021,99061,'x','v'),(0,99062,'New Mexico','j'),
(179,99427,'fliq','because'),
(107,99063,'Virginia','Mississippi'),
(0,99064,'si','to'),(113,99065,'Illinois','Kansas'),(20808,99066,'tsi','d'),
(-15372,99067,'d','vdftsidjtvulamazljxpgiwmbnmwfjlxmvwyttuimlsuownlnl'),
(0,99068,'y','then'),(2,99069,'all','b'),(NULL,99070,'by','Wisconsin'),
(4,99071,'about','right'),(5,99072,'m','s'),(0,99073,'e','Pennsylvania'),
(-28284,99074,'x','f'),(1,99075,'Rhode Isla','Georgia'),(NULL,99076,'p','was'),
(168,99077,'Tennessee','Minnesota'),(18349,99078,'x','Rhode Island'),
(5,99079,'as','d'),(12217,99080,'c','i'),(0,99081,'rdvdxboydm','s'),
(19132,99082,'her','jerdvdxboydmpefbiesqbyyvdftsidjtvulamazljxpgiwmbn'),
(0,99083,'all','jhjerdvdxboydmpefbiesqbyyvdftsidjtvulamazljx'),
(32767,99084,'s','flj'),(-4947,99085,'something','Vermont'),
(0,99086,'cjfljhjerd','Washington');
--enable_query_log
--enable_result_log
SELECT COUNT(*) FROM t1 IGNORE INDEX (idx2,idx3)
WHERE c = 'i' OR b IN ( 'Arkansas' , 'd' , 'pdib' , 'can' ) OR
(pk BETWEEN 120 AND 79 + 255 OR a IN ( 4 , 179 , 1 ) ) AND a > 8 ;
SELECT COUNT(*) FROM t1
WHERE c = 'i' OR b IN ( 'Arkansas' , 'd' , 'pdib' , 'can' ) OR
(pk BETWEEN 120 AND 79 + 255 OR a IN ( 4 , 179 , 1 ) ) AND a > 8 ;
EXPLAIN
SELECT COUNT(*) FROM t1
WHERE c = 'i' OR b IN ( 'Arkansas' , 'd' , 'pdib' , 'can' ) OR
(pk BETWEEN 120 AND 79 + 255 OR a IN ( 4 , 179 , 1 ) ) AND a > 8 ;
DROP TABLE t1;
#
# Bug #684117: ORing of two index merge that caused a crash
#
CREATE TABLE t1 (
f1 int, f2 int, f3 int, f4 int, f5 int,
PRIMARY KEY (f4), KEY (f1), KEY (f2), KEY (f3)
) ;
INSERT INTO t1 VALUES (0,0,NULL,9,5), (0,0,1,9425,NULL);
SELECT f5 FROM t1
WHERE f2 != 1 OR f1 IS NULL OR f4 = 4 OR
f2 AND (f4 BETWEEN 6 AND 255 OR f3 IS NULL);
DROP TABLE t1;
#
# Bug #685952: An invalid index merge union plan
#
CREATE TABLE t1 (
f1 int, f2 int, f3 int, f4 int,
PRIMARY KEY (f1), KEY (f3), KEY (f4)
);
INSERT INTO t1 VALUES (9,0,2,6), (9930,0,0,NULL);
SET SESSION optimizer_switch='index_merge_intersection=off';
SET SESSION optimizer_switch='index_merge_sort_union=off';
SET SESSION optimizer_switch='index_merge_union=off';
EXPLAIN
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
SET SESSION optimizer_switch='index_merge_union=on';
EXPLAIN
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
INSERT INTO t1 VALUES
(93,0,3,6), (9933,0,3,3), (94,0,4,6), (9934,0,4,4),
(95,0,5,6), (9935,0,5,5), (96,0,6,6), (9936,0,6,6),
(97,0,7,6), (9937,0,7,7), (98,0,8,6), (9938,0,8,8),
(99,0,9,6), (9939,0,9,9);
SET SESSION optimizer_switch='index_merge_union=off';
EXPLAIN
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
SET SESSION optimizer_switch='index_merge_union=on';
EXPLAIN
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
SELECT * FROM t1 FORCE KEY (PRIMARY,f3,f4)
WHERE ( f3 = 1 OR f1 = 7 ) AND f1 < 10
OR f3 BETWEEN 2 AND 2 AND ( f3 = 1 OR f4 != 1 );
SET SESSION optimizer_switch=DEFAULT;
DROP TABLE t1;
--source include/have_innodb.inc
SET SESSION STORAGE_ENGINE='InnoDB';
--source t/range_vs_index_merge.test
SET SESSION STORAGE_ENGINE=DEFAULT;
......@@ -261,6 +261,11 @@ public:
uint8 min_flag,max_flag,maybe_flag;
uint8 part; // Which key part
uint8 maybe_null;
/*
The ordinal number the least significant component encountered in
the ranges of the SEL_ARG tree (the first component has number 1)
*/
uint16 max_part_no;
/*
Number of children of this element in the RB-tree, plus 1 for this
element itself.
......@@ -497,6 +502,11 @@ public:
pos->increment_use_count(count);
}
}
void incr_refs()
{
increment_use_count(1);
use_count++;
}
void free_tree()
{
for (SEL_ARG *pos=first(); pos ; pos=pos->next)
......@@ -558,6 +568,99 @@ public:
class SEL_IMERGE;
#define CLONE_KEY1_MAYBE 1
#define CLONE_KEY2_MAYBE 2
#define swap_clone_flag(A) ((A & 1) << 1) | ((A & 2) >> 1)
/*
While objects of the class SEL_ARG represent ranges for indexes or
index infixes (including ranges for index prefixes and index suffixes),
objects of the class SEL_TREE represent AND/OR formulas of such ranges.
Currently an AND/OR formula represented by a SEL_TREE object can have
at most three levels:
<SEL_TREE formula> ::=
[ <SEL_RANGE_TREE formula> AND ]
[ <SEL_IMERGE formula> [ AND <SEL_IMERGE formula> ...] ]
<SEL_RANGE_TREE formula> ::=
<SEL_ARG formula> [ AND <SEL_ARG_formula> ... ]
<SEL_IMERGE formula> ::=
<SEL_RANGE_TREE formula> [ OR <SEL_RANGE_TREE formula> ]
As we can see from the above definitions:
- SEL_RANGE_TREE formula is a conjunction of SEL_ARG formulas
- SEL_IMERGE formula is a disjunction of SEL_RANGE_TREE formulas
- SEL_TREE formula is a conjunction of a SEL_RANGE_TREE formula
and SEL_IMERGE formulas.
It's required above that a SEL_TREE formula has at least one conjunct.
Usually we will consider normalized SEL_RANGE_TREE formulas where we use
TRUE as conjunct members for those indexes whose SEL_ARG trees are empty.
We will call an SEL_TREE object simply 'tree'.
The part of a tree that represents SEL_RANGE_TREE formula is called
'range part' of the tree while the remaining part is called 'imerge part'.
If a tree contains only a range part then we call such a tree 'range tree'.
Components of a range tree that represent SEL_ARG formulas are called ranges.
If a tree does not contain any range part we call such a tree 'imerge tree'.
Components of the imerge part of a tree that represent SEL_IMERGE formula
are called imerges.
Usually we'll designate:
SEL_TREE formulas by T_1,...,T_k
SEL_ARG formulas by R_1,...,R_k
SEL_RANGE_TREE formulas by RT_1,...,RT_k
SEL_IMERGE formulas by M_1,...,M_k
Accordingly we'll use:
t_1,...,t_k - to designate trees representing T_1,...,T_k
r_1,...,r_k - to designate ranges representing R_1,...,R_k
rt_1,...,r_tk - to designate range trees representing RT_1,...,RT_k
m_1,...,m_k - to designate imerges representing M_1,...,M_k
SEL_TREE objects are usually built from WHERE conditions or
ON expressions.
A SEL_TREE object always represents an inference of the condition it is
built from. Therefore, if a row satisfies a SEL_TREE formula it also
satisfies the condition it is built from.
The following transformations of tree t representing SEL_TREE formula T
yield a new tree t1 thar represents an inference of T: T=>T1.
(1) remove any of SEL_ARG tree from the range part of t
(2) remove any imerge from the tree t
(3) remove any of SEL_ARG tree from any range tree contained
in any imerge of tree
Since the basic blocks of any SEL_TREE objects are ranges, SEL_TREE
objects in many cases can be effectively used to filter out a big part
of table rows that do not satisfy WHERE/IN conditions utilizing
only single or multiple range index scans.
A single range index scan is constructed for a range tree that contains
only one SEL_ARG object for an index or an index prefix.
An index intersection scan can be constructed for a range tree
that contains several SEL_ARG objects. Currently index intersection
scans are constructed only for single-point ranges.
An index merge scan is constructed for a imerge tree that contains only
one imerge. If range trees of this imerge contain only single-point merges
than a union of index intersections can be built.
Usually the tree built by the range optimizer for a query table contains
more than one range in the range part, and additionally may contain some
imerges in the imerge part. The range optimizer evaluates all of them one
by one and chooses the range or the imerge that provides the cheapest
single or multiple range index scan of the table. According to rules
(1)-(3) this scan always filter out only those rows that do not satisfy
the query conditions.
For any condition the SEL_TREE object for it is built in a bottom up
manner starting from the range trees for the predicates. The tree_and
function builds a tree for any conjunction of formulas from the trees
for its conjuncts. The tree_or function builds a tree for any disjunction
of formulas from the trees for its disjuncts.
*/
class SEL_TREE :public Sql_alloc
{
......@@ -574,7 +677,7 @@ public:
keys_map.clear_all();
bzero((char*) keys,sizeof(keys));
}
SEL_TREE(SEL_TREE *arg, RANGE_OPT_PARAM *param);
SEL_TREE(SEL_TREE *arg, bool without_merges, RANGE_OPT_PARAM *param);
/*
Note: there may exist SEL_TREE objects with sel_tree->type=KEY and
keys[i]=0 for all i. (SergeyP: it is not clear whether there is any
......@@ -597,6 +700,9 @@ public:
struct st_ror_scan_info **ror_scans; /* list of ROR key scans */
struct st_ror_scan_info **ror_scans_end; /* last ROR scan */
/* Note that #records for each key scan is stored in table->quick_rows */
bool without_ranges() { return keys_map.is_clear_all(); }
bool without_imerges() { return merges.is_empty(); }
};
class RANGE_OPT_PARAM
......@@ -636,12 +742,13 @@ public:
uint real_keynr[MAX_KEY];
/* Number of SEL_ARG objects allocated by SEL_ARG::clone_tree operations */
uint alloced_sel_args;
KEY_PART *key[MAX_KEY]; /* First key parts of keys used in the query */
};
class PARAM : public RANGE_OPT_PARAM
{
public:
KEY_PART *key[MAX_KEY]; /* First key parts of keys used in the query */
ha_rows quick_rows[MAX_KEY];
longlong baseflag;
uint max_key_part, range_count;
......@@ -669,7 +776,7 @@ class TABLE_READ_PLAN;
class TRP_RANGE;
class TRP_ROR_INTERSECT;
class TRP_ROR_UNION;
class TRP_ROR_INDEX_MERGE;
class TRP_INDEX_MERGE;
class TRP_GROUP_MIN_MAX;
struct st_ror_scan_info;
......@@ -708,6 +815,10 @@ static
TABLE_READ_PLAN *get_best_disjunct_quick(PARAM *param, SEL_IMERGE *imerge,
double read_time);
static
TABLE_READ_PLAN *merge_same_index_scans(PARAM *param, SEL_IMERGE *imerge,
TRP_INDEX_MERGE *imerge_trp,
double read_time);
static
TRP_GROUP_MIN_MAX *get_best_group_min_max(PARAM *param, SEL_TREE *tree);
#ifndef DBUG_OFF
......@@ -719,11 +830,15 @@ static void print_ror_scans_arr(TABLE *table, const char *msg,
static void print_quick(QUICK_SELECT_I *quick, const key_map *needed_reg);
#endif
static SEL_TREE *tree_and(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2);
static SEL_TREE *tree_or(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2);
static SEL_TREE *tree_and(RANGE_OPT_PARAM *param,
SEL_TREE *tree1, SEL_TREE *tree2);
static SEL_TREE *tree_or(RANGE_OPT_PARAM *param,
SEL_TREE *tree1,SEL_TREE *tree2);
static SEL_ARG *sel_add(SEL_ARG *key1,SEL_ARG *key2);
static SEL_ARG *key_or(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2);
static SEL_ARG *key_and(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2,
static SEL_ARG *key_or(RANGE_OPT_PARAM *param,
SEL_ARG *key1, SEL_ARG *key2);
static SEL_ARG *key_and(RANGE_OPT_PARAM *param,
SEL_ARG *key1, SEL_ARG *key2,
uint clone_flag);
static bool get_range(SEL_ARG **e1,SEL_ARG **e2,SEL_ARG *root1);
bool get_quick_keys(PARAM *param,QUICK_RANGE_SELECT *quick,KEY_PART *key,
......@@ -734,21 +849,22 @@ static bool eq_tree(SEL_ARG* a,SEL_ARG *b);
static SEL_ARG null_element(SEL_ARG::IMPOSSIBLE);
static bool null_part_in_key(KEY_PART *key_part, const uchar *key,
uint length);
bool sel_trees_can_be_ored(SEL_TREE *tree1, SEL_TREE *tree2, RANGE_OPT_PARAM* param);
/*
SEL_IMERGE is a list of possible ways to do index merge, i.e. it is
a condition in the following form:
(t_1||t_2||...||t_N) && (next)
where all t_i are SEL_TREEs, next is another SEL_IMERGE and no pair
(t_i,t_j) contains SEL_ARGS for the same index.
static bool sel_trees_have_common_keys(SEL_TREE *tree1, SEL_TREE *tree2,
key_map *common_keys);
static void eliminate_single_tree_imerges(RANGE_OPT_PARAM *param,
SEL_TREE *tree);
static bool sel_trees_can_be_ored(RANGE_OPT_PARAM* param,
SEL_TREE *tree1, SEL_TREE *tree2,
key_map *common_keys);
static bool sel_trees_must_be_ored(RANGE_OPT_PARAM* param,
SEL_TREE *tree1, SEL_TREE *tree2,
key_map common_keys);
static int and_range_trees(RANGE_OPT_PARAM *param,
SEL_TREE *tree1, SEL_TREE *tree2,
SEL_TREE *result);
static bool remove_nonrange_trees(RANGE_OPT_PARAM *param, SEL_TREE *tree);
SEL_TREE contained in SEL_IMERGE always has merges=NULL.
This class relies on memory manager to do the cleanup.
*/
class SEL_IMERGE : public Sql_alloc
{
......@@ -766,23 +882,39 @@ public:
trees_next(trees),
trees_end(trees + PREALLOCED_TREES)
{}
SEL_IMERGE (SEL_IMERGE *arg, RANGE_OPT_PARAM *param);
SEL_IMERGE (SEL_IMERGE *arg, uint cnt, RANGE_OPT_PARAM *param);
int or_sel_tree(RANGE_OPT_PARAM *param, SEL_TREE *tree);
int or_sel_tree_with_checks(RANGE_OPT_PARAM *param, SEL_TREE *new_tree);
int or_sel_imerge_with_checks(RANGE_OPT_PARAM *param, SEL_IMERGE* imerge);
bool have_common_keys(RANGE_OPT_PARAM *param, SEL_TREE *tree);
int and_sel_tree(RANGE_OPT_PARAM *param, SEL_TREE *tree,
SEL_IMERGE *new_imerge);
int or_sel_tree_with_checks(RANGE_OPT_PARAM *param,
uint n_init_trees,
SEL_TREE *new_tree,
bool is_first_check_pass,
bool *is_last_check_pass);
int or_sel_imerge_with_checks(RANGE_OPT_PARAM *param,
uint n_init_trees,
SEL_IMERGE* imerge,
bool is_first_check_pass,
bool *is_last_check_pass);
};
/*
Add SEL_TREE to this index_merge without any checks,
Add a range tree to the range trees of this imerge
NOTES
This function implements the following:
(x_1||...||x_N) || t = (x_1||...||x_N||t), where x_i, t are SEL_TREEs
SYNOPSIS
or_sel_tree()
param Context info for the operation
tree SEL_TREE to add to this imerge
DESCRIPTION
The function just adds the range tree 'tree' to the range trees
of this imerge.
RETURN
0 - OK
-1 - Out of memory.
0 if the operation is success
-1 if the function runs out memory
*/
int SEL_IMERGE::or_sel_tree(RANGE_OPT_PARAM *param, SEL_TREE *tree)
......@@ -807,96 +939,304 @@ int SEL_IMERGE::or_sel_tree(RANGE_OPT_PARAM *param, SEL_TREE *tree)
/*
Perform OR operation on this SEL_IMERGE and supplied SEL_TREE new_tree,
combining new_tree with one of the trees in this SEL_IMERGE if they both
have SEL_ARGs for the same key.
Check if any of the range trees of this imerge intersects with a given tree
SYNOPSIS
or_sel_tree_with_checks()
param PARAM from SQL_SELECT::test_quick_select
new_tree SEL_TREE with type KEY or KEY_SMALLER.
have_common_keys()
param Context info for the function
tree SEL_TREE intersection with the imerge range trees is checked for
NOTES
This does the following:
(t_1||...||t_k)||new_tree =
either
= (t_1||...||t_k||new_tree)
or
= (t_1||....||(t_j|| new_tree)||...||t_k),
DESCRIPTION
The function checks whether there is any range tree rt_i in this imerge
such that there are some indexes for which ranges are defined in both
rt_i and the range part of the SEL_TREE tree.
To check this the function calls the function sel_trees_have_common_keys.
RETURN
TRUE if there are such range trees in this imerge
FALSE otherwise
*/
bool SEL_IMERGE::have_common_keys(RANGE_OPT_PARAM *param, SEL_TREE *tree)
{
for (SEL_TREE** or_tree= trees, **bound= trees_next;
or_tree != bound; or_tree++)
{
key_map common_keys;
if (sel_trees_have_common_keys(*or_tree, tree, &common_keys))
return TRUE;
}
return FALSE;
}
where t_i, y are SEL_TREEs.
new_tree is combined with the first t_j it has a SEL_ARG on common
key with. As a consequence of this, choice of keys to do index_merge
read may depend on the order of conditions in WHERE part of the query.
/*
Perform AND operation for this imerge and the range part of a tree
SYNOPSIS
and_sel_tree()
param Context info for the operation
tree SEL_TREE for the second operand of the operation
new_imerge OUT imerge for the result of the operation
DESCRIPTION
This function performs AND operation for this imerge m and the
range part of the SEL_TREE tree rt. In other words the function
pushes rt into this imerge. The resulting imerge is returned in
the parameter new_imerge.
If this imerge m represent the formula
RT_1 OR ... OR RT_k
then the resulting imerge of the function represents the formula
(RT_1 AND RT) OR ... OR (RT_k AND RT)
The function calls the function and_range_trees to construct the
range tree representing (RT_i AND RT).
NOTE
The function may return an empty imerge without any range trees.
This happens when each call of and_range_trees returns an
impossible range tree (SEL_TREE::IMPOSSIBLE).
Example: (key1 < 2 AND key2 > 10) AND (key1 > 4 OR key2 < 6).
RETURN
0 OK
1 One of the trees was combined with new_tree to SEL_TREE::ALWAYS,
and (*this) should be discarded.
-1 An error occurred.
0 if the operation is a success
-1 otherwise: there is not enough memory to perform the operation
*/
int SEL_IMERGE::or_sel_tree_with_checks(RANGE_OPT_PARAM *param, SEL_TREE *new_tree)
int SEL_IMERGE::and_sel_tree(RANGE_OPT_PARAM *param, SEL_TREE *tree,
SEL_IMERGE *new_imerge)
{
for (SEL_TREE** tree = trees;
tree != trees_next;
tree++)
for (SEL_TREE** or_tree= trees; or_tree != trees_next; or_tree++)
{
if (sel_trees_can_be_ored(*tree, new_tree, param))
SEL_TREE *res_or_tree= 0;
if (!(res_or_tree= new SEL_TREE()))
return (-1);
if (!and_range_trees(param, *or_tree, tree, res_or_tree))
{
*tree = tree_or(param, *tree, new_tree);
if (!*tree)
return 1;
if (((*tree)->type == SEL_TREE::MAYBE) ||
((*tree)->type == SEL_TREE::ALWAYS))
if (new_imerge->or_sel_tree(param, res_or_tree))
return (-1);
}
}
return 0;
}
/*
Perform OR operation on this imerge and the range part of a tree
SYNOPSIS
or_sel_tree_with_checks()
param Context info for the operation
n_trees Number of trees in this imerge to check for oring
tree SEL_TREE whose range part is to be ored
is_first_check_pass <=> the first call of the function for this imerge
is_last_check_pass OUT <=> no more calls of the function for this imerge
DESCRIPTION
The function performs OR operation on this imerge m and the range part
of the SEL_TREE tree rt. It always replaces this imerge with the result
of the operation.
The operation can be performed in two different modes: with
is_first_check_pass==TRUE and is_first_check_pass==FALSE, transforming
this imerge differently.
Given this imerge represents the formula
RT_1 OR ... OR RT_k:
1. In the first mode, when is_first_check_pass==TRUE :
1.1. If rt must be ored(see the function sel_trees_must_be_ored) with
some rt_j (there may be only one such range tree in the imerge)
then the function produces an imerge representing the formula
RT_1 OR ... OR (RT_j OR RT) OR ... OR RT_k,
where the tree for (RT_j OR RT) is built by oring the pairs
of SEL_ARG trees for the corresponding indexes
1.2. Otherwise the function produces the imerge representing the formula:
RT_1 OR ... OR RT_k OR RT.
2. In the second mode, when is_first_check_pass==FALSE :
2.1. For each rt_j in the imerge that can be ored (see the function
sel_trees_can_be_ored), but not must be ored, with rt the function
replaces rt_j for a range tree such that for each index for which
ranges are defined in both in rt_j and rt the tree contains the
result of oring of these ranges.
2.2. In other cases the function does not produce any imerge.
When is_first_check==TRUE the function returns FALSE in the parameter
is_last_check_pass if there is no rt_j such that rt_j can be ored with rt,
but, at the same time, it's not true that rt_j must be ored with rt.
When is_first_check==FALSE the function always returns FALSE in the
parameter is_last_check_pass.
RETURN
1 The result of oring of rt_j and rt that must be ored returns the
the range tree with type==SEL_TREE::ALWAYS
(in this case the imerge m should be discarded)
-1 The function runs out of memory
0 in all other cases
*/
int SEL_IMERGE::or_sel_tree_with_checks(RANGE_OPT_PARAM *param,
uint n_trees,
SEL_TREE *tree,
bool is_first_check_pass,
bool *is_last_check_pass)
{
bool was_ored= FALSE;
*is_last_check_pass= TRUE;
SEL_TREE** or_tree = trees;
for (uint i= 0; i < n_trees; i++, or_tree++)
{
SEL_TREE *result= 0;
key_map result_keys;
key_map ored_keys;
if (sel_trees_can_be_ored(param, *or_tree, tree, &ored_keys))
{
bool must_be_ored= sel_trees_must_be_ored(param, *or_tree, tree,
ored_keys);
if (must_be_ored == is_first_check_pass)
{
result_keys.clear_all();
result= *or_tree;
for (uint key_no= 0; key_no < param->keys; key_no++)
{
if (!ored_keys.is_set(key_no))
{
result->keys[key_no]= 0;
continue;
}
SEL_ARG *key1= (*or_tree)->keys[key_no];
SEL_ARG *key2= tree->keys[key_no];
key2->incr_refs();
if ((result->keys[key_no]= key_or(param, key1, key2)))
{
result_keys.set_bit(key_no);
#ifdef EXTRA_DEBUG
if (param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS)
{
key1= result->keys[key_no];
(key1)->test_use_count(key1);
}
#endif
}
}
}
else if(is_first_check_pass)
*is_last_check_pass= FALSE;
}
if (result)
{
if (result_keys.is_clear_all())
result->type= SEL_TREE::ALWAYS;
*is_last_check_pass= TRUE;
if ((result->type == SEL_TREE::MAYBE) ||
(result->type == SEL_TREE::ALWAYS))
return 1;
/* SEL_TREE::IMPOSSIBLE is impossible here */
result->keys_map= result_keys;
*or_tree= result;
if (is_first_check_pass)
return 0;
else
was_ored= TRUE;
}
}
if (was_ored)
return 0;
/* New tree cannot be combined with any of existing trees. */
return or_sel_tree(param, new_tree);
if (!*is_last_check_pass &&
!(tree= new SEL_TREE(tree, FALSE, param)))
return (-1);
return or_sel_tree(param, tree);
}
/*
Perform OR operation on this index_merge and supplied index_merge list.
Perform OR operation on this imerge and and another imerge
SYNOPSIS
or_sel_imerge_with_checks()
param Context info for the operation
n_trees Number of trees in this imerge to check for oring
imerge The second operand of the operation
is_first_check_pass <=> the first call of the function for this imerge
is_last_check_pass OUT <=> no more calls of the function for this imerge
DESCRIPTION
For each range tree rt from 'imerge' the function calls the method
SEL_IMERGE::or_sel_tree_with_checks that performs OR operation on this
SEL_IMERGE object m and the tree rt. The mode of the operation is
specified by the parameter is_first_check_pass. Each call of
SEL_IMERGE::or_sel_tree_with_checks transforms this SEL_IMERGE object m.
The function returns FALSE in the prameter is_last_check_pass if
at least one of the calls of SEL_IMERGE::or_sel_tree_with_checks
returns FALSE as the value of its last parameter.
RETURN
0 - OK
1 - One of conditions in result is always TRUE and this SEL_IMERGE
should be discarded.
-1 - An error occurred
1 One of the calls of SEL_IMERGE::or_sel_tree_with_checks returns 1.
(in this case the imerge m should be discarded)
-1 The function runs out of memory
0 in all other cases
*/
int SEL_IMERGE::or_sel_imerge_with_checks(RANGE_OPT_PARAM *param, SEL_IMERGE* imerge)
{
for (SEL_TREE** tree= imerge->trees;
tree != imerge->trees_next;
tree++)
{
if (or_sel_tree_with_checks(param, *tree))
return 1;
int SEL_IMERGE::or_sel_imerge_with_checks(RANGE_OPT_PARAM *param,
uint n_trees,
SEL_IMERGE* imerge,
bool is_first_check_pass,
bool *is_last_check_pass)
{
*is_last_check_pass= TRUE;
SEL_TREE** tree= imerge->trees;
SEL_TREE** tree_end= imerge->trees_next;
for ( ; tree < tree_end; tree++)
{
uint rc;
bool is_last= TRUE;
rc= or_sel_tree_with_checks(param, n_trees, *tree,
is_first_check_pass, &is_last);
if (!is_last)
*is_last_check_pass= FALSE;
if (rc)
return rc;
}
return 0;
}
SEL_TREE::SEL_TREE(SEL_TREE *arg, RANGE_OPT_PARAM *param): Sql_alloc()
/*
Copy constructor for SEL_TREE objects
SYNOPSIS
SEL_TREE
arg The source tree for the constructor
without_merges <=> only the range part of the tree arg is copied
param Context info for the operation
DESCRIPTION
The constructor creates a full copy of the SEL_TREE arg if
the prameter without_merges==FALSE. Otherwise a tree is created
that contains the copy only of the range part of the tree arg.
*/
SEL_TREE::SEL_TREE(SEL_TREE *arg, bool without_merges,
RANGE_OPT_PARAM *param): Sql_alloc()
{
keys_map= arg->keys_map;
type= arg->type;
for (int idx= 0; idx < MAX_KEY; idx++)
for (uint idx= 0; idx < param->keys; idx++)
{
if ((keys[idx]= arg->keys[idx]))
keys[idx]->increment_use_count(1);
keys[idx]->incr_refs();
}
if (without_merges)
return;
List_iterator<SEL_IMERGE> it(arg->merges);
for (SEL_IMERGE *el= it++; el; el= it++)
{
SEL_IMERGE *merge= new SEL_IMERGE(el, param);
SEL_IMERGE *merge= new SEL_IMERGE(el, 0, param);
if (!merge || merge->trees == merge->trees_next)
{
merges.empty();
......@@ -907,7 +1247,23 @@ SEL_TREE::SEL_TREE(SEL_TREE *arg, RANGE_OPT_PARAM *param): Sql_alloc()
}
SEL_IMERGE::SEL_IMERGE (SEL_IMERGE *arg, RANGE_OPT_PARAM *param) : Sql_alloc()
/*
Copy constructor for SEL_IMERGE objects
SYNOPSIS
SEL_IMERGE
arg The source imerge for the constructor
cnt How many trees from arg are to be copied
param Context info for the operation
DESCRIPTION
The cnt==0 then the constructor creates a full copy of the
imerge arg. Otherwise only the first cnt trees of the imerge
are copied.
*/
SEL_IMERGE::SEL_IMERGE(SEL_IMERGE *arg, uint cnt,
RANGE_OPT_PARAM *param) : Sql_alloc()
{
uint elements= (arg->trees_end - arg->trees);
if (elements > PREALLOCED_TREES)
......@@ -919,13 +1275,13 @@ SEL_IMERGE::SEL_IMERGE (SEL_IMERGE *arg, RANGE_OPT_PARAM *param) : Sql_alloc()
else
trees= &trees_prealloced[0];
trees_next= trees;
trees_next= trees + (cnt ? cnt : arg->trees_next-arg->trees);
trees_end= trees + elements;
for (SEL_TREE **tree = trees, **arg_tree= arg->trees; tree < trees_end;
for (SEL_TREE **tree = trees, **arg_tree= arg->trees; tree < trees_next;
tree++, arg_tree++)
{
if (!(*tree= new SEL_TREE(*arg_tree, param)))
if (!(*tree= new SEL_TREE(*arg_tree, FALSE, param)))
goto mem_err;
}
......@@ -939,7 +1295,19 @@ mem_err:
/*
Perform AND operation on two index_merge lists and store result in *im1.
Perform AND operation on two imerge lists
SYNOPSIS
imerge_list_and_list()
param Context info for the operation
im1 The first imerge list for the operation
im2 The second imerge list for the operation
DESCRIPTION
The function just appends the imerge list im2 to the imerge list im1
RETURN VALUE
none
*/
inline void imerge_list_and_list(List<SEL_IMERGE> *im1, List<SEL_IMERGE> *im2)
......@@ -949,73 +1317,242 @@ inline void imerge_list_and_list(List<SEL_IMERGE> *im1, List<SEL_IMERGE> *im2)
/*
Perform OR operation on 2 index_merge lists, storing result in first list.
Perform OR operation on two imerge lists
NOTES
The following conversion is implemented:
(a_1 &&...&& a_N)||(b_1 &&...&& b_K) = AND_i,j(a_i || b_j) =>
=> (a_1||b_1).
i.e. all conjuncts except the first one are currently dropped.
This is done to avoid producing N*K ways to do index_merge.
If (a_1||b_1) produce a condition that is always TRUE, NULL is returned
and index_merge is discarded (while it is actually possible to try
harder).
SYNOPSIS
imerge_list_or_list()
param Context info for the operation
im1 The first imerge list for the operation
im2 The second imerge list for the operation
As a consequence of this, choice of keys to do index_merge read may depend
on the order of conditions in WHERE part of the query.
DESCRIPTION
Assuming that the first imerge list represents the formula
F1= M1_1 AND ... AND M1_k1
while the second imerge list represents the formula
F2= M2_1 AND ... AND M2_k2,
where M1_i= RT1_i_1 OR ... OR RT1_i_l1i (i in [1..k1])
and M2_i = RT2_i_1 OR ... OR RT2_i_l2i (i in [1..k2]),
the function builds a list of imerges for some formula that can be
inferred from the formula (F1 OR F2).
More exactly the function builds imerges for the formula (M1_1 OR M2_1).
Note that
(F1 OR F2) = (M1_1 AND ... AND M1_k1) OR (M2_1 AND ... AND M2_k2) =
AND (M1_i OR M2_j) (i in [1..k1], j in [1..k2]) =>
M1_1 OR M2_1.
So (M1_1 OR M2_1) is indeed an inference formula for (F1 OR F2).
To build imerges for the formula (M1_1 OR M2_1) the function invokes,
possibly twice, the method SEL_IMERGE::or_sel_imerge_with_checks
for the imerge m1_1.
At its first invocation the method SEL_IMERGE::or_sel_imerge_with_checks
performs OR operation on the imerge m1_1 and the range tree rt2_1_1 by
calling SEL_IMERGE::or_sel_tree_with_checks with is_first_pass_check==TRUE.
The resulting imerge of the operation is ored with the next range tree of
the imerge m2_1. This oring continues until the last range tree from
m2_1 has been ored.
At its second invocation the method SEL_IMERGE::or_sel_imerge_with_checks
performs the same sequence of OR operations, but now calling
SEL_IMERGE::or_sel_tree_with_checks with is_first_pass_check==FALSE.
The imerges that the operation produces replace those in the list im1
RETURN
0 OK, result is stored in *im1
other Error, both passed lists are unusable
0 if the operation is a success
-1 if the function has run out of memory
*/
int imerge_list_or_list(RANGE_OPT_PARAM *param,
List<SEL_IMERGE> *im1,
List<SEL_IMERGE> *im2)
{
uint rc;
bool is_last_check_pass= FALSE;
SEL_IMERGE *imerge= im1->head();
uint elems= imerge->trees_next-imerge->trees;
im1->empty();
im1->push_back(imerge);
return imerge->or_sel_imerge_with_checks(param, im2->head());
rc= imerge->or_sel_imerge_with_checks(param, elems, im2->head(),
TRUE, &is_last_check_pass);
if (rc)
{
if (rc == 1)
{
im1->empty();
rc= 0;
}
return rc;
}
if (!is_last_check_pass)
{
SEL_IMERGE* new_imerge= new SEL_IMERGE(imerge, elems, param);
if (new_imerge)
{
is_last_check_pass= TRUE;
rc= new_imerge->or_sel_imerge_with_checks(param, elems, im2->head(),
FALSE, &is_last_check_pass);
if (!rc)
im1->push_back(new_imerge);
}
}
return rc;
}
/*
Perform OR operation on index_merge list and key tree.
Perform OR operation for each imerge from a list and the range part of a tree
SYNOPSIS
imerge_list_or_tree()
param Context info for the operation
merges The list of imerges to be ored with the range part of tree
tree SEL_TREE whose range part is to be ored with the imerges
DESCRIPTION
For each imerge mi from the list 'merges' the function performes OR
operation with mi and the range part of 'tree' rt, producing one or
two imerges.
Given the merge mi represent the formula RTi_1 OR ... OR RTi_k,
the function forms the merges by the following rules:
1. If rt cannot be ored with any of the trees rti the function just
produces an imerge that represents the formula
RTi_1 OR ... RTi_k OR RT.
2. If there exist a tree rtj that must be ored with rt the function
produces an imerge the represents the formula
RTi_1 OR ... OR (RTi_j OR RT) OR ... OR RTi_k,
where the range tree for (RTi_j OR RT) is constructed by oring the
SEL_ARG trees that must be ored.
3. For each rti_j that can be ored with rt the function produces
the new tree rti_j' and substitutes rti_j for this new range tree.
In any case the function removes mi from the list and then adds all
produced imerges.
To build imerges by rules 1-3 the function calls the method
SEL_IMERGE::or_sel_tree_with_checks, possibly twice. With the first
call it passes TRUE for the third parameter of the function.
At this first call imerges by rules 1-2 are built. If the call
returns FALSE as the return value of its fourth parameter then the
function are called for the second time. At this call the imerge
of rule 3 is produced.
If a call of SEL_IMERGE::or_sel_tree_with_checks returns 1 then
then it means that the produced tree contains an always true
range tree and the whole imerge can be discarded.
RETURN
0 OK, result is stored in *im1.
other Error
1 if no imerges are produced
0 otherwise
*/
static
int imerge_list_or_tree(RANGE_OPT_PARAM *param,
List<SEL_IMERGE> *im1,
List<SEL_IMERGE> *merges,
SEL_TREE *tree)
{
SEL_IMERGE *imerge;
List_iterator<SEL_IMERGE> it(*im1);
bool tree_used= FALSE;
List<SEL_IMERGE> additional_merges;
List_iterator<SEL_IMERGE> it(*merges);
while ((imerge= it++))
{
SEL_TREE *or_tree;
if (tree_used)
bool is_last_check_pass;
int rc= 0;
int rc1= 0;
SEL_TREE *or_tree= new SEL_TREE (tree, FALSE, param);
if (or_tree)
{
or_tree= new SEL_TREE (tree, param);
if (!or_tree ||
(or_tree->keys_map.is_clear_all() && or_tree->merges.is_empty()))
return FALSE;
uint elems= imerge->trees_next-imerge->trees;
rc= imerge->or_sel_tree_with_checks(param, elems, or_tree,
TRUE, &is_last_check_pass);
if (!is_last_check_pass)
{
SEL_IMERGE *new_imerge= new SEL_IMERGE(imerge, elems, param);
if (new_imerge)
{
rc1= new_imerge->or_sel_tree_with_checks(param, elems, or_tree,
FALSE, &is_last_check_pass);
if (!rc1)
additional_merges.push_back(new_imerge);
}
else
or_tree= tree;
}
}
if (rc || rc1 || !or_tree)
it.remove();
}
merges->concat(&additional_merges);
return merges->is_empty();
}
/*
Perform pushdown operation of the range part of a tree into given imerges
SYNOPSIS
imerge_list_and_tree()
param Context info for the operation
merges IN/OUT List of imerges to push the range part of 'tree' into
tree SEL_TREE whose range part is to be pushed into imerges
DESCRIPTION
For each imerge from the list merges the function pushes the range part
rt of 'tree' into the imerge.
More exactly if the imerge mi from the list represents the formula
RTi_1 OR ... OR RTi_k
the function bulds a new imerge that represents the formula
(RTi_1 AND RT) OR ... OR (RTi_k AND RT)
and adds this imerge to the list merges.
To perform this pushdown operation the function calls the method
SEL_IMERGE::and_sel_tree.
For any imerge mi the new imerge is not created if for each pair of
trees rti_j and rt the intersection of the indexes with defined ranges
is empty.
If the result of the pushdown operation for the imerge mi returns an
imerge with no trees then then not only nothing is added to the list
merges but mi itself is removed from the list.
RETURN
1 if no imerges are left in the list merges
0 otherwise
*/
if (imerge->or_sel_tree_with_checks(param, or_tree))
static
int imerge_list_and_tree(RANGE_OPT_PARAM *param,
List<SEL_IMERGE> *merges,
SEL_TREE *tree)
{
SEL_IMERGE *imerge;
SEL_IMERGE *new_imerge= NULL;
List<SEL_IMERGE> new_merges;
List_iterator<SEL_IMERGE> it(*merges);
while ((imerge= it++))
{
if (!new_imerge)
new_imerge= new SEL_IMERGE();
if (imerge->have_common_keys(param, tree) &&
new_imerge && !imerge->and_sel_tree(param, tree, new_imerge))
{
if (new_imerge->trees == new_imerge->trees_next)
it.remove();
tree_used= TRUE;
else
{
new_merges.push_back(new_imerge);
new_imerge= NULL;
}
return im1->is_empty();
}
}
imerge_list_and_list(&new_merges, merges);
*merges= new_merges;
return merges->is_empty();
}
......@@ -1660,6 +2197,7 @@ SEL_ARG::SEL_ARG(SEL_ARG &arg) :Sql_alloc()
min_value=arg.min_value;
max_value=arg.max_value;
next_key_part=arg.next_key_part;
max_part_no= arg.max_part_no;
use_count=1; elements=1;
}
......@@ -1677,9 +2215,10 @@ SEL_ARG::SEL_ARG(Field *f,const uchar *min_value_arg,
:min_flag(0), max_flag(0), maybe_flag(0), maybe_null(f->real_maybe_null()),
elements(1), use_count(1), field(f), min_value((uchar*) min_value_arg),
max_value((uchar*) max_value_arg), next(0),prev(0),
next_key_part(0),color(BLACK),type(KEY_RANGE)
next_key_part(0), color(BLACK), type(KEY_RANGE)
{
left=right= &null_element;
max_part_no= 1;
}
SEL_ARG::SEL_ARG(Field *field_,uint8 part_,
......@@ -1690,6 +2229,7 @@ SEL_ARG::SEL_ARG(Field *field_,uint8 part_,
field(field_), min_value(min_value_), max_value(max_value_),
next(0),prev(0),next_key_part(0),color(BLACK),type(KEY_RANGE)
{
max_part_no= part+1;
left=right= &null_element;
}
......@@ -1733,6 +2273,7 @@ SEL_ARG *SEL_ARG::clone(RANGE_OPT_PARAM *param, SEL_ARG *new_parent,
increment_use_count(1);
tmp->color= color;
tmp->elements= this->elements;
tmp->max_part_no= max_part_no;
return tmp;
}
......@@ -2376,12 +2917,12 @@ int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
It is possible to use a range-based quick select (but it might be
slower than 'all' table scan).
*/
if (tree->merges.is_empty())
{
TRP_RANGE *range_trp;
TRP_ROR_INTERSECT *rori_trp;
bool can_build_covering= FALSE;
remove_nonrange_trees(&param, tree);
/* Get best 'range' plan and prepare data for making other plans */
if ((range_trp= get_key_scans_params(&param, tree, FALSE, TRUE,
best_read_time)))
......@@ -2417,9 +2958,7 @@ int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
best_trp= rori_trp;
}
}
}
else
{
if (optimizer_flag(thd, OPTIMIZER_SWITCH_INDEX_MERGE))
{
/* Try creating index_merge/ROR-union scan. */
......@@ -2435,15 +2974,18 @@ int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
if (new_conj_trp)
set_if_smaller(param.table->quick_condition_rows,
new_conj_trp->records);
if (!best_conj_trp || (new_conj_trp && new_conj_trp->read_cost <
best_conj_trp->read_cost))
if (new_conj_trp &&
(!best_conj_trp ||
new_conj_trp->read_cost < best_conj_trp->read_cost))
{
best_conj_trp= new_conj_trp;
best_read_time= best_conj_trp->read_cost;
}
}
if (best_conj_trp)
best_trp= best_conj_trp;
}
}
}
thd->mem_root= param.old_root;
......@@ -3705,7 +4247,6 @@ TABLE_READ_PLAN *get_best_disjunct_quick(PARAM *param, SEL_IMERGE *imerge,
{
SEL_TREE **ptree;
TRP_INDEX_MERGE *imerge_trp= NULL;
uint n_child_scans= imerge->trees_next - imerge->trees;
TRP_RANGE **range_scans;
TRP_RANGE **cur_child;
TRP_RANGE **cpk_scan= NULL;
......@@ -3725,6 +4266,24 @@ TABLE_READ_PLAN *get_best_disjunct_quick(PARAM *param, SEL_IMERGE *imerge,
DBUG_ENTER("get_best_disjunct_quick");
DBUG_PRINT("info", ("Full table scan cost: %g", read_time));
/*
In every tree of imerge remove SEL_ARG trees that do not make ranges.
If after this removal some SEL_ARG tree becomes empty discard imerge.
*/
for (ptree= imerge->trees; ptree != imerge->trees_next; ptree++)
{
if (remove_nonrange_trees(param, *ptree))
{
imerge->trees_next= imerge->trees;
break;
}
}
uint n_child_scans= imerge->trees_next - imerge->trees;
if (!n_child_scans)
DBUG_RETURN(NULL);
if (!(range_scans= (TRP_RANGE**)alloc_root(param->mem_root,
sizeof(TRP_RANGE*)*
n_child_scans)))
......@@ -3844,6 +4403,13 @@ TABLE_READ_PLAN *get_best_disjunct_quick(PARAM *param, SEL_IMERGE *imerge,
imerge_trp->range_scans_end= range_scans + n_child_scans;
read_time= imerge_cost;
}
if (imerge_trp)
{
TABLE_READ_PLAN *trp= merge_same_index_scans(param, imerge, imerge_trp,
read_time);
if (trp != imerge_trp)
DBUG_RETURN(trp);
}
}
build_ror_index_merge:
......@@ -3859,6 +4425,7 @@ build_ror_index_merge:
sizeof(TABLE_READ_PLAN*)*
n_child_scans)))
DBUG_RETURN(imerge_trp);
skip_to_ror_scan:
roru_index_costs= 0.0;
roru_total_records= 0;
......@@ -3945,6 +4512,115 @@ skip_to_ror_scan:
DBUG_RETURN(imerge_trp);
}
/*
Merge index scans for the same indexes in an index merge plan
SYNOPSIS
merge_same_index_scans()
param Context info for the operation
imerge IN/OUT SEL_IMERGE from which imerge_trp has been extracted
imerge_trp The index merge plan where index scans for the same
indexes are to be merges
read_time The upper bound for the cost of the plan to be evaluated
DESRIPTION
For the given index merge plan imerge_trp extracted from the SEL_MERGE
imerge the function looks for range scans with the same indexes and merges
them into SEL_ARG trees. Then for each such SEL_ARG tree r_i the function
creates a range tree rt_i that contains only r_i. All rt_i are joined
into one index merge that replaces the original index merge imerge.
The function calls get_best_disjunct_quick for the new index merge to
get a new index merge plan that contains index scans only for different
indexes.
If there are no index scans for the same index in the original index
merge plan the function does not change the original imerge and returns
imerge_trp as its result.
RETURN
The original or or improved index merge plan
*/
static
TABLE_READ_PLAN *merge_same_index_scans(PARAM *param, SEL_IMERGE *imerge,
TRP_INDEX_MERGE *imerge_trp,
double read_time)
{
uint16 first_scan_tree_idx[MAX_KEY];
SEL_TREE **tree;
TRP_RANGE **cur_child;
uint removed_cnt= 0;
DBUG_ENTER("merge_same_index_scans");
bzero(first_scan_tree_idx, sizeof(first_scan_tree_idx[0])*param->keys);
for (tree= imerge->trees, cur_child= imerge_trp->range_scans;
tree != imerge->trees_next;
tree++, cur_child++)
{
DBUG_ASSERT(tree);
uint key_idx= (*cur_child)->key_idx;
uint16 *tree_idx_ptr= &first_scan_tree_idx[key_idx];
if (!*tree_idx_ptr)
*tree_idx_ptr= (uint16) (tree-imerge->trees+1);
else
{
SEL_TREE **changed_tree= imerge->trees+(*tree_idx_ptr-1);
SEL_ARG *key= (*changed_tree)->keys[key_idx];
bzero((*changed_tree)->keys,
sizeof((*changed_tree)->keys[0])*param->keys);
(*changed_tree)->keys_map.clear_all();
if (((*changed_tree)->keys[key_idx]=
key_or(param, key, (*tree)->keys[key_idx])))
(*changed_tree)->keys_map.set_bit(key_idx);
*tree= NULL;
removed_cnt++;
}
}
if (!removed_cnt)
DBUG_RETURN(imerge_trp);
TABLE_READ_PLAN *trp= NULL;
SEL_TREE **new_trees_next= imerge->trees;
for (tree= new_trees_next; tree != imerge->trees_next; tree++)
{
if (!*tree)
continue;
if (tree > new_trees_next)
*new_trees_next= *tree;
new_trees_next++;
}
imerge->trees_next= new_trees_next;
DBUG_ASSERT(imerge->trees_next>imerge->trees);
if (imerge->trees_next-imerge->trees > 1)
trp= get_best_disjunct_quick(param, imerge, read_time);
else
{
/*
This alternative theoretically can be reached when the cost
of the index merge for such a formula as
(key1 BETWEEN c1_1 AND c1_2) AND key2 > c2 OR
(key1 BETWEEN c1_3 AND c1_4) AND key3 > c3
is estimated as being cheaper than the cost of index scan for
the formula
(key1 BETWEEN c1_1 AND c1_2) OR (key1 BETWEEN c1_3 AND c1_4)
In the current code this may happen for two reasons:
1. for a single index range scan data records are accessed in
a random order
2. the functions that estimate the cost of a range scan and an
index merge retrievals are not well calibrated
*/
trp= get_key_scans_params(param, *imerge->trees, FALSE, TRUE,
read_time);
}
DBUG_RETURN(trp);
}
typedef struct st_ror_scan_info
{
uint idx; /* # of used key in param->keys */
......@@ -4001,7 +4677,7 @@ ROR_SCAN_INFO *make_ror_scan(const PARAM *param, int idx, SEL_ARG *sel_arg)
ror_scan->key_rec_length= (param->table->key_info[keynr].key_length +
param->table->file->ref_length);
ror_scan->sel_arg= sel_arg;
ror_scan->records= param->table->quick_rows[keynr];
ror_scan->records= param->quick_rows[keynr];
if (!(bitmap_buf= (my_bitmap_map*) alloc_root(param->mem_root,
param->fields_bitmap_size)))
......@@ -4307,7 +4983,7 @@ static double ror_scan_selectivity(const ROR_INTERSECT_INFO *info,
}
if (!prev_covered)
{
double tmp= rows2double(info->param->table->quick_rows[scan->keynr]) /
double tmp= rows2double(info->param->quick_rows[scan->keynr]) /
rows2double(prev_records);
DBUG_PRINT("info", ("Selectivity multiplier: %g", tmp));
selectivity_mult *= tmp;
......@@ -4386,7 +5062,7 @@ static bool ror_intersect_add(ROR_INTERSECT_INFO *info,
}
else
{
info->index_records += info->param->table->quick_rows[ror_scan->keynr];
info->index_records += info->param->quick_rows[ror_scan->keynr];
info->index_scan_costs += ror_scan->index_read_cost;
bitmap_union(&info->covered_fields, &ror_scan->covered_fields);
if (!info->is_covering && bitmap_is_subset(&info->param->needed_fields,
......@@ -6063,13 +6739,138 @@ sel_add(SEL_ARG *key1,SEL_ARG *key2)
return root;
}
#define CLONE_KEY1_MAYBE 1
#define CLONE_KEY2_MAYBE 2
#define swap_clone_flag(A) ((A & 1) << 1) | ((A & 2) >> 1)
/*
Build a range tree for the conjunction of the range parts of two trees
static SEL_TREE *
tree_and(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2)
SYNOPSIS
and_range_trees()
param Context info for the operation
tree1 SEL_TREE for the first conjunct
tree2 SEL_TREE for the second conjunct
result SEL_TREE for the result
DESCRIPTION
This function takes range parts of two trees tree1 and tree2 and builds
a range tree for the conjunction of the formulas that these two range parts
represent.
More exactly:
if the range part of tree1 represents the normalized formula
R1_1 AND ... AND R1_k,
and the range part of tree2 represents the normalized formula
R2_1 AND ... AND R2_k,
then the range part of the result represents the formula:
RT = R_1 AND ... AND R_k, where R_i=(R1_i AND R2_i) for each i from [1..k]
The function assumes that tree1 is never equal to tree2. At the same
time the tree result can be the same as tree1 (but never as tree2).
If result==tree1 then rt replaces the range part of tree1 leaving
imerges as they are.
if result!=tree1 than it is assumed that the SEL_ARG trees in tree1 and
tree2 should be preserved. Otherwise they can be destroyed.
RETURN
1 if the type the result tree is SEL_TREE::IMPOSSIBLE
0 otherwise
*/
static
int and_range_trees(RANGE_OPT_PARAM *param, SEL_TREE *tree1, SEL_TREE *tree2,
SEL_TREE *result)
{
DBUG_ENTER("and_ranges");
key_map result_keys;
result_keys.clear_all();
key_map anded_keys= tree1->keys_map;
anded_keys.merge(tree2->keys_map);
int key_no;
key_map::Iterator it(anded_keys);
while ((key_no= it++) != key_map::Iterator::BITMAP_END)
{
uint flag=0;
SEL_ARG *key1= tree1->keys[key_no];
SEL_ARG *key2= tree2->keys[key_no];
if (key1 && !key1->simple_key())
flag|= CLONE_KEY1_MAYBE;
if (key2 && !key2->simple_key())
flag|=CLONE_KEY2_MAYBE;
if (result != tree1)
{
if (key1)
key1->incr_refs();
if (key2)
key2->incr_refs();
}
SEL_ARG *key;
if ((result->keys[key_no]= key =key_and(param, key1, key2, flag)))
{
if (key && key->type == SEL_ARG::IMPOSSIBLE)
{
result->type= SEL_TREE::IMPOSSIBLE;
DBUG_RETURN(1);
}
result_keys.set_bit(key_no);
#ifdef EXTRA_DEBUG
if (param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS)
key->test_use_count(key);
#endif
}
}
result->keys_map= result_keys;
DBUG_RETURN(0);
}
/*
Build a SEL_TREE for a conjunction out of such trees for the conjuncts
SYNOPSIS
tree_and()
param Context info for the operation
tree1 SEL_TREE for the first conjunct
tree2 SEL_TREE for the second conjunct
DESCRIPTION
This function builds a tree for the formula (A AND B) out of the trees
tree1 and tree2 that has been built for the formulas A and B respectively.
In a general case
tree1 represents the formula RT1 AND MT1,
where RT1 = R1_1 AND ... AND R1_k1, MT1=M1_1 AND ... AND M1_l1;
tree2 represents the formula RT2 AND MT2
where RT2 = R2_1 AND ... AND R2_k2, MT2=M2_1 and ... and M2_l2.
The result tree will represent the formula of the the following structure:
RT AND MT1 AND MT2 AND RT1MT2 AND RT2MT1, such that
rt is a tree obtained by range intersection of trees tree1 and tree2,
RT1MT2 = RT1M2_1 AND ... AND RT1M2_l2,
RT2MT1 = RT2M1_1 AND ... AND RT2M1_l1,
where rt1m2_i (i=1,...,l2) is the result of the pushdown operation
of range tree rt1 into imerge m2_i, while rt2m1_j (j=1,...,l1) is the
result of the pushdown operation of range tree rt2 into imerge m1_j.
RT1MT2/RT2MT is empty if MT2/MT1 is empty.
The range intersection of two range trees is produced by the function
and_range_trees. The pushdown of a range tree to a imerge is performed
by the function imerge_list_and_tree. This function may produce imerges
containing only one range tree. Such trees are intersected with rt and
the result of intersection is returned as the range part of the result
tree, while the corresponding imerges are removed altogether from its
imerge part.
NOTE.
The pushdown operation of range trees into imerges is needed to be able
to construct valid imerges for the condition like this:
key1_p1=c1 AND (key1_p2 BETWEEN c21 AND c22 OR key2 < c2)
RETURN
The result tree, if a success
0 - otherwise.
*/
static
SEL_TREE *tree_and(RANGE_OPT_PARAM *param, SEL_TREE *tree1, SEL_TREE *tree2)
{
DBUG_ENTER("tree_and");
if (!tree1)
......@@ -6091,86 +6892,215 @@ tree_and(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2)
tree1->type=SEL_TREE::KEY_SMALLER;
DBUG_RETURN(tree1);
}
key_map result_keys;
result_keys.clear_all();
/* Join the trees key per key */
SEL_ARG **key1,**key2,**end;
for (key1= tree1->keys,key2= tree2->keys,end=key1+param->keys ;
key1 != end ; key1++,key2++)
{
uint flag=0;
if (*key1 || *key2)
if (!tree1->merges.is_empty())
imerge_list_and_tree(param, &tree1->merges, tree2);
if (!tree2->merges.is_empty())
imerge_list_and_tree(param, &tree2->merges, tree1);
if (and_range_trees(param, tree1, tree2, tree1))
DBUG_RETURN(tree1);
imerge_list_and_list(&tree1->merges, &tree2->merges);
eliminate_single_tree_imerges(param, tree1);
DBUG_RETURN(tree1);
}
/*
Eliminate single tree imerges in a SEL_TREE objects
SYNOPSIS
eliminate_single_tree_imerges()
param Context info for the function
tree SEL_TREE where single tree imerges are to be eliminated
DESCRIPTION
For each imerge in 'tree' that contains only one disjunct tree, i.e.
for any imerge of the form m=rt, the function performs and operation
the range part of tree, replaces rt the with the result of anding and
removes imerge m from the the merge part of 'tree'.
RETURN VALUE
none
*/
static
void eliminate_single_tree_imerges(RANGE_OPT_PARAM *param, SEL_TREE *tree)
{
SEL_IMERGE *imerge;
List<SEL_IMERGE> merges= tree->merges;
List_iterator<SEL_IMERGE> it(merges);
tree->merges.empty();
while ((imerge= it++))
{
if (*key1 && !(*key1)->simple_key())
flag|=CLONE_KEY1_MAYBE;
if (*key2 && !(*key2)->simple_key())
flag|=CLONE_KEY2_MAYBE;
*key1=key_and(param, *key1, *key2, flag);
if (*key1 && (*key1)->type == SEL_ARG::IMPOSSIBLE)
if (imerge->trees+1 == imerge->trees_next)
{
tree1->type= SEL_TREE::IMPOSSIBLE;
DBUG_RETURN(tree1);
}
result_keys.set_bit(key1 - tree1->keys);
#ifdef EXTRA_DEBUG
if (*key1 && param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS)
(*key1)->test_use_count(*key1);
#endif
tree= tree_and(param, tree, *imerge->trees);
it.remove();
}
}
tree1->keys_map= result_keys;
/* dispose index_merge if there is a "range" option */
if (!result_keys.is_clear_all())
{
tree1->merges.empty();
DBUG_RETURN(tree1);
}
tree->merges= merges;
}
/* ok, both trees are index_merge trees */
imerge_list_and_list(&tree1->merges, &tree2->merges);
DBUG_RETURN(tree1);
/*
For two trees check that there are indexes with ranges in both of them
SYNOPSIS
sel_trees_have_common_keys()
tree1 SEL_TREE for the first tree
tree2 SEL_TREE for the second tree
common_keys OUT bitmap of all indexes with ranges in both trees
DESCRIPTION
For two trees tree1 and tree1 the function checks if there are indexes
in their range parts such that SEL_ARG trees are defined for them in the
range parts of both trees. The function returns the bitmap of such
indexes in the parameter common_keys.
RETURN
TRUE if there are such indexes (common_keys is nor empty)
FALSE otherwise
*/
static
bool sel_trees_have_common_keys(SEL_TREE *tree1, SEL_TREE *tree2,
key_map *common_keys)
{
*common_keys= tree1->keys_map;
common_keys->intersect(tree2->keys_map);
return !common_keys->is_clear_all();
}
/*
Check if two SEL_TREES can be combined into one (i.e. a single key range
read can be constructed for "cond_of_tree1 OR cond_of_tree2" ) without
using index_merge.
Check whether range parts of two trees can be ored for some indexes
SYNOPSIS
sel_trees_can_be_ored()
param Context info for the function
tree1 SEL_TREE for the first tree
tree2 SEL_TREE for the second tree
common_keys IN/OUT IN: bitmap of all indexes with SEL_ARG in both trees
OUT: bitmap of all indexes that can be ored
DESCRIPTION
For two trees tree1 and tree2 and the bitmap common_keys containing
bits for indexes that have SEL_ARG trees in range parts of both trees
the function checks if there are indexes for which SEL_ARG trees can
be ored. Two SEL_ARG trees for the same index can be ored if the most
major components of the index used in these trees coincide. If the
SEL_ARG trees for an index cannot be ored the function clears the bit
for this index in the bitmap common_keys.
The function does not verify that indexes marked in common_keys really
have SEL_ARG trees in both tree1 and tree2. It assumes that this is true.
NOTE
The function sel_trees_can_be_ored is usually used in pair with the
function sel_trees_have_common_keys.
RETURN
TRUE if there are indexes for which SEL_ARG trees can be ored
FALSE otherwise
*/
bool sel_trees_can_be_ored(SEL_TREE *tree1, SEL_TREE *tree2,
RANGE_OPT_PARAM* param)
static
bool sel_trees_can_be_ored(RANGE_OPT_PARAM* param,
SEL_TREE *tree1, SEL_TREE *tree2,
key_map *common_keys)
{
key_map common_keys= tree1->keys_map;
DBUG_ENTER("sel_trees_can_be_ored");
common_keys.intersect(tree2->keys_map);
if (!sel_trees_have_common_keys(tree1, tree2, common_keys))
DBUG_RETURN(FALSE);
int key_no;
key_map::Iterator it(*common_keys);
while ((key_no= it++) != key_map::Iterator::BITMAP_END)
{
DBUG_ASSERT(tree1->keys[key_no] && tree2->keys[key_no]);
/* Trees have a common key, check if they refer to the same key part */
if (tree1->keys[key_no]->part != tree2->keys[key_no]->part)
common_keys->clear_bit(key_no);
}
DBUG_RETURN(!common_keys->is_clear_all());
}
if (common_keys.is_clear_all())
/*
Check whether range parts of two trees must be ored for some indexes
SYNOPSIS
sel_trees_must_be_ored()
param Context info for the function
tree1 SEL_TREE for the first tree
tree2 SEL_TREE for the second tree
ordable_keys bitmap of SEL_ARG trees that can be ored
DESCRIPTION
For two trees tree1 and tree2 the function checks whether they must be
ored. The function assumes that the bitmap ordable_keys contains bits for
those corresponding pairs of SEL_ARG trees from tree1 and tree2 that can
be ored.
We believe that tree1 and tree2 must be ored if any pair of SEL_ARG trees
r1 and r2, such that r1 is from tree1 and r2 is from tree2 and both
of them are marked in ordable_keys, can be merged.
NOTE
The function sel_trees_must_be_ored as a rule is used in pair with the
function sel_trees_can_be_ored.
RETURN
TRUE if there are indexes for which SEL_ARG trees must be ored
FALSE otherwise
*/
static
bool sel_trees_must_be_ored(RANGE_OPT_PARAM* param,
SEL_TREE *tree1, SEL_TREE *tree2,
key_map oredable_keys)
{
key_map tmp;
DBUG_ENTER("sel_trees_must_be_ored");
tmp= tree1->keys_map;
tmp.merge(tree2->keys_map);
tmp.subtract(oredable_keys);
if (!tmp.is_clear_all())
DBUG_RETURN(FALSE);
/* trees have a common key, check if they refer to same key part */
SEL_ARG **key1,**key2;
for (uint key_no=0; key_no < param->keys; key_no++)
int idx1, idx2;
key_map::Iterator it1(oredable_keys);
while ((idx1= it1++) != key_map::Iterator::BITMAP_END)
{
if (common_keys.is_set(key_no))
KEY_PART *key1_init= param->key[idx1]+tree1->keys[idx1]->part;
KEY_PART *key1_end= param->key[idx1]+tree1->keys[idx1]->max_part_no;
key_map::Iterator it2(oredable_keys);
while ((idx2= it2++) != key_map::Iterator::BITMAP_END)
{
key1= tree1->keys + key_no;
key2= tree2->keys + key_no;
if ((*key1)->part == (*key2)->part)
if (idx2 <= idx1)
continue;
KEY_PART *key2_init= param->key[idx2]+tree2->keys[idx2]->part;
KEY_PART *key2_end= param->key[idx2]+tree2->keys[idx2]->max_part_no;
KEY_PART *part1, *part2;
for (part1= key1_init, part2= key2_init;
part1 < key1_end && part2 < key2_end;
part1++, part2++)
{
DBUG_RETURN(TRUE);
if (!part1->field->eq(part2->field))
DBUG_RETURN(FALSE);
}
}
}
DBUG_RETURN(FALSE);
DBUG_RETURN(TRUE);
}
/*
Remove the trees that are not suitable for record retrieval.
Remove the trees that are not suitable for record retrieval
SYNOPSIS
param Range analysis parameter
remove_nonrange_trees()
param Context info for the function
tree Tree to be processed, tree->type is KEY or KEY_SMALLER
DESCRIPTION
......@@ -6183,39 +7113,34 @@ bool sel_trees_can_be_ored(SEL_TREE *tree1, SEL_TREE *tree2,
A SEL_ARG* tree cannot be used to construct quick select if it has
tree->part != 0. (e.g. it could represent "keypart2 < const").
WHY THIS FUNCTION IS NEEDED
Normally we allow construction of SEL_TREE objects that have SEL_ARG
trees that do not allow quick range select construction. For example for
" keypart1=1 AND keypart2=2 " the execution will proceed as follows:
trees that do not allow quick range select construction.
For example:
for " keypart1=1 AND keypart2=2 " the execution will proceed as follows:
tree1= SEL_TREE { SEL_ARG{keypart1=1} }
tree2= SEL_TREE { SEL_ARG{keypart2=2} } -- can't make quick range select
from this
call tree_and(tree1, tree2) -- this joins SEL_ARGs into a usable SEL_ARG
tree.
There is an exception though: when we construct index_merge SEL_TREE,
any SEL_ARG* tree that cannot be used to construct quick range select can
be removed, because current range analysis code doesn't provide any way
that tree could be later combined with another tree.
Consider an example: we should not construct
st1 = SEL_TREE {
merges = SEL_IMERGE {
SEL_TREE(t.key1part1 = 1),
SEL_TREE(t.key2part2 = 2) -- (*)
}
};
because
- (*) cannot be used to construct quick range select,
- There is no execution path that would cause (*) to be converted to
a tree that could be used.
The latter is easy to verify: first, notice that the only way to convert
(*) into a usable tree is to call tree_and(something, (*)).
Second look at what tree_and/tree_or function would do when passed a
SEL_TREE that has the structure like st1 tree has, and conlcude that
tree_and(something, (*)) will not be called.
Another example:
tree3= SEL_TREE { SEL_ARG{key1part1 = 1} }
tree4= SEL_TREE { SEL_ARG{key2part2 = 2} } -- can't make quick range select
from this
call tree_or(tree3, tree4) -- creates a SEL_MERGE ot of which no index
merge can be constructed, but it is potentially useful, as anding it with
tree5= SEL_TREE { SEL_ARG{key2part1 = 3} } creates an index merge that
represents the formula
key1part1=1 AND key2part1=3 OR key2part1=3 AND key2part2=2
for which an index merge can be built.
Any final SEL_TREE may contain SEL_ARG trees for which no quick select
can be built. Such SEL_ARG trees should be removed from the range part
before different range scans are evaluated. Such SEL_ARG trees also should
be removed from all range trees of each index merge before different
possible index merge plans are evaluated. If after this removal one
of the range trees in the index merge becomes empty the whole index merge
must be discarded.
RETURN
0 Ok, some suitable trees left
......@@ -6242,6 +7167,74 @@ static bool remove_nonrange_trees(RANGE_OPT_PARAM *param, SEL_TREE *tree)
}
/*
Build a SEL_TREE for a disjunction out of such trees for the disjuncts
SYNOPSIS
tree_or()
param Context info for the operation
tree1 SEL_TREE for the first disjunct
tree2 SEL_TREE for the second disjunct
DESCRIPTION
This function builds a tree for the formula (A OR B) out of the trees
tree1 and tree2 that has been built for the formulas A and B respectively.
In a general case
tree1 represents the formula RT1 AND MT1,
where RT1=R1_1 AND ... AND R1_k1, MT1=M1_1 AND ... AND M1_l1;
tree2 represents the formula RT2 AND MT2
where RT2=R2_1 AND ... AND R2_k2, MT2=M2_1 and ... and M2_l2.
The function constructs the result tree according the formula
(RT1 OR RT2) AND (MT1 OR RT1) AND (MT2 OR RT2) AND (MT1 OR MT2)
that is equivalent to the formula (RT1 AND MT1) OR (RT2 AND MT2).
To limit the number of produced imerges the function considers
a weaker formula than the original one:
(RT1 AND M1_1) OR (RT2 AND M2_1)
that is equivalent to:
(RT1 OR RT2) (1)
AND
(M1_1 OR M2_1) (2)
AND
(M1_1 OR RT2) (3)
AND
(M2_1 OR RT1) (4)
For the first conjunct (1) the function builds a tree with a range part
and, possibly, one imerge. For the other conjuncts (2-4)the function
produces sets of imerges. All constructed imerges are included into the
result tree.
For the formula (1) the function produces the tree representing a formula
of the structure RT [AND M], such that:
- the range tree rt contains the result of oring SEL_ARG trees from rt1
and rt2
- the imerge m consists of two range trees rt1 and rt2.
The imerge m is added if it's not true that rt1 and rt2 must be ored
If rt1 and rt2 can't be ored rt is empty and only m is produced for (1).
To produce imerges for the formula (2) the function calls the function
imerge_list_or_list passing it the merge parts of tree1 and tree2 as
parameters.
To produce imerges for the formula (3) the function calls the function
imerge_list_or_tree passing it the imerge m1_1 and the range tree rt2 as
parameters. Similarly, to produce imerges for the formula (4) the function
calls the function imerge_list_or_tree passing it the imerge m2_1 and the
range tree rt1.
If rt1 is empty then the trees for (1) and (4) are empty.
If rt2 is empty then the trees for (1) and (3) are empty.
If mt1 is empty then the trees for (2) and (3) are empty.
If mt2 is empty then the trees for (2) and (4) are empty.
RETURN
The result tree for the operation if a success
0 - otherwise
*/
static SEL_TREE *
tree_or(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2)
{
......@@ -6257,74 +7250,100 @@ tree_or(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2)
if (tree2->type == SEL_TREE::MAYBE)
DBUG_RETURN(tree2);
SEL_TREE *result= 0;
SEL_TREE *result= NULL;
key_map result_keys;
result_keys.clear_all();
if (sel_trees_can_be_ored(tree1, tree2, param))
key_map ored_keys;
SEL_TREE *rtree[2]= {NULL,NULL};
SEL_IMERGE *imerge[2]= {NULL, NULL};
bool no_ranges1= tree1->without_ranges();
bool no_ranges2= tree2->without_ranges();
bool no_merges1= tree1->without_imerges();
bool no_merges2= tree2->without_imerges();
if (!no_ranges1 && !no_merges2)
{
/* Join the trees key per key */
SEL_ARG **key1,**key2,**end;
for (key1= tree1->keys,key2= tree2->keys,end= key1+param->keys ;
key1 != end ; key1++,key2++)
rtree[0]= new SEL_TREE(tree1, TRUE, param);
imerge[1]= new SEL_IMERGE(tree2->merges.head(), 0, param);
}
if (!no_ranges2 && !no_merges1)
{
*key1=key_or(param, *key1, *key2);
if (*key1)
rtree[1]= new SEL_TREE(tree2, TRUE, param);
imerge[0]= new SEL_IMERGE(tree1->merges.head(), 0, param);
}
bool no_imerge_from_ranges= FALSE;
if (!(result= new SEL_TREE()))
DBUG_RETURN(result);
/* Build the range part of the tree for the formula (1) */
if (sel_trees_can_be_ored(param, tree1, tree2, &ored_keys))
{
result=tree1; // Added to tree1
result_keys.set_bit(key1 - tree1->keys);
#ifdef EXTRA_DEBUG
if (param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS)
(*key1)->test_use_count(*key1);
#endif
bool must_be_ored= sel_trees_must_be_ored(param, tree1, tree2, ored_keys);
no_imerge_from_ranges= must_be_ored;
key_map::Iterator it(ored_keys);
int key_no;
while ((key_no= it++) != key_map::Iterator::BITMAP_END)
{
SEL_ARG *key1= tree1->keys[key_no];
SEL_ARG *key2= tree2->keys[key_no];
if (!must_be_ored)
{
key1->incr_refs();
key2->incr_refs();
}
if ((result->keys[key_no]= key_or(param, key1, key2)))
result->keys_map.set_bit(key_no);
}
if (result)
result->keys_map= result_keys;
result->type= tree1->type;
}
else
{
/* ok, two trees have KEY type but cannot be used without index merge */
if (tree1->merges.is_empty() && tree2->merges.is_empty())
{
if (param->remove_jump_scans)
if (no_imerge_from_ranges && no_merges1 && no_merges2)
{
bool no_trees= remove_nonrange_trees(param, tree1);
no_trees= no_trees || remove_nonrange_trees(param, tree2);
if (no_trees)
DBUG_RETURN(new SEL_TREE(SEL_TREE::ALWAYS));
if (result->keys_map.is_clear_all())
result->type= SEL_TREE::ALWAYS;
DBUG_RETURN(result);
}
SEL_IMERGE *merge;
/* both trees are "range" trees, produce new index merge structure */
if (!(result= new SEL_TREE()) || !(merge= new SEL_IMERGE()) ||
(result->merges.push_back(merge)) ||
(merge->or_sel_tree(param, tree1)) ||
(merge->or_sel_tree(param, tree2)))
SEL_IMERGE *imerge_from_ranges;
if (!(imerge_from_ranges= new SEL_IMERGE()))
result= NULL;
else if (!no_ranges1 && !no_ranges2 && !no_imerge_from_ranges)
{
/* Build the imerge part of the tree for the formula (1) */
SEL_TREE *rt1= tree1;
SEL_TREE *rt2= tree2;
if (!no_merges1)
rt1= new SEL_TREE(tree1, TRUE, param);
if (!no_merges2)
rt2= new SEL_TREE(tree2, TRUE, param);
if (!rt1 || !rt2 ||
result->merges.push_back(imerge_from_ranges) ||
imerge_from_ranges->or_sel_tree(param, rt1) ||
imerge_from_ranges->or_sel_tree(param, rt2))
result= NULL;
else
result->type= tree1->type;
}
else if (!tree1->merges.is_empty() && !tree2->merges.is_empty())
if (!result)
DBUG_RETURN(result);
result->type= tree1->type;
if (!no_merges1 && !no_merges2 &&
!imerge_list_or_list(param, &tree1->merges, &tree2->merges))
{
if (imerge_list_or_list(param, &tree1->merges, &tree2->merges))
result= new SEL_TREE(SEL_TREE::ALWAYS);
else
result= tree1;
/* Build the imerges for the formula (2) */
imerge_list_and_list(&result->merges, &tree1->merges);
}
else
/* Build the imerges for the formulas (3) and (4) */
for (uint i=0; i < 2; i++)
{
/* one tree is index merge tree and another is range tree */
if (tree1->merges.is_empty())
swap_variables(SEL_TREE*, tree1, tree2);
List<SEL_IMERGE> merges;
SEL_TREE *rt= rtree[i];
SEL_IMERGE *im= imerge[1-i];
if (param->remove_jump_scans && remove_nonrange_trees(param, tree2))
DBUG_RETURN(new SEL_TREE(SEL_TREE::ALWAYS));
/* add tree2 to tree1->merges, checking if it collapses to ALWAYS */
if (imerge_list_or_tree(param, &tree1->merges, tree2))
result= new SEL_TREE(SEL_TREE::ALWAYS);
else
result= tree1;
}
if (rt && im && !merges.push_back(im) &&
!imerge_list_or_tree(param, &merges, rt))
imerge_list_and_list(&result->merges, &merges);
}
DBUG_RETURN(result);
}
......@@ -6370,6 +7389,7 @@ and_all_keys(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2,
if (!key1)
return &null_element; // Impossible ranges
key1->use_count++;
key1->max_part_no= max(key2->max_part_no, key2->part+1);
return key1;
}
......@@ -6462,6 +7482,7 @@ key_and(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2, uint clone_flag)
key1->use_count--;
key2->use_count--;
SEL_ARG *e1=key1->first(), *e2=key2->first(), *new_tree=0;
uint max_part_no= max(key1->max_part_no, key2->max_part_no);
while (e1 && e2)
{
......@@ -6499,6 +7520,7 @@ key_and(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2, uint clone_flag)
key2->free_tree();
if (!new_tree)
return &null_element; // Impossible range
new_tree->max_part_no= max_part_no;
return new_tree;
}
......@@ -6627,7 +7649,7 @@ key_or(RANGE_OPT_PARAM *param, SEL_ARG *key1,SEL_ARG *key2)
{
swap_variables(SEL_ARG *,key1,key2);
}
if (key1->use_count > 0 || !(key1=key1->clone_tree(param)))
if (key1->use_count > 0 && !(key1=key1->clone_tree(param)))
return 0; // OOM
}
......@@ -6635,6 +7657,8 @@ key_or(RANGE_OPT_PARAM *param, SEL_ARG *key1,SEL_ARG *key2)
bool key2_shared=key2->use_count != 0;
key1->maybe_flag|=key2->maybe_flag;
uint max_part_no= max(key1->max_part_no, key2->max_part_no);
for (key2=key2->first(); key2; )
{
SEL_ARG *tmp=key1->find_range(key2); // Find key1.min <= key2.min
......@@ -6868,6 +7892,7 @@ end:
key2=next;
}
key1->use_count++;
key1->max_part_no= max_part_no;
return key1;
}
......@@ -7343,11 +8368,7 @@ static ulong count_key_part_usage(SEL_ARG *root, SEL_ARG *key)
void SEL_ARG::test_use_count(SEL_ARG *root)
{
uint e_count=0;
if (this == root && use_count != 1)
{
sql_print_information("Use_count: Wrong count %lu for root",use_count);
return;
}
if (this->type != SEL_ARG::KEY_RANGE)
return;
for (SEL_ARG *pos=first(); pos ; pos=pos->next)
......@@ -7450,12 +8471,13 @@ check_quick_select(PARAM *param,uint idx,SEL_ARG *tree, bool update_tbl_stats)
param->table->quick_n_ranges[key]= param->n_ranges;
param->table->quick_condition_rows=
min(param->table->quick_condition_rows, records);
param->table->quick_rows[key]= records;
}
/*
Need to save quick_rows in any case as it is used when calculating
cost of ROR intersection:
*/
param->table->quick_rows[key]=records;
param->quick_rows[key]= records;
if (cpk_scan)
param->is_ror_scan= TRUE;
}
......
......@@ -235,6 +235,11 @@ public:
{
if (!list->is_empty())
{
if (is_empty())
{
*this= *list;
return;
}
*last= list->first;
last= list->last;
elements+= list->elements;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment