CREATE INDEX landmark_city_country ON `travel-sample`(city, country) WHERE type =
“landmark”; Optional index:
CREATE INDEX hotel_title ON `travel-sample`(title) WHERE type = “hotel”;
Query:
SELECT hotel.name hotel_name, landmark.name landmark_name, landmark.activity
FROM `travel-sample` hotel JOIN `travel-sample` landmark
ON hotel.city = landmark.city AND hotel.country = landmark.country AND landmark.
type = “landmark”
WHERE hotel.type = “hotel” AND hotel.title like “Yosemite%” AND array_length(hotel.
public_likes) > 5;
CREATE INDEX airport_faa_name ON `travel-sample`(faa, airportname) WHERE type =
“airport”;
Optional index:
CREATE INDEX route_airline_distance ON `travel-sample`(airline, distance) WHERE type =
“route”;
Query:
SELECT DISTINCT airport.airportname
FROM `travel-sample` route JOIN `travel-sample` airport
ON airport.faa IN [ route.sourceairport, route.destinationairport ] AND airport.
type = “airport”
WHERE route.type = “route” AND route.airline = “F9” AND route.distance > 3000;
Required index (route_airports index same as example 1):
CREATE INDEX route_airports ON `travel-sample`(sourceairport, destinationairport) WHERE
type = “route”;
CREATE INDEX route_airports2 ON `travel-sample`(destinationairport, sourceairport)
WHERE type = “route”;
Optional index (same as example 1):
CREATE INDEX airport_city_country ON `travel-sample`(city, country) WHERE type =
“airport”;
Query:
SELECT count(*)
FROM `travel-sample` airport JOIN `travel-sample` route
ON (route.sourceairport = airport.faa OR route.destinationairport = airport.faa)
AND route.type = “route”
WHERE airport.type = “airport” AND airport.city = “Denver” AND airport.country =
“United States”;
SELECT DISTINCT route.destinationairport
FROM `travel-sample` airport JOIN `travel-sample` route USE INDEX(route_airports)
ON airport.faa = route.sourceairport AND route.type = “route”
WHERE airport.type = “airport” AND airport.city = “San Francisco” AND airport.country =
“United States”;
使用索引提示限制计划人员执行联接时需要考虑的索引数量。
提示也可以在ANSI JOIN的左侧键空间中指定。
SELECT DISTINCT route.destinationairport
FROM `travel-sample` airport USE INDEX(airport_city_country)
JOIN `travel-sample` route USE INDEX(route_airports)
ON airport.faa = route.sourceairport AND route.type = “route”
WHERE airport.type = “airport” AND airport.city = “San Francisco” AND airport.country =
“United States”;
CREATE INDEX route_airports ON `travel-sample`(sourceairport, destinationairport)
WHERE type = “route”;
Optional index (same as example 1):
CREATE INDEX airport_city_country ON `travel-sample`(city, country) WHERE type =
“airport”;
Query:
SELECT airport.airportname, route.airlineid
FROM `travel-sample` airport LEFT JOIN `travel-sample` route
ON airport.faa = route.sourceairport AND route.type = “route”
WHERE airport.type = “airport” AND airport.city = “Denver” AND airport.country =
“United States”;
SELECT airport.airportname, route.airlineid
FROM `travel-sample` airport LEFT JOIN `travel-sample` route
ON airport.faa = route.sourceairport AND route.type = “route”
WHERE airport.type = “airport” AND airport.city = “Denver” AND airport.country =
“United States”
AND route.airlineid IS MISSING;
SELECT airport.airportname, route.airlineid
FROM `travel-sample` route RIGHT JOIN `travel-sample` airport
ON airport.faa = route.sourceairport AND route.type = “route”
WHERE airport.type = “airport” AND airport.city = “Denver” AND airport.country =
“United States”;
SELECT DISTINCT route.destinationairport
FROM `travel-sample` airport JOIN `travel-sample` route USE HASH(build)
ON airport.faa = route.sourceairport AND route.type = “route”
WHERE airport.type = “airport” AND airport.city = “San Jose” AND airport.country =
“United States”;
SELECT DISTINCT route.destinationairport
FROM `travel-sample` airport JOIN `travel-sample` route USE HASH(probe)
ON airport.faa = route.sourceairport AND route.type = “route”
WHERE airport.type = “airport” AND airport.city = “San Jose” AND airport.country =
“United States”;
SELECT DISTINCT route.destinationairport
FROM `travel-sample` airport JOIN `travel-sample` route USE HASH(probe)
INDEX(route_airports)
ON airport.faa = route.sourceairport AND route.type = “route”
WHERE airport.type = “airport” AND airport.city = “San Jose” AND airport.country =
“United States”;
注意,当多个提示一起使用时,您只需指定“USE”关键字一次,如上面的示例所示。
USE HASH提示还可以与USE KEYS提示相结合。
示例11:ANSI连接多个连接
ANSI连接可以链接在一起。例如:
Required indexes (route_airports index same as example 1):
CREATE INDEX route_airports ON `travel-sample`(sourceairport, destinationairport)
WHERE type = “route”;
CREATE INDEX airline_iata ON `travel-sample`(iata) WHERE type = “airline”;
Optional index (same as example 1):
CREATE INDEX airport_city_country ON `travel-sample`(city, country) WHERE type =
“airport”;
Query:
SELECT DISTINCT airline.name
FROM `travel-sample` airport INNER JOIN `travel-sample` route
ON airport.faa = route.sourceairport AND route.type = “route”
INNER JOIN `travel-sample` airline
ON route.airline = airline.iata AND airline.type = “airline”
WHERE airport.type = “airport” AND airport.city = “San Jose”
AND airport.country = “United States”;
CREATE INDEX default_ix_left on default(c11, DISTINCT a11) WHERE type = “left”;
CREATE INDEX efault_ix_right on default(c21, DISTINCT a21) WHERE type = “right”;
SELECT b1.c11, b2.c21, b2.c22
FROM default b1 JOIN default b2
ON b2.c21 = b1.c11 AND ANY v IN b2.a21 SATISFIES v = b1.c12 END AND b2.type =
“right”
WHERE b1.type = “left”;
SELECT b1.c11, b2.c21, b2.c22
FROM default b1 UNNEST b1.a11 AS ba1
JOIN default b2 ON ba1 = b2.c21 AND b2.type = “right”
WHERE b1.c11 = 2 AND b1.type = “left”;
SELECT b1.c11, b2.c21, b2.c22
FROM default b1 UNNEST b1.a11 AS ba1
JOIN default b2 ON b2.c21 = b1.c11 AND ANY v IN b2.a21 SATISFIES v = ba1 END AND
b2.type = “right”
WHERE b1.type = “left”;
Option 2: use IN-clause
SELECT b1.c11, b2.c21, b2.c22
FROM default b1 JOIN default b2
ON b2.c21 = b1.c11 AND ANY v IN b2.a21 SATISFIES v IN b1.a11 END AND b2.type =
“right”
WHERE b1.type = “left”;
CREATE INDEX route_airports ON `travel-sample`(sourceairport, destinationairport) WHERE
type = “route”;
这是一个使用查找连接语法的查询(注意ON KEYS子句):
SELECT airline.name
FROM `travel-sample` route JOIN `travel-sample` airline
ON KEYS route.airlineid
WHERE route.type = “route” AND route.sourceairport = “SFO” AND route.destinationairport
= “JFK”;
SELECT airline.name
FROM `travel-sample` route JOIN `travel-sample` airline
ON route.airlineid = meta(airline).id
WHERE route.type = “route” AND route.sourceairport = “SFO” AND route.destinationairport
= “JFK”;
CREATE INDEX route_airlineid ON `travel-sample`(airlineid) WHERE type = “route”;
Optional index (same as example 3):
CREATE INDEX airline_name ON `travel-sample`(name) WHERE type = “airline”;
Query using index join syntax (note the ON KEY … FOR … clause):
SELECT count(*)
FROM `travel-sample` airline JOIN `travel-sample` route
ON KEY route.airlineid FOR airline
WHERE airline.type = “airline” AND route.type = “route” AND airline.name = “United
Airlines”;