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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:32:19+00:00 2026-05-26T14:32:19+00:00

First of all, I have no control over the database structure, etc. I need

  • 0

First of all, I have no control over the database structure, etc.

I need to use PHP to retrieve records by state and name but all the data is in multiple tables. Basically I need to try and understand how to get these two queries combined so that they do not run so slow.

First I get my record ID’s in PHP. (Lets assume $query returns an array of IDs)

table_products = A bunch of products

$query = "SELECT id,name FROM table_products WHERE name = '".$name."';";

Than I need to iterate through these records (NOTE : There can be A LOT) and figure out where these IDs reside in another two tables that has the location information of where they could be at.

table_places = a table with a bunch of locations

link_table = Contains the relationships between product and location

$state = "somestate";

foreach($query as $row)
    {
    $query_two = "SELECT table_places.name, table_places.id, table_places.state, link_table.place_id, link_table.product_id 
    FROM table_places 
    INNER JOIN link_table 
    ON table_places.id = link_table.place_id 
    WHERE table_places.state = '".$state."' AND link_table.product_id = '".$row->id."';";
    }

God I hope this made sense. I am no query guru so if I could get assistance in optimizing this to run faster, I would be grateful.

  • 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-26T14:32:20+00:00Added an answer on May 26, 2026 at 2:32 pm

    The pain is here:

    foreach($query as $row)   <<--- you are pinging the DB to death.
    

    Combine the two queries:

    SELECT 
      pl.name, pl.id, pl.state, 
      l.place_id, l.product_id,
      pr.name   
    FROM table_places pl  
    INNER JOIN link_table l ON (pl.id = l.place_id)  
    INNER JOIN table_products pr ON (l.product_id = pr.id)
    WHERE pr.name = '$name'
      AND pl.state = '$state'
    ORDER BY pr.name, pl.state
    

    Make sure you put indexes on all fields used in the ON clauses and the where clauses.

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

Sidebar

Related Questions

First of all I have to admit that my programming skills are pretty limited
So first of all I have such image (and ofcourse I have all points
To execute my application in iPhone first of all I have to add provisioning
First of all, I have no idea if the title even reflects my question.
First of all, I have to say that I'm going to talk about System.ComponentModel.Component
first of all I'm totally new to FAST but I already have a couple
First of all: I'm at Scala 2.8 I have a slight issue while using
First of all: I am not an experienced ClearCase user, but I have lots
First of all many thanks for clicking into my question. I have a few
First of all, I know how to build a Java application. But I have

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.