Query by slug or query by id?

Typically, when I make applications that need bullets in the URL, I query slug in the database to get the content (of course, with an index in the slug field). Using a typical LAMP stack (with PHP and MySQL), what is the advantage or disadvantage of this in a database perspective? Would it make sense to always request an id and just create some type or route for the bullet? Can this application design create security issues?

I use cakePHP in particular, therefore, if there are any specific answers to the cake, this will be appreciated, but not necessary.

+4
source share
2 answers

stackoverflow.com/questions/ 5501961 / queries by unsubscribed or -query-by-ID

If you are absolutely sure that the mucus will not change, you can use it. But the numbers are probably faster and safer (that's for sure).

I just ran queries in the database with 1,000,000 rows, you can see that the ID is 230 times faster (the subject is not indexed, though). Also, as Kol Shrapnel said, what if you change the subject? Your URL will be corrupted and you will not be able to remember all changes to the topic. 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) 
0
source

Two things: Remember that slug values โ€‹โ€‹often need to be unique. In addition, using the i18n database schema, SQL queries in the table need an additional connection (slug is usually associated with the language)

0
source

Source: https://habr.com/ru/post/1346074/


All Articles