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

  • Home
  • SEARCH
  • 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 8428749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:08:29+00:00 2026-06-10T05:08:29+00:00

I have 5 tables in my database. [Items] – id, name, etc… [Categories] –

  • 0

I have 5 tables in my database.
[Items] – id, name, etc…
[Categories] – id, name
[Tags] – id, name

2 Joining tables
[Items_Categories] – item_id, category_id
[Items_Tags] – item_id, tag_id

I simply need to know the best performing query (using JOINS as necessary) to pull 1 or more items from the DB WITH all of it’s information, including the Categories AND Tags, given the item_id = $id.

So Far, I have the following which works, but on 25 – 50 queries, it was slow (Does anyone have something better?):

    SELECT `items`.`name`, `items`.`etc`,
group_concat(DISTINCT categories.name ORDER BY categories.name DESC SEPARATOR ", ") AS category, 
group_concat(DISTINCT tags.name ORDER BY tags.name DESC SEPARATOR ", ") AS tag, 
`items`.`id` AS id 
FROM (`items` AS items, `item_categories` AS categories, `items_to_categories` AS items_cats, `item_tags` AS tags, `items_to_tags` AS items_tags) 
JOIN `item_categories` ON `categories`.`id` = `items_cats`.`category_id` AND items_cats.item_id = $id 
JOIN `item_tags` ON `tags`.`id` = `items_tags`.`tag_id` AND items_tags.item_id = $id WHERE `items`.`id` = $id
  • 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-06-10T05:08:31+00:00Added an answer on June 10, 2026 at 5:08 am

    The problem is that you are actually doing a full cross join first (by listing the tables the way you did) instead of joining selectively “in order” and allowing the joins to be streamlined by the query plan. Try joining like this to explicitly join only the appropriate rows as it goes.

    SELECT 
      `items`.`name`, 
      `items`.`etc`,
      group_concat(DISTINCT `categories`.`name` ORDER BY `categories`.`name` DESC SEPARATOR ", ") AS category, 
      group_concat(DISTINCT `tags`.`name` ORDER BY `tags`.`name` DESC SEPARATOR ", ") AS tag, 
      `items`.`id` AS id 
    FROM `items`, 
      LEFT JOIN `item_categories` ON `items`.`id` = `item_categories`.`item_id`
      LEFT JOIN `categories` ON `item_categories`.`category_id` = `categories`.`id`,
      LEFT JOIN `item_tags` ON `items`.`id` = `item_tags`.`item_id`
      LEFT JOIN `tags` ON `item_tags`.`tag_id` = `tags`.`id` 
    WHERE `item`.`id` = $id
    GROUP BY `item`.`id`
    

    This will yield a lightening-fast query, and it is easily made even faster by adding the appropriate indexes. My philosophy, however, is that you should have a fast query first, and then get it wicked fast with indexing; not using indexes as a first-approach.

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

Sidebar

Related Questions

I have 2 tables in my database: CREATE TABLE [items]( [item_id] [int] IDENTITY(1,1) NOT
I have items and itemgroup tables in my database: CREATE TABLE [items]( [item_id] [int]
In a postgresql database I have 2 tables like these ones: Table items: item_id
I have five tables in my database. Members, items, comments, votes and countries. I
I have four tables in my database( notes , expense , category and items
I have two database tables that represent lists of items that belong to parent
I have three tables in my database: What I want is to get items
I'm currently facing a database query issue. I have two tables, items and details
I have two tables in a database Items and Details but when I do
I have two tables in a database one named purchases and one named items

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.