fastest way to count - check for mere existence of any record

41 views
Skip to first unread message

Alex Glaros

unread,
Mar 6, 2015, 12:12:29 PM3/6/15
to web...@googlegroups.com
Anyone have syntax to optimize speed of count, to check record existance?

Often just want to check for the existence of ANY records and don't need a full count, but I only know how to get info from full count.  See below where logic only cares if count is greater than zero.

    checking_for_already_existing_connection_count = db((db.VerifiedConnection.personID == personID.id) & (db.VerifiedConnection.connectedTo == auth.user_id)).count()
    if checking_for_already_existing_connection_count > 0:
        redirect(URL('error_duplicate_role'))

Do you have phrasing that perhaps includes ".select(limitby=(0,1)).first()"?

thanks,

Alex Glaros

Alex Glaros

unread,
Mar 6, 2015, 12:26:00 PM3/6/15
to web...@googlegroups.com
am looking for something like below

    if count(db((db.VerifiedConnection.personID == personID.id) & (db.VerifiedConnection.connectedTo == auth.user_id)).select(limitby=(0,1)).first()) > 0:
        redirect(URL('error_duplicate_role'))


Massimo Di Pierro

unread,
Mar 6, 2015, 1:07:59 PM3/6/15
to web...@googlegroups.com
How about just fetching the id?

if count(db((db.VerifiedConnection.personID == personID.id) & (db.VerifiedConnection.connectedTo == auth.user_id)).select(db.VerifiedConnection.id,limitby=(0,1)).first()) > 0:

Alex Glaros

unread,
Mar 6, 2015, 1:43:33 PM3/6/15
to web...@googlegroups.com
not sure how to structure that syntax Massimo...would this be okay?

    if (db((db.VerifiedConnection.personID == personID.id) & (db.VerifiedConnection.connectedTo == auth.user_id)).select(limitby=(0,1)).first()):
        redirect(URL('error_duplicate_role'))

Alex Glaros

unread,
Mar 6, 2015, 2:19:54 PM3/6/15
to web...@googlegroups.com
oops, meant this

if (db((db.VerifiedConnection.personID == personID.id) & (db.VerifiedConnection.connectedTo == auth.user_id)).select(db.VerifiedConnection.id, limitby=(0,1)).first()):


Niphlod

unread,
Mar 6, 2015, 3:02:28 PM3/6/15
to web...@googlegroups.com
by any chance, is isempty() being totally forgotten ? it just does

return not self.select(limitby=(0,1), orderby_on_limitby=False)

Alex Glaros

unread,
Mar 6, 2015, 4:17:38 PM3/6/15
to web...@googlegroups.com
does this look okay?

if not (db((db.VerifiedConnection.personID == personID.id) & (db.VerifiedConnection.connectedTo == auth.user_id))).isempty():


Niphlod

unread,
Mar 6, 2015, 5:54:46 PM3/6/15
to web...@googlegroups.com
yep.

db(query).isempty() returns True if no records are found, in the uttermost simplicistic and speed-aware method I know, that is basically

db(query).select(limitby=(0,1), orderby_on_limitby=False)

Alex Glaros

unread,
Mar 6, 2015, 6:04:52 PM3/6/15
to web...@googlegroups.com
thanks Niphlod and Massimo

works great

Alex

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/jiQMVzMo2G4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages