Thank you for taking the time to sort out my query! (The meat is at the
bottom.)

>     SELECT
>         r.qi,
>         r.ri,
>         r.drl,
>         r.score
>     FROM
>         results_1 r
>         INNER JOIN bounds b ON
>             r.qis = b.bqis AND r.ris = b.bris
>     WHERE
>         (r.qi = 5604 OR r.ri = 5468)
>         AND (r.qi >= b.bqis AND r.qi <= 5604)
>         AND (r.ri >= b.bris AND r.ri <= 5468)
>         AND b.bi = 1
>     GROUP BY
>         r.score DESC
>     LIMIT 1

This one above was as slow as the original. 

>     SELECT
>         r.qi,
>         r.ri,
>         r.drl,
>         r.score
>     FROM
>         results_1 r
>     WHERE
>         (r.qi = 5604 AND r.ri <= 5468) OR (r.ri = 5468 AND 
> r.qi <= 5604)
>         AND EXISTS (
>             SELECT
>                 NULL
>             FROM
>                 bounds b
>             WHERE
>                 b.bi = 1
>                 AND b.bqis = r.qis
>                 AND b.bris = r.ris
>                 AND b.bqis <= r.qi
>                 AND b.bris <= r.ri
>         )
>     ORDER BY
>         r.score DESC
>     LIMIT 1

But this one was twice as fast! I was able to use Mr. Cote's suggestion of
EXPLAIN QUERY PLAN to look at the indexes being used and make it run even
faster. My results were interesting, though. I had an index on results_1
that was both (qi,ri), call it coord. That was being unused in the above
query. I added a qi index and that made the above query use coord.
Apparently it doesn't matter if I add an index for qi or ri or both, any of
those options makes it use the coord index. I don't understand exactly why
on that. I tried adding an index on score. That seemed to speed up queries
where there were lots of scores, but it slowed down queries where there were
few scores.

Reply via email to