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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:44:46+00:00 2026-05-26T19:44:46+00:00

This is an efficiency/best practice question. Hoping to receive some feed back on performance.

  • 0

This is an efficiency/best practice question. Hoping to receive some feed back on performance. Any advice is greatly appreciated.

So here is a little background in what i have setup. I’m using codeigniter, the basic setup is pretty similar to any other product relationships. Basic tables are: Brands, products, categories. On top of these tables there is a need for install sheets, marketing materials, and colors.

I created some relationship tables:
Brands_Products
Products_Colors
Products_Images
Products_Sheets

I also have a Categories_Relationships table that holds all of the relationships to categories. Install sheets etc can have their own categories but i didn’t want to define a different category relationship table for each type because i didn’t think that would be very expandable.

On the front end I am sorting by brands, and categories.

I think that covers the background now to the efficiency part. I guess my question pertains mostly to weather it would be better to use joins or to make separate calls to return individual parts of each item (colors, images, etc)

What I currently have coded is working, and sorting fine but I think i can improve the performance, as it take some time to return the query. Right now its returning about 45 items. Here is my first function it grabs all the products and its info.

It works by first selecting all the products and joining it’s brand information. then looping through the result i set up the basic information, but for the categories images and installs i am using functions that returns each of respected items.

public function all()
    {
        $q = $this->db
                    ->select('*')
                    ->from('Products')
                    ->join('Brands_Products', 'Brands_Products.product_id = Products.id')
                    ->join('Brands', 'Brands.id = Brands_Products.brand_id')
                    ->get();

        foreach($q->result() as $row)
        {
            // Set Regular Data
            $data['Id'] = $row->product_id;
            $data['Name'] = $row->product_name;
            $data['Description'] = $row->description;
            $data['Brand'] = $row->brand_name;
            $data['Category'] = $this->categories($row->product_id);
            $data['Product_Images'] = $this->product_images($row->product_id);
            $data['Product_Installs'] = $this->product_installs($row->product_id);
            $data['Slug'] = $row->slug;


            // Set new item in return object with created data
            $r[] = (object)$data;
        }


        return $r;
    }

Here is an example of one of the functions used to get the individual parts.

private function product_installs($id)
    {
        // Select Install Images 
        $install_images = $this->db
                  ->select('*')
                  ->where('product_id', $id)
                  ->from('Products_Installs')
                  ->join('Files', 'Files.id = Products_Installs.file_id')
                  ->get();

        // Add categories to category object
        foreach($install_images->result() as $pImage)
        {
            $data[] = array(
                        'id' => $pImage->file_id, 
                        'src' => $pImage->src,
                        'title' => $pImage->title,
                        'alt' => $pImage->alt
                        );
        }

        // Make sure data exists
        if(!isset($data))
        {
            $data = array();
        }

        return $data;
    } 

So again really just looking on advice on what is the most efficient, best practice way of doing this. I really appreciate any advice, or information.

  • 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-26T19:44:47+00:00Added an answer on May 26, 2026 at 7:44 pm

    I think your approach is correct. There are only a couple of options: 1) load your product list first, then loop, and load required data for each product row. 2) create a big join on all tables first, then loop through (possibly massive) cartesian product. The second might get rather ugly to parse. For example, if you got Product A and Product B, and Product A has Install 1, Install 2, Install 3, and product B has Install 1, and Install 2,t hen your result is
    Product A Install 1
    Product A Install 2
    Product A Install 3
    Product B Install 1
    Product B Install 2

    Now, add your images and categories to the join and it might become huge.

    I am not sure what the sizes of your tables are but returning 45 rows shouldn’t take long. The obvious thing to ensure (and you probably did that already) is that product_id is indexed in all tables as well as your brands_products tables and others. Otherwise, you’ll do a table scan.

    The next question is how you’re displaying your data on the screen. So you’re getting all products. Do you need to load categories, images, installs when you’re getting a list of products? If you’re simply listing products on the screen, you might want to wait to load that data until user picks a products they are viewing.

    On a side note, any reason you’re converting your array to object

    $r[] = (object)$data;
    

    Also, in the second function, you can simply add

    $data = array();
    

    before the foreach, instead of

    // Make sure data exists
    if(!isset($data))
    {
        $data = array();
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a best practice/efficiency/performance issue. If I want to have a profile page
This is a web dev best practice question. I am envisioning a website where
This question has to do with best practices... please do not confuse it with
I'm looking for sort of a 'best practice' way to tackle this common scenario.
I've been doing some research on whether what best practice is regarding the use
In relation to this question ( Efficient hashCode() implementation ) I have one more
I'm wondering if theres a best practice for what I'm trying to accomplish... First
The implementing-result-paging-in-hibernate-getting-total-number-of-rows question trigger another question for me, about some implementation concern : Now
From an efficiency and best practices point of view, I appreciate everyones thoughts. I
One of the best practice is accessing data via stored procedures. I understand why

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.