Issue
Typically, when I make applications that need slugs in the URL, I query by the slug in the database to get the content (with an index on the slug field, of course). Using a typical LAMP stack (with PHP & MySQL), what is the advantage or disadvantage of doing this from a database perspective? Would it make more sense to always query by id and simply create some sort or route for slugs? Could this application design pose any security problems?
I'm using cakePHP, specifically, so if there are any cake-specific answers, that would be appreciated, but not necessary.
Solution
If you are absolutely positive that slug won't change, you can use it. But numbers are probably faster and safer (this one is for sure).
I just ran queries on a database with 1 000 000 rows, you can see that by ID is 230x faster (subject is not indexed though). Besides that, like Col Shrapnel said, what if you change the subject? Your URL will be broken, and you cannot remember every change in subject. Numbers are numbers, computers work with numbers :) JK
SELECT tid FROM `forum_threads` WHERE subject = "New BMW X5";
Showing rows 0 - 0 (1 total, Query took 0.0230 sec)
SELECT tid FROM `forum_threads` WHERE tid = 19906;
Showing rows 0 - 0 (1 total, Query took 0.0001 sec)
Answered By - Dejan Marjanović
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.