tags:

views:

47

answers:

3

I have a CMS that has two menthods to query contents. One that queries by id and another one queries by the name of the content.

ContentManager.Select(12);

or

ContentManager.Select("Content Name");

The way I see the first one would be faster, because the id is an index and doesn't invlove string comparison. While the second one is much easier to work with.

I have worked, for mainatance reasons, with the second one. But if i change the content name, the Select obviously is not going to work anymore. But the Id is supposed to be only o database level, and not visible from the CMS forms.

Edit:Also, if a content were to be deleted and reinserted the string select would work and the id select wouldn't.

I can't come to a common point between this two approachs.

Thank you

+1  A: 

Selecting by the primary key gives best possible performance, but that's not always your only motivation. You might be able to add an index to the content name column, depending on it's width and your read/write ration (and depending on how much control over the database you have, I suppose).

Verdict, if you have the id, select by the id, if you don't and it's not ruining your performance, don't sweat using the content name.

marr75
PK searches are not 'always' best... Depends on the index. if PK is five column composite key which is 1000 bytes wide, and another index exists that is on a surrogate integer key only 4 bytes wide, then searching on the surrogate alternate key would be faster
Charles Bretana
@Charles: Then why would you make that your primary key, and not merely a unique constraint? I would never do that, unless your database implementation always uses the primary key as a clustered index or something.
Thorarin
I was limiting my example to the very simple discussion we're having of 2 columns that both, as far as I can tell, uniquely identify a row. Technically in this case, the 'id' is a surrogate key of the string column, if in fact the string column uniquely identifies the row. The 'id' column just happens to have the clustered index on it, so can you do a lot of weird stuff to screw up a primary key and make it's searches less than ideal? Absolutely. But in the general case with a clustered index primary key on an integer, most likely auto-incremented column, search by id is fastest.
marr75
A: 

Depends on which one is indexed... So yes you are right, in this case use the ID... If there is a need to also search by name, add another index using the name..

Charles Bretana
The `ID` version would still be faster if that were the column used for the clustered index. Assuming you're retrieving the entire row that is.
Thorarin
A: 

IDs ususally work best in databases. However, you are at the mercy of the CMS, it might be storing both those in an array and using the same exact select statement. Who knows? view the source code and see what is going on.

whatever you do, stick to one style in all of your code.

KM