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.

3
  • 2
    Do you have indexes on your relevant columns? That alone could help speed up the queries significantly. Commented Nov 3, 2011 at 14:41
  • 1
    Unless I'm missing something can you not just write one query that joins all three queries together? Commented Nov 3, 2011 at 14:44
  • @liquorvicar : "I need to try and understand how to get these two queries combined", the OP says. So you're not missing anything, and I think you could answer the question. Commented Nov 3, 2011 at 14:48

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I think this is what I was looking for. Going to try this. Be right back!
0

You can join more than two tables in one query. Untested query would be something like;

SELECT * FROM table_products AS prod LEFT JOIN (link_table AS link, table_places AS places)
    ON (prod.id=link.id AND prod.id=places.id)
WHERE some_field='some_value'

This will definitely get you some performance boost, as one query is most of times a lot faster than number-of-records query's (as you now loop through the records and query the db once per record)

2 Comments

you are wrong about "always". there are some things like query plan and stuff.
maybe always is a bit too strong, but, assuming he has at least hundreds of records, in this case it'll be faster. There is a flipping point somewhere, depending on the weight of both types of query's, the number of query's etc.
-2

The answer is simple: no control over database structure - no way to optimize.
As the indexing being the cornerstone of query optimization and you obviously need access to table structure to add one.

8 Comments

Ok, I am unfamiliar with indexing. Could this possibly be the solution?
You can still optimize the query statement itself it is pulling too much data or not using the most optimal way of getting data. In this case I don't see anything wrong with the query to correct though.
If you have no control over db structure then you can't index anything.
indexing is not "possibly be the solution", but, as I said, a cornerstone, an alpha and omega, the only way to optimize queries. a reference: dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html
@Col.Shrapnel "the only way"? Not exactly. First of all the query should be as 'light' as possible... But you should ALWAYS, if possible, index your tables. But in this case; indexing the table doesn't alter the structure of the database, so other code on the same database won't be affected.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.