I have the following query
SELECT * FROM A JOIN B ON A.a=B.b JOIN C ON A.a=C.c.
The above query return million rows and takes lot of time to return results. How can I see just first "N" records so that the query runs faster.
I have the following query
SELECT * FROM A JOIN B ON A.a=B.b JOIN C ON A.a=C.c.
The above query return million rows and takes lot of time to return results. How can I see just first "N" records so that the query runs faster.
In Oracle, you would just add rownum
to the where
clause:
SELECT *
FROM A JOIN
B
ON A.a = B.b JOIN
C
ON A.a = C.c
WHERE rownum < 100;
You can use hint first_rows:
SELECT /*+ FIRST_ROWS(100) */ * FROM A JOIN B ON A.a=B.b JOIN C ON A.a=C.c
Also check if you have indices on A.a, B.b and C.c.
As an addition you should understand that any sorting(order by
), or analytic function, or group by
operation, or distinct
(maybe something else as well which requests the whole data set, e.g. UNION
or INTERSECT
) will destroy this hint and anyway will collect the whole dataset. They simply cannot work without it: e.g. how to order the data set if there is not the whole list presented?
You can get desired number of record with TOP Clause. Here is an example:-
SELECT TOP n *
FROM A
JOIN B ON A.a=B.b
JOIN C ON A.a=C.C