|
I had a program that was running in less than a second, and I had to
add a check to it to see if a record existed in another file. I used
a SELECT with the FETCH 1 ROW ONLY clause (I wasn't aware of the
"FIRST" option, although that shouldn't make a difference in this
case). The program took 15 minutes. I replaced the SELECT with SELECT
COUNT(*) and the program was back to sub-second completion. Now I
know there are a myriad factors in play, a couple of which being in
my favor were that the majority of records would NOT have a record in
the second file and those that did would most likely only have one
and occasionally a few. In my particular experience, I've never found
a SELECT COUNT(*) to be a performance hog, which I've always
attributed, in my admitted ignorance of the actual mechanics, to be
due to the fact that it doesn't care anything about having to
manipulate any data.
CRPence on Friday, November 02, 2012 3:55 PM wrote:
<<SNIP>>
Both an EXISTS predicate and the FETCH FIRST 1 ROW ONLY clause
ask via the SQL that the database should stop searching after
encountering the first matching row. If one could be positive
that the query implementation will be with a known compatible
INDEX which can be /probed/ for the existence of the row(s) and
the database easily be able to know [all key values] or quickly
count the rows [partial key] under a particular node [which as
/random access/ that the data is effectively
clustered\already-paged would be desirable]. But if one can
presume so much, then selecting only [any of the key columns or]
a literal value enables an index-only implementation [possibly
same as the noted /fast/ COUNT(*) queries] for which no actual
row data is accessed to conclude the query. With either of these
non-aggregate queries, the implication is that the [sub]query
will be performed as though the /hint/ OPTIMIZE FOR 1 ROW has
been coded on the SQL request.
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.