I am using Oracle 11g XE.
“Simple” question: which is better? Query A, B or C? I am trying to achieve the fastest response time of my query for resultset pagination. My pages will contain at most 20 results.
A:
SELECT /*+ FIRST_ROWS(20) */ *
FROM
((SELECT SCORE(1) RANK, F.ID, 1 AS "IsFile"
FROM "File" F
WHERE CONTAINS("ContentCLOB", 'April 2009', 1) > 0)
UNION ALL
(SELECT SCORE(2) RANK, C.ID, 0 AS "IsFile"
FROM "Claim" C
WHERE CONTAINS(C."IdClaim", 'jjkl', 2) > 0))
ORDER BY RANK DESC
B:
((SELECT /*+ FIRST_ROWS(20) */ SCORE(1) RANK, F.ID, 1 AS "IsFile"
FROM "File" F
WHERE CONTAINS("ContentCLOB", 'April 2009', 1) > 0)
UNION ALL
(SELECT /*+ FIRST_ROWS(20) */ SCORE(2) RANK, C.ID, 0 AS "IsFile"
FROM "Claim" C
WHERE CONTAINS(C."IdClaim", 'jjkl', 2) > 0))
ORDER BY RANK DESC
C:
Like B but without the hints.
The only difference in Explain Plans is that query A has an extra View before ordering.
adding first rows to the sub queries is usually a good idea (ignore the VIEW step, its there because on the first query you have “select .. from ()” which is the extra step).
I think the following will be better for you as I will guess that your SORT ORDER BY occurs after the table access. Also how are you paginating as I don’t see any rownum stuff in there (did you just simplify it for this case?).
the case at the end is only needed if you want ID and rowid isnt good enough. eg heres a small test:
versus
same result, but note the IO:
original:
delaying the table access till after the sort:
depending on your index/table size, the savings could be significantly more.