dongxi1320 2018-05-04 22:06
浏览 202

如何将SELECT转换为SELECT JOIN?

I'm trying to convert my SELECT query into a faster version of SELECT including JOIN, that I found online.

Here is my code:

$stmt->prepare("SELECT * FROM books WHERE person_id = :person_id ORDER BY id DESC LIMIT 5, 10");

convert to:

SELECT  l.id, value, LENGTH(stuffing) AS len
FROM    (
        SELECT  id
        FROM    t_limit
        ORDER BY
                id
        LIMIT 150000, 10
        ) o
JOIN    t_limit l
ON      l.id = o.id
ORDER BY
        l.id

I found that code online (last piece of code). Its supposed to help with performance when doing OFFSET and LIMIT. anyways... here is what i tried:

$stmt->prepare("SELECT * FROM (SELECT id FROM books WHERE person_id = :person_id ORDER BY id DESC LIMIT 5, 10) o WHERE person_id = :person_id JOIN books l ON l.id = o.id ORDER BY l.id");

but it does not seem to work.

any help would be appreciated!

  • 写回答

3条回答 默认 最新

  • dprxj1995 2018-05-04 22:12
    关注

    I found that code online. Its supposed to help with performance when doing OFFSET and LIMIT.

    Where did you hear that? The statement you've quoted appears to be very specific to a particular database schema, and is not applicable to your situation. Forget about it.

    As it stands, the SQL statement you are currently using is already optimal. If it is not performing adequately, you may need to create an index on books (person_id, id).

    评论

报告相同问题?