STRAIGHT_JOIN
is a hint that enforces JOIN
order in MySQL
.
PostgreSQL
developers dislike hints, that's why PostgreSQL
lacks this feature.
If you don't know / care what the JOIN
order is, just omit this keyword:
SELECT s.*, date_part('epoch', s.date) AS unixdate,
date_part('epoch', s.expire) as expireunix, u.username, u.fullname,
u.photo, u.email, t.topic, t.imageurl
FROM stories s, users u, topics t
WHERE s.uid = u.uid
AND s.tid = t.tid
MySQL
doesn't implement any JOIN
methods except for NESTED LOOPS
, but PostgreSQL
does.
JOIN
order makes no sense for MERGE JOIN
and cannot be forced in PostgreSQL
for HASH JOIN
.
You can make some of the columns non-sargable and this will force PostgreSQL
to use intended JOIN
order in case the optimizer chooses NESTED LOOPS
:
SELECT s.*, date_part('epoch', s.date) AS unixdate,
date_part('epoch', s.expire) as expireunix, u.username, u.fullname,
u.photo, u.email, t.topic, t.imageurl
FROM stories s, users u, topics t
WHERE s.uid = (u.uid + 1) - 1
AND t.tid = (s.tid + 1) - 1
This will force JOIN
order in case NESTED LOOPS
will be chosen by optimizer.
To force NESTED LOOPS
, get rid or the equijoin in the query:
SELECT s.*, date_part('epoch', s.date) AS unixdate,
date_part('epoch', s.expire) as expireunix, u.username, u.fullname,
u.photo, u.email, t.topic, t.imageurl
FROM stories s, users u, topics t
WHERE s.uid > u.uid - 1
AND s.uid < u.uid + 1
AND t.tid > s.tid - 1
AND t.tid < s.tid + 1