treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Inner join vs two select statements

  • Hey peeps!

    Not so much a php problem, more of a general SQL performance question.

    So i'll have two tables, one with say user information with a unique userID, and another table with all other kinds of meta-data along with which user created it (the userID again).

    Now, when i want to display the username of the person (which is in the user-table) based off the userID in the meta-data table:
    Would i be better off using an inner join statement fetching the data i need from the users table, or just do it say via a php function that returns me a username based off of a userID (essentially another db-query)?

    It's not really gonna be huge amounts of traffic, i'm just sort of wondering what the best approach would be performance-wise anyways.

    Cheers for now!
  • Always a join for this kind of thing. The fewer SQL queries you have to perform on a page the better performance will be, within reason.
  • Yeah that's kind of what i figured as well. It's just that it's kind of easy and handy programmingwise later just having a function for fetching usernames etc, but i think i'll just make a habit out of always fetching usernames etc via Inner Joins in the future on those kind of queries.

    Cheers for the reply. :)