Simon
Simon

Reputation: 19

Most efficient MySQL query?

I have just finished this MySQL query and as I still consider myself a novice, was wondering if I have done this in the most efficient way?

I have 4 tables, the first 2 contain category data and category associations which need to be linked up to one another. The 3rd table contains product info and the 4th table contains prices; some products have more than one price and I only need the lowest price.

          **Category Data**
        cat_id        cat_name
    |:-----------|------------:|
    |  21        |  Cat One    |
    |  22        |  Cat Two    |


            **Category Associations**
         ca_id        cat_id        prod_id
    |:-----------|------------:|:------------:|
    |   1        |    21       |     83       |
    |   2        |    22       |     88       |


                **Product Info**
        prod_id      prod_name    brand_name
    |:-----------|------------:|:------------:|
    |   83       |  Prod One   |  Brand One   |
    |   88       |  Prod Two   |  Brand Two   |


             **Prices**
       prod_id      price_id    
    |:-----------|------------:|
    |   83       |  22.15      |
    |   83       |  25.66      |
    |   88       |  47.23      |
    |   88       |  12.22      |

and this is the data that I need as the output

    prod_id     prod_name     brand_name      cat_name     lowest_price
|:-----------|------------:|:------------:|:------------:|:------------:|
|   83       |  Prod One   |  Brand One   |  Brand Two   |  22.15       |
|   88       |  Prod Two   |  Brand Two   |  Brand Two   |  12.22       |

I have left out alot of the data and just given the basics for ease.

This query works; just wondering if it can be improved upon. The data will be stored in an array and will be sorted using javascript.

SELECT `".DB_PREFIX."products`.prod_id, `".DB_PREFIX."products`.prod_name, `" . DB_PREFIX . "products`.prod_brand, `" . DB_PREFIX . "categories`.cat_name, `" . DB_PREFIX . "products`.prod_image, `" . DB_PREFIX . "products`.prod_field_one, `" . DB_PREFIX . "products`.prod_field_two, `" . DB_PREFIX . "products`.prod_field_three, `" . DB_PREFIX . "products`.prod_field_four, `" . DB_PREFIX . "products`.prod_field_five, `" . DB_PREFIX . "products`.prod_field_six, MIN(`" . DB_PREFIX . "prices`.price) as LowestPrice
FROM (`".DB_PREFIX."products` 
INNER JOIN `".DB_PREFIX."category_associations` ON `".DB_PREFIX."products`.prod_id = `".DB_PREFIX."category_associations`.prod_id)
INNER JOIN `".DB_PREFIX."categories` ON `".DB_PREFIX."category_associations`.cat_id = `".DB_PREFIX."categories`.cat_id     
INNER JOIN `" . DB_PREFIX . "prices` 
ON `" . DB_PREFIX . "prices`.prod_id = `" . DB_PREFIX . "products`.prod_id 
GROUP BY `" . DB_PREFIX . "prices`.prod_id`

Thanks, Simon

Upvotes: 0

Views: 1353

Answers (2)

user399666
user399666

Reputation: 19879

Debug your query using EXPLAIN. Using EXPLAIN, you should be able to figure out if your query is utilizing the correct indexes or not.

Upvotes: 4

Luke
Luke

Reputation: 23680

You could create a MySQL view of the data that you require and simply query that view as a table, this would improve things from a coding/maintenance point of view...

Upvotes: 0

Related Questions