Welcome Guest, Not a member yet? Register   Sign In
MySQL retrieve method question.
#1

[eluser]Unknown[/eluser]
Hello everyone,

I just have a quick question regarding Key usage in MySQL.
In the documentation under the "News" tutorial it tells us to create the following:
Code:
CREATE TABLE news (
id int(11) NOT NULL AUTO_INCREMENT,
title varchar(128) NOT NULL,
slug varchar(128) NOT NULL,
text text NOT NULL,
PRIMARY KEY (id),
        KEY slug (slug)
);

My question is, why is it necessary to create "KEY slug (slug)"?
Why not just retrieve the slug by the slug content in the column name?
For example "SELECT * FROM news WHERE slug = '$slug'"

In other words, I am curious to know the need for making slug a "KEY" in the MySQL table.
I am somewhat of a newbie, any response is appreciated.
#2

[eluser]CroNiX[/eluser]
Because it will be found a lot faster if that key is indexed, and since you will be loading the entry by the slug name, it makes sense to index it for faster retrieval.
#3

[eluser]InsiteFX[/eluser]
KEY creates an index, which speeds it up.

#4

[eluser]Unknown[/eluser]
[quote author="CroNiX" date="1330292865"]Because it will be found a lot faster if that key is indexed, and since you will be loading the entry by the slug name, it makes sense to index it for faster retrieval.[/quote]

[quote author="InsiteFX" date="1330293060"]KEY creates an index, which speeds it up.[/quote]

That explains a lot, thank you for the insight!




Theme © iAndrew 2016 - Forum software by © MyBB