check for record and proceed with index only scan

40 views
Skip to first unread message

Jayadevan M

unread,
Jan 22, 2014, 3:50:29 AM1/22/14
to web...@googlegroups.com
I am checking for existence of record in a table

cond = db.mytable(id=someid)
if not cond :
  do something

This works fine. The query executed selects all columns and adds LIMIT 1 OFFSET 0; The LIMIT and OFFSET are OK. Since I want to just check for existence of a record, how can I rewrite this so that we have only the id column selected? I am trying to avoid a table scan and have just an index scan.

Johann Spies

unread,
Jan 22, 2014, 6:50:27 AM1/22/14
to web...@googlegroups.com
cond = db.mytable(id=someid).select(db.mytable.id)

Regards
Johann


--
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 the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Because experiencing your loyal love is better than life itself,
my lips will praise you.  (Psalm 63:3)

Johann Spies

unread,
Jan 22, 2014, 6:52:24 AM1/22/14
to web...@googlegroups.com
Oops. That should be
cond = db(db.mytable.id==someid).select(db.mytable.id)

Jayadevan M

unread,
Jan 22, 2014, 6:58:58 AM1/22/14
to web...@googlegroups.com
I tried that. I get the error -
<type 'exceptions.AttributeError'> 'Row' object has no attribute 'select'
By the way, the "="  works. Additional information, this function is under modules. Do I have to imprt something to make the select construct work?


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/XYeLmZjQ6fY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

Jayadevan M

unread,
Jan 22, 2014, 7:06:00 AM1/22/14
to web...@googlegroups.com
Sorry. It works (I got something wrong earlier) but the limit has gone away. I just have to ensure that there is ay least one record.

Jayadevan M

unread,
Jan 22, 2014, 7:35:22 AM1/22/14
to web...@googlegroups.com
Here is how it is now ..

1) cond = db.mytable(id=someid)
generates
SELECT  <all columns> FROM mytable WHERE (id = someid) LIMIT 1 OFFSET 0;

2) cond = db(db.mytable.id==someid).select(db.mytable.id,limitby=(0, 1))
generates
SELECT  <mycolumn> FROM mytable WHERE (id = someid)  ORDER BY pk LIMIT 1 OFFSET 0;

What I would like to have is

SELECT   <mycolumn> FROM mytable WHERE (id = someid) LIMIT 1 ;
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
--
Because experiencing your loyal love is better than life itself,
my lips will praise you.  (Psalm 63:3)



--
Because experiencing your loyal love is better than life itself,
my lips will praise you.  (Psalm 63:3)

--
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/XYeLmZjQ6fY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.

Anthony

unread,
Jan 22, 2014, 8:15:32 AM1/22/14
to web...@googlegroups.com
If you want to get rid of the orderby, do:

db(db.mytable.id == someid).select(db.mytable.id, limitby=(0, 1), orderby_on_limitby=False)

When you specify a limitby, by default it will orderby the PK (or add the PK to the orderby). The reason for this is to ensure a consistent ordering if you repeat the query or are using the limitby for pagination purposes (otherwise, successive queries are not guaranteed to return results in the same order).

Anthony
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
--
Because experiencing your loyal love is better than life itself,
my lips will praise you.  (Psalm 63:3)



--
Because experiencing your loyal love is better than life itself,
my lips will praise you.  (Psalm 63:3)

--
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/XYeLmZjQ6fY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages