Forum Moderators: coopster

Message Too Old, No Replies

Which of these queries is better to use?

         

trigoon

6:01 pm on Feb 16, 2009 (gmt 0)

10+ Year Member



For one of my scripts I want to use a SEO friendly URL that involves something like this:

http://example.com/page-title.html

This may be a bit hard to explain so I'll give an example:

MySQL Database Structure:
id ¦ seo ¦content
1 ¦ page-title ¦ the page content here
2 ¦ page_no_2 ¦page #2's content here
etc...

Someone goes to http://example.com/page-title.html (I have the htaccess code etc setup for all this).

The script gets the variable "page-title" from the URL and does a mysql search to find the content of that page.

I want to avoid using the id for this as it just seems cleaner to me to use something like above rather than domain.com/1/page-title.html

Now my real question is whats a better thing to do:

- Run 2 queries, the first something like:
"SELECT `id` FROM table_name WHERE seo = 'page-title' LIMIT 1"

and then get the ID and do another query:
"SELECT * FROM table_name WHERE id = '1' LIMIT 1"
To get the rest of the items from the database?

Or should I just do the first query and get all of the information I need from that?
("SELECT * FROM table_name WHERE seo = 'page-title' LIMIT 1")

Thanks! (Any other ideas on how I can optimize the above would be great too!)

[edited by: coopster at 6:22 pm (utc) on Feb. 16, 2009]
[edit reason] please use example.com, thanks! [/edit]

IanKelley

2:01 am on Feb 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Do the single query, make sure the SEO column is indexed.

trigoon

1:15 pm on Feb 17, 2009 (gmt 0)

10+ Year Member



Thanks, should I use a unique index or a normal index?

coopster

1:30 pm on Feb 17, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It has to be a unique column or you are going to have the possibility of duplicate rows in your table.