If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > Between and indexes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-28-10, 23:49
santana.beto santana.beto is offline
Registered User
 
Join Date: Apr 2010
Posts: 2
Between and indexes

Hello.

A friend of my told me that if I create an index to a column, the index is called only if the select matches only one row. In other words if I select using = it works but if I use between not.

Ex. He told me that this select will not use index.

select * from Product where id between 1000 and 2000;

Even if I have an index to id.

I really can't believe. Is he right?

Thanks in advance
Reply With Quote
  #2 (permalink)  
Old 04-29-10, 03:38
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
No - he is wrong.

It is possible the query won't use an index for that query, and note that an RDBMS can "use" an index in a few different ways but it depends on the specifics.

But he is categorically wrong to say an index is only "used" if the query matches only one row. Ask him then why the engine will let him index non-unique columns?

What is you RDBMS? In some of them you can look at the plans and see how the engine uses the index.
Reply With Quote
  #3 (permalink)  
Old 04-29-10, 06:06
dportas dportas is offline
Registered User
 
Join Date: Dec 2007
Location: London, UK
Posts: 732
Quote:
Originally Posted by santana.beto View Post
A friend of my told me that if I create an index to a column, the index is called only if the select matches only one row. In other words if I select using = it works but if I use between not.
As a general rule he's wrong, or at least too simplistic. The way indexes are used is highly dependent on your DBMS, the nature of the query, the data and the type of index. Perhaps your friend is thinking specifically of hash indexes which only work for comparisons based on equality and not on range queries.

The number of rows returned by a query can also be a factor in whether an index gets used or not. If the query returns a large proportion of the rows from a table then the query optimiser may find it more efficient not to use an index.
Reply With Quote
  #4 (permalink)  
Old 04-29-10, 06:09
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Quote:
Originally Posted by dportas View Post
The number of rows returned by a query can also be a factor in whether an index gets used or not. If the query returns a large proportion of the rows from a table then the query optimiser may find it more efficient not to use an index.
Unless it covers the query or is clustered in which case it is likely to find it efficient.
Reply With Quote
  #5 (permalink)  
Old 04-29-10, 07:24
santana.beto santana.beto is offline
Registered User
 
Join Date: Apr 2010
Posts: 2
Thank you very much

Thank you for all responses
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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On