Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search for row

Status
Not open for further replies.

djam

Technical User
Nov 15, 2002
223
CA
I was wondering if there was a more efficient way to perform this search.

"select count (*) as num from customer where userid = 2";

I would like to only find the first occurrance of this and exit after, but the sql that I have will check the whole table.

thanks " ahhh computers, how they made our lives much simpler ;) "
 
I could not understand your question
if you want the first occurence why are you
selecting count(*), that gives you a number and not
an occurence

If you want to select count(*) many a times
I would suggest that you use a function. They get cached in memory and are much better than sql statements
 
Hi djam,

If the table is large, you could have an index on the relevant field. Read the manual about the "limit" command that you could use to return only the first row found.

LelandJ Leland F. Jackson, CPA
Software - Master (TM)
Nothing Runs Like the Fox
 
userid is a primary key, so when i do find the occurence, i would like to stop looking " ahhh computers, how they made our lives much simpler ;) "
 
Hi,

The postgres 7.3 doc states postgres take "limit" into conisderation when forming its execution plan which lead me to believe that in the select statement below would complete when the first occurance to meet your criteria were found. You could give it a try to see if it works for you.


********************************************************
SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
* | expression [ AS output_name ] [, ...]
[ FROM from_item [, ...] ]
[ WHERE condition ]
[ GROUP BY expression [, ...] ]
[ HAVING condition [, ...] ]
[ { UNION | INTERSECT | EXCEPT } [ ALL ] select ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [, ...] ]
[ LIMIT { count | ALL } ]
[ OFFSET start ]
[ FOR UPDATE [ OF tablename [, ...] ] ]
******************************************************

select * from the_table where userid = 1 limit 1;



LIMIT Clause

LIMIT { count | ALL }
OFFSET start

where count specifies the maximum number of rows to return, and start specifies the number of rows to skip before starting to return rows.

LIMIT allows you to retrieve just a portion of the rows that are generated by the rest of the query. If a limit count is given, no more than that many rows will be returned. If an offset is given, that many rows will be skipped before starting to return rows.

When using LIMIT, it is a good idea to use an ORDER BY clause that constrains the result rows into a unique order. Otherwise you will get an unpredictable subset of the query's rows---you may be asking for the tenth through twentieth rows, but tenth through twentieth in what ordering? You don't know what ordering unless you specify ORDER BY.

As of PostgreSQL 7.0, the query optimizer takes LIMIT into account when generating a query plan, so you are very likely to get different plans (yielding different row orders) depending on what you use for LIMIT and OFFSET. Thus, using different LIMIT/OFFSET values to select different subsets of a query result will give inconsistent results unless you enforce a predictable result ordering with ORDER BY. This is not a bug; it is an inherent consequence of the fact that SQL does not promise to deliver the results of a query in any particular order unless ORDER BY is used to constrain the order.

LelandJ Leland F. Jackson, CPA
Software - Master (TM)
Nothing Runs Like the Fox
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top