Bluehost.com Web Hosting $6.95

is limit 1 needed on a query on and auto_increment or unique column

This is a discussion on is limit 1 needed on a query on and auto_increment or unique column within the MySQL Database forums, part of the Database Forums category; Say I have a simple table with an ordinary auto_increment column called id. Is MySQL smart enough to know that ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-18-2006
lynn.newton@gmail.com
 
Posts: n/a
Default is limit 1 needed on a query on and auto_increment or unique column

Say I have a simple table with an ordinary auto_increment column called
id.
Is MySQL smart enough to know that a query like

select * from mytab where id=123

will have only one match, or is it always a good idea to append limit 1
to such queries? If mysql knows (and my guess is that it probably
does),
then does adding limit 1 actually do anything to slow down the query?

The same question would apply to a table with a column defined with
the UNIQUE constraint.

Reply With Quote
  #2 (permalink)  
Old 12-19-2006
Jerry Stuckle
 
Posts: n/a
Default Re: is limit 1 needed on a query on and auto_increment or uniquecolumn

lynn.newton@gmail.com wrote:
> Say I have a simple table with an ordinary auto_increment column called
> id.
> Is MySQL smart enough to know that a query like
>
> select * from mytab where id=123
>
> will have only one match, or is it always a good idea to append limit 1
> to such queries? If mysql knows (and my guess is that it probably
> does),
> then does adding limit 1 actually do anything to slow down the query?
>
> The same question would apply to a table with a column defined with
> the UNIQUE constraint.
>


Unless this column is the table's primary key or has a unique index,
even an autoincrement column can have duplicates.

Autoincrement is only the default. You can still specify your own
values on an insert or update statement.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
  #3 (permalink)  
Old 12-20-2006
lynn.newton@gmail.com
 
Posts: n/a
Default Re: is limit 1 needed on a query on and auto_increment or unique column

> Say I have a simple table with an ordinary
> auto_increment column called id. Is MySQL smart
> enough to know that a query like


> select * from mytab where id=123


> will have only one match, or is it always a good
> idea to append limit 1 to such queries? If mysql
> knows (and my guess is that it probably does), then
> does adding limit 1 actually do anything to slow
> down the query?


> The same question would apply to a table with a
> column defined with the UNIQUE constraint.


> Unless this column is the table's primary key or has a unique index,
> even an autoincrement column can have duplicates.


> Autoincrement is only the default. You can still specify your own
> values on an insert or update statement.


You are right. But suppose it *is* a primary key or has
a unique index, is adding "limit 1" superfluous, i.e.,
does MySQL know to stop searching for matches after
finding one because it knows by the table definition
that there can't be any more?

Reply With Quote
  #4 (permalink)  
Old 12-20-2006
Jerry Stuckle
 
Posts: n/a
Default Re: is limit 1 needed on a query on and auto_increment or uniquecolumn

lynn.newton@gmail.com wrote:
> > Say I have a simple table with an ordinary
> > auto_increment column called id. Is MySQL smart
> > enough to know that a query like

>
> > select * from mytab where id=123

>
> > will have only one match, or is it always a good
> > idea to append limit 1 to such queries? If mysql
> > knows (and my guess is that it probably does), then
> > does adding limit 1 actually do anything to slow
> > down the query?

>
> > The same question would apply to a table with a
> > column defined with the UNIQUE constraint.

>
>
>>Unless this column is the table's primary key or has a unique index,
>>even an autoincrement column can have duplicates.

>
>
>>Autoincrement is only the default. You can still specify your own
>>values on an insert or update statement.

>
>
> You are right. But suppose it *is* a primary key or has
> a unique index, is adding "limit 1" superfluous, i.e.,
> does MySQL know to stop searching for matches after
> finding one because it knows by the table definition
> that there can't be any more?
>


It will stop searching for matches because there is only one in the
primary (or unique) index. It won't find any more.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 04:12 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0