User Name
Password
AppleNova Forums » Programmer's Nook »

Please explain "index" in MySQL


Register Members List Calendar Search FAQ Posting Guidelines
Please explain "index" in MySQL
Thread Tools
nassau
Member
 
Join Date: Jul 2004
 
2006-10-04, 15:33

I have a vague idea of what an SQL index is and what it's good for, but, will someone knowledgable please explain it to me once and for all. My understanding is that an index is.. just that - an index. I understand it helps the database search faster on table-columns that are often searched.

thanks
  quote
Brad
Selfish Heathen
 
Join Date: May 2004
Location: Zone of Pain
 
2006-10-04, 15:35

Your understanding is correct. An index is simply an index that is used for speeding up SQL queries.

That's really all there is to it!

Say you have a large table that is sorted by column A, but not by columns B and C. If you need to do some selects based on the contents of B or C, you can create an index on that column to speed up the search.

The quality of this board depends on the quality of the posts. The only way to guarantee thoughtful, informative discussion is to write thoughtful, informative posts. AppleNova is not a real-time chat forum. You have time to compose messages and edit them before and after posting.
  quote
nassau
Member
 
Join Date: Jul 2004
 
2006-10-04, 15:56

but there must be drawbacks to the system, otherwise they would have put automatic indexes (indices?) on all columns, right?

does an index take up much space? what's the drawback?
  quote
Banana
is the next Chiquita
 
Join Date: Feb 2005
 
2006-10-04, 16:00

You just answered it. It does take space, and if you indexed everything, it would get quite confusing (e.g. if you had two indexes relating A with B and A with C, and you need B with C, which index do you want?)

Normally, I index my foreign keys, some relevant data such as dates, a certain person or group that I may use as a criteria in one of my queries.

HTH.
  quote
Brad
Selfish Heathen
 
Join Date: May 2004
Location: Zone of Pain
 
2006-10-04, 16:13

What Banana said. The only drawback is that it uses up more storage.

For small databases, it isn't a problem to have an index on every single column. You can do that if you want to. With larger databases that have thousand or millions or billions of records, though, you have to be a bit more careful with how you index.

The quality of this board depends on the quality of the posts. The only way to guarantee thoughtful, informative discussion is to write thoughtful, informative posts. AppleNova is not a real-time chat forum. You have time to compose messages and edit them before and after posting.
  quote
nassau
Member
 
Join Date: Jul 2004
 
2006-10-04, 16:26

but if i have unlimited storage space on my hosting account (which i do), theoretically i should index everything than, heh.

i gather from bananas post that you can combine indices of many columns. i've done that accidentally before and never really understood what i did..

thanks to both of you for the info!
  quote
Banana
is the next Chiquita
 
Join Date: Feb 2005
 
2006-10-04, 16:30

It's not just about storage, but also about performance.

When doing a query, engine (be it SQL, Access, Filemaker or Brad's Brain) runs through every relevant indices and assemble together the results. Now, if indexes are redundant, you can be sure that engines will likely go through them again, and even if they knew better, they would have to make sure they didn't miss out that and this records.

So even with an unlimited storage, I'll come back to check on your database in oh say, 20 years if you have a billion records that has all fields indexed.

Which is why you should index only fields that you mostly likely will be doing search on. Dates and Names are obvious example. But to index street address? Nothanksbyeverymuch!
  quote
nassau
Member
 
Join Date: Jul 2004
 
2006-10-04, 16:49

yea i think i get the idea now.
  quote
Posting Rules Navigation
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Post Reply

Forum Jump
Thread Tools
Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL v. PostgreSQL cooop Programmer's Nook 15 2006-07-20 14:18
Save arrays in MySQL nassau Programmer's Nook 2 2006-07-08 12:24
MySQL database disabled, how to "restart" it? drewprops Programmer's Nook 3 2005-12-31 19:17
MySQL in OSX nassau Programmer's Nook 2 2005-12-12 12:04
MySQL 5.0 Install on OS X v.10.4.1- need help with socket error DogMom Genius Bar 1 2005-07-11 17:00


« Previous Thread | Next Thread »

All times are GMT -5. The time now is 21:41.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004 - 2024, AppleNova