Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6867611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:19:53+00:00 2026-05-27T03:19:53+00:00

let’s say I have three tables (MySQL), one for users, one for tags and

  • 0

let’s say I have three tables (MySQL), one for users, one for tags and a last one for joining them (many to many relation):

create table user_tag(
 user_id int unsigned not null,
 tag_id int unsigned not null,
 primary_key (user_id, tag_id)
);

I want to get out of the database a complete list of users (or at least many of them) along with the tags they have associated. I use PHP as a server language for that.

So my question is, is it better to perform one SQL query fetching all the information like this:

select user.name, user.image, ..., tag.name from user, tag, user_tag
   where user.user_id = user_tag.user_id and tag.tag_id = user_tag.tag_id;

or is it better to perform one first query for the users and afterwards fetch their tags:

select user.name, user.image, ... from user;

and then for each user:

select tag.name from tag, user_tag where tag.tag_id = user_tag.tag_id
   and user_tag.user_id = $USERID;

I feel like second is better option but I am afraid that can be too much queries for the database (NOTE that this is a general design example, but this pattern can be present multiple times over the database with different tables).

Which is better? Pros and Cons? Other ways?

Thanks

PS: please dont take into account SQL syntax, I haven’t checked it out against a real database, it is only design question, thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:19:54+00:00Added an answer on May 27, 2026 at 3:19 am

    I would only consider two options:

    1. One query:

      SELECT user.user_id, user.name, user.image, ..., tag.tag_id, tag.name
      FROM user
      LEFT JOIN user_tag ON user.user_id = user_tag.user_id
      LEFT JOIN tag ON user_tag.tag_id=tag.tag_id
      
    2. Two queries:

      SELECT user_id, name, image, ...
      FROM user
      
      SELECT user_tag.user_id, tag.tag_id, tag.name
      FROM user_tag
      INNER JOIN tag ON user_tag.tag_id=tag.tag_id
      

    If you have 100 users, there’s no point in issuing an identical query 100 times.

    I normally go for #1 when I’m only fetching a few columns and I go for #2 otherwise. In this case, you seem to be retrieving a pretty complete user profile so #2 sounds good.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have this MySQL table: OK.. see the type field? Type 0
Let's say I have the following models class Photo(models.Model): tags = models.ManyToManyField(Tag) class Tag(models.Model):
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let's say I have a C++ Visual Studio 2010 solution with 2 projects: one
Let's say i have a mysql database table 'article' with the following fields: id
Let's say I've got two tables: one with customer data, one with location data.
Let's say I have rows of data retrieved from a relational database tables (perhaps
Let's say i have this block of code, <div id=id1> This is some text
Let's say I have an image called hello.png with dimensions 200x100 . I create
Let's say for example i have URL containing the following percent encoded character :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.