views:

58

answers:

4

I am creating a site that allows users to share specific pages to the public. It is similar to how jsbin.com let's you create a public url of the script you're working on. The basic MySQL table I am working with now is:

CREATE TABLE IF NOT EXISTS `lists` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `hash` varchar(6) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `hash` (`hash`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

The key field is the column that holds the random string the user will type at the end of the URL. So, for example, if the user publicized there page with the hash value of a1b2c3 then the URL would be http://mysite.com/a1b2c3

OK, sorry the description took so long. My question is, should I index the hash column? Will this make queries faster, as I am primarily going to look rows up by their hash value?

Also, I have another table that has a foreign key relationship to this one. Does it make sense performance-wise to relate it to the hash column?

Thanks for the help.

A: 

Yes, if you are looking up by the hash, then you most certainly would want to have an index on that field. By the looks of your table design, you would probably want it to be a unique index as well.

Eric Petroelje
A: 

Yes, you definitely should index the hash column and, which is very important, disable the key compression.

The key compression is a feature in MyISAM indexes that shrinks the size of a text index by storing only the length of the common prefix in the index keys.

This may help to improve performance on the human language phrases, but is not going to help for the hashes.

This article from my blog that compares performance of index lookups for the hash-like strings with key compression on and off:

To disable key compression, add PACK_KEYS = 0 to the CREATE TABLE statement.

Update:

This statement in your CREATE TABLE:

UNIQUE KEY `hash` (`hash`)

effectively creates a UNIQUE index on hash, so you already have it. Just make sure that the key compression is off.

Quassnoi
+1  A: 

Not only should you index on the hash column, you should probably make it the primary key and get rid of the redundant id column.

Are there some other tables and/or columns you didn't list here? This table has little point outside of the created_at column.

Jeffrey Hantin
I forgot to mention that another table has a foreign key many-to-one relationship to this table. One other question I had was if the hash column is the primary key, is it ok to have a foreign key refer to this hash column?
Zach Smith
There might be performance considerations regarding using a `varchar` field as a key, but it shouldn't be prohibited. What is the hash derived from, how long is it expected to be, and how many rows are you expecting in the table?
Jeffrey Hantin
It does make sense to have a primary key in one table (i.e. your unique hash column) that is referenced by a foreign key in another table.
Summer
The hash is randomly derived from lower and upper case letters and numbers. I will probably make it 6 characters long. Rows will probably be deleted after about 3 months or so automatically, so I will probably only have about 400 rows at one time. Thanks for so much help on this!
Zach Smith
You may want to consider making it a random integer instead, and encode it base-36 in the URL. Calling it 'hash' implies that it is a function of some other data rather than a randomly-assigned identifier; this also gives you the speed of an integer key.
Jeffrey Hantin
That's a great idea. I will probably go with that. Thanks again!
Zach Smith
A: 

Yes. It's important to add UNIQUE INDEX for that column. First of all you have to be sure that it's unique (well... it's kind of ID) and you'll do a lot of queries like following:

SELECT .... FROM .... WHERE hash = 'abc323';
Crozin