What is an index?

What is an index in MySQL ????

Asked By: webnat0

Answer #1:

Indexes speed up SELECT queries because indexes are sorted by definition. Additionally a UNIQUE index enforces the constraint that the value of that column (or the combination of values of bound columns) exists only once (same applies to PRIMARY, but PRIMARY can only exist once per table in contrast to a UNIQUE key).

Indexes are a tradeoff: they tremendously speed up SELECT queries (when used columns have an INDEX) but they make that MySQL table consume more space and cost more time when changing the table via INSERT/UPDATE/DELETE.

Answered By: webnat0

Answer #2:

Indexes are just like an index of a book. Imagine you had a recipe book, and you wanted to find out how to make an omelet, you'd simply skip to the back, find the word and its page number, and skip to that page number. Now imagine you had no index and had to search through 400 pages of recipes, what a nightmare!

Indexes have several types Primary Key, Index, Unique, Fulltext

Primary Key is considered your main index, the first place mysql goes to find a record. Most people use an auto incrementing integer field for this since it's generally unique on each row.

Indexes are considered your secondary primary keys, you place these on fields that you want to be able to search for quickly.

Unique keys are similar to indexes however they work by ensuring that you cannot place duplicates in that column, eg you cant have the word 'eggs' appear in the same column on two different rows.

Finally Fulltext is a special index used by mysql's MyISAM storage engine only, it is used for searching your records using human phrases. without going into too much detail its mysqls own search engine, a more advanced version of the LIKE sql command.

For example if i searched for 'eggs and butter', fulltext would search for records containing eggs, butter, or both. where as LIKE would simply search for strings containing 'eggs and butter'

I hope this helps, the mysql site has plenty info on the subject, but this gives you the general gist of it.

Happy coding :)

Answered By: Robert

Answer #3:

Indexes are used for two things:

  1. Specify a field (or fields) that uniquely identifies a row (primary key).
  2. Save time during lookups on often used fields. For instance, if you often lookup users by their usernames you should add an index on the username column.

Read more about indexes here http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html or better buy a proper books on database design.

Regarding your second question, have a look at this http://dev.mysql.com/doc/refman/5.0/en/string-type-overview.html .

The main difference is that TEXT is stored outside of the table space and is referenced from the table, whereas VARCHAR is stored as a normal field. Performance in this case depends solely on your usage patterns. TEXT can also allow full text search.

Also, you cannot use an index on TEXT for the reasons stated above, so it is not a good idea to use it as a lookup field.

Answered By: studioromeo

Answer #4:

You need a primary key for every database table, and I think that is the primary index too. Indexes are used to speed up queries, and you should have an index of the column if you used it in the WHERE-part of an SQL-queries for performance reasons.

Using TEXT in cases where you don´t need it is bad. It is better to limit you users for some fields i.e. title and url, since you have an index on url. And I don't think you can use index on columns that are of type TEXT. I don´t think a FULLTEXT-index on url is what you want.

I would recommend you to read up about databases in an introductory text or website.

Answered By: Egor Pavlikhin

Answer #5:

Indexes are data structures used to optimise queries. They are conceptually very similar to the index at the back of a book. They cost some space and time to maintain, just as a book index would have to be redone if you decided to add a new chapter. But they often speed up queries enormously (up to thousands or even millions of times faster than without).

Answered By: Jonas

Answer #6:

If you know that everyone has a unique social security number (SSN), then you could create an index of everyone's name, ordered by SSN. Then, if you were given someone's SSN, it would be really quick to find their name.

Now assume that you have a manilla folder for each person. The card may have detailed medical records etc. Your database may not keep these cards in order - maybe it just adds new folders to the end of the compactus. But it keeps a sorted "index" so that if you look up an SSN, it can tell you exactly where the relevant folder is kept.

In this example, the SSN is being used as a Primary Key. It's nice if it's unique, but even if it isn't unique, it can still speed things up.

Answered By: Marcelo Cantos

Answer #7:

Indexes speed up select queries. They make the difference between the database having to check every single row in a table for the results and knowing exactly where to go to find the information. It's kind of the same concept as looping through a dictionary type data structure to find what you are looking for instead of just finding the value you want by it's key(index). Indexes will slow down Inserts, updates and deletes though if you have too many because every alteration of the table will now have to create the indexes as well.

A popular strategy in very very high volume/high performance applications is to have two databases. One that is optimally indexed for fast retrievals and another that has very few indexes for fast inserts. The only issue with this is, you lose real time data when the retrieval database has not synced yet with the insert database.

There are also two different kinds of indexes. Clustered and non-clustered indexes. If I'm not mistaken, in MySQL primary keys are clustered and all other indexes are non clustered. A good introduction article about the difference is here. It covers SQL Server, but the concept should be the same.

I haven't worked much with MySQL but from what I can see online, FULLTEXT provides a way to search using a Natural Language full text search. Basically, in the query, you will provide terms to search the full text indexed column(s) on and it will pull back all of the results. Here are a few articles I found on the topic you might find useful.
MySQL Docs on Full Text Search
Article 1
Article 2

Answered By: John
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

# More Articles