Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimizing MySQL Query
    primarykey
    data
    text
    <p>We have a query that is currently killing our database and I know there has to be a way to optimize it. We have 3 tables:</p> <ol> <li>items - table of items where each items has an associated object_id, length, difficulty_rating, rating, avg_rating & status <li>lists - table of lists which are basically lists of items created by our users <li>list_items - table with 2 columns: list_id, item_id </ol> <p>We've been using the following query to display a simple HTML table that shows each list and a number of attributes related to the list including averages of attributes of the included list items:</p> <pre><code>select object_id, user_id, slug, title, description, items, city, state, country, created, updated, (select AVG(rating) from items where object_id IN (select object_id from list_items where list_id=lists.object_id) AND status="A" ) as 'avg_rating', (select AVG(avg_rating) from items where object_id IN (select object_id from list_items where list_id=lists.object_id) AND status="A" ) as 'avg_avg_rating', (select AVG(length) from items where object_id IN (select object_id from list_items where list_id=lists.object_id) AND status="A" ) as 'avg_length', (select AVG(difficulty_rating) from items where object_id IN (select object_id from list_items where list_id=lists.object_id) AND status="A" ) as 'avg_difficulty' from lists where user_id=$user_id AND status="A" order by $orderby LIMIT $start,$step </code></pre> <p>The reason why we haven't broken this up in 1 query to get all the lists and subsequent lookups to pull the averages for each list is because we want the user to be able to sort on the averages columns (i.e. 'order by avg_difficulty').</p> <p>Hopefully my explanation makes sense. There has to be a much more efficient way to do this and I'm hoping that a MySQL guru out there can point me in the right direction. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload