News for hackers. Stuff that matters.
Re:Ask HN: Why can't we just use this idea for clean URLs wi... by grzm on Friday November 04, 2016 @10:49PM (#12877901)
There's no reason you can't do that. Even more to the point, there's no reason to look up using a hash of the text. You can look it up using the slug directly.

People generally want (permanent) URIs to resources such as articles to be unchanging as you don't want links to break. This means that once you decide on a slug, you can't change it. If you want some correspondence between titles and slugs, this also constrains changing the title.

Including an arbitrary, context independent id in the URI means you can use that to look up the article and you're free to change the slug. You're not using the slug as part of the key used to look up the article in the database.

This also explains why using a hash of the slug would be effectively the same as using the slug itself, and with the same constraints.

Re:Ask HN: Why can't we just use this idea for clean URLs wi... by myf01d on Friday November 04, 2016 @11:05PM (#12877927)
If I address the sql table using the slug (let's say a slug is between 10 to 100 character), isn't it an overkill for performance instead of just looking for an int or bigint like in id or hash?

Maybe it won't be a difference for something like Redis (i.e. O(1) for any key) but it will be a performance hit for a sql containing hundreds of thousands of row, am I right?

Re:Ask HN: Why can't we just use this idea for clean URLs wi... by grzm on Friday November 04, 2016 @11:41PM (#12878027)
If you have an index on the column, it shouldn't be an huge issue. You'd have similar issues if you had no index on the integer column.

Of course, if you're really concerned about it, you can easily put together a test. I suspect other latencies in your app would be larger than those due to the difference between integer lookup and text lookup. But like I said: if you're highly sensitive to performance, you'll want to be measuring against your application, your stack, regardless of advice you might read about on the internet.

If beautiful URIs are a priority, you can use a separate mapping table or service (memcache? Redis?) that maps the URI to a database ID. Of course, this adds latency/overhead as well. This could also give you flexibility in supporting multiple slugs for a single story.