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 6967561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:20:16+00:00 2026-05-27T16:20:16+00:00

I came across an article about Join decomposition. SCENARIO #1 (Not good): Select *

  • 0

I came across an article about Join decomposition.

SCENARIO #1 (Not good):

Select * from tag
Join tag_post ON tag_post.tag_id=tag.id
Join post ON tag_post.post_id=post.id
Where tag.tag='mysql'

SCENARIO #2 (good):

Select * from tag where tag='mysql'

Select * from tag_post Where tag_id=1234

Select * from post where post.id in (123,456,9098,545)

It was suggested to stick to scenario #2 for many reasons specially caching.
The question is how to join inside our application. Could u give us an example with PHP
after retrieving them individually?
(I have read MyISAM Performance: Join Decomposition?
but it did not help)

  • 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-27T16:20:17+00:00Added an answer on May 27, 2026 at 4:20 pm

    You COULD use an SQL subselect (if I understand your question). Using PHP would be rather odd while SQL has all the capabilities.

    SELECT *
    FROM `post`
    WHERE `id` IN (
        SELECT `post_id`
        FROM `tag_post`
        WHERE `tag_id` = (
            SELECT `tag_id`
            FROM `tag`
            WHERE `tag` = 'mysql'
        )
    )
    

    I’m not sure how your database structure looks like, but this should get you started. It’s pretty much SQL inception. A query within a query. You can select data using the result of a subselect.

    Please, before copying this SQL and telling me it’s not working, verify all table and column names.

    Before anyone starts to cry about speed, caching and efficiency: I think this is rather efficient. Instead of selecting ALL data and loop through it using PHP you can just select smaller bits using native SQL as it was ment to be used.

    Again, I highly discourage to use PHP to get specific data. SQL is all you need.


    edit: here’s your script

    Assuming you have some multi-dimensional arrays containing all data:

    // dummy results
    
    // table tag
    $tags = array(
        // first record
        array(
            'id'    => 0,
            'tag'   => 'mysql'
        ), 
        // second record
        array(
            'id'    => 1,
            'tag'   => 'php'
        )
        // etc
    );
    
    // table tag_post
    $tag_posts = array(
        // first record
        array(
            'id'        => 0,
            'post_id'   => 0,   // post #1
            'tag_id'    => 0    // has tag mysql
        ),
        // second record
        array(
            'id'        => 1,
            'post_id'   => 1,   // post #2
            'tag_id'    => 0    // has tag mysql
        ),
        // second record
        array(
            'id'        => 2,
            'post_id'   => 2,   // post #3
            'tag_id'    => 1    // has tag mysql
        )
        // etc
    );
    
    // table post
    $posts = array(
        // first record
        array(
            'id'        => 0,
            'content'   => 'content post #1'
        ),
        // second record
        array(
            'id'        => 1,
            'content'   => 'content post #2'
        ),
        // third record
        array(
            'id'        => 2,
            'content'   => 'content post #3'
        )
        // etc
    );
    
    // searching for tag
    $tag = 'mysql';
    $tagid = -1;
    $postids = array();
    $results = array();
    
    // first get the id of this tag
    foreach($tags as $key => $value) {
        if($value['tag'] === $tag) {
            // set the id of the tag
            $tagid = $value['id'];
    
            // theres only one possible id, so we break the loop
            break;
        }
    }
    
    // get post ids using the tag id
    if($tagid > -1) { // verify if a tag id was found
        foreach($tag_posts as $key => $value) {
            if($value['tag_id'] === $tagid) {
                // add post id to post ids
                $postids[] = $value['post_id'];
            }
        }
    }
    
    // finally get post content
    if(count($postids) > 0) { //verify if some posts were found
        foreach($posts as $key => $value) {
            // check if the id of the post can be found in the posts ids we have found
            if(in_array($value['id'], $postids)) {
                // add all data of the post to result
                $results[] = $value;
            }
        }
    }
    

    If you look at the length of the script above, this is exactly why I’d stick to SQL.

    Now, as I recall, you wanted to join using PHP, rather doing it in SQL. This is not a join but getting results using some arrays. I know, but a join would only be a waste of time and less efficient than just leaving all results as they are.


    edit: 21-12-12 as result of comments below

    I’ve done a little benchmark and the results are quite stunning:

    DATABASE RECORDS:
    tags:           10
    posts:          1000
    tag_posts:      1000 (every post has 1 random tag)
    
    Selecting all posts with a specific tag resulted in 82 records.
    
    SUBSELECT RESULTS:
    run time:                        0.772885084152
    bytes downloaded from database:  3417
    
    PHP RESULTS:
    run time:                        0.086599111557
    bytes downloaded from database:  48644
    
    
    
    Please note that the benchmark had both the application as the database on the
    same host. If you use different hosts for the application and the database layer,
    the PHP result could end up taking longer because naturally sending data between
    two hosts will take much more time then when they're on the same host.
    

    Even though the subselect returns much less data, the duration of the requests is nearly 10 times longer…

    I’ve NEVER expected these results, so I’m convinced and I will certainly use this information when I know that performance is important however I will still use SQL for smaller operations hehe…

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

Sidebar

Related Questions

Just came across this article about NOSQL patterns (not mine). It's covers lots of
I came across an article about Car remote entry system at http://auto.howstuffworks.com/remote-entry2.htm In the
I came across this article on Code Project that talks about using an abstract
I came across this article about detecting VMWare or Virtual PC http://www.codeproject.com/KB/system/VmDetect.aspx and I
I came across a php article about regular expressions which used (.*?) in its
I was searching the archives and came across the following article about how to
Hi I just came across an article about how to combine Rails with jQuery.
About a month ago, I came across an amazing blog post about the challenges
I just came across this article about WPF performance on Touch/Tablet devices. In the
I learning about writing my own interfaces and came across the MSDN article Interfaces

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.