How to Improve Query Performance with many JOINs

dloewen

I have a query (with the purpose of making a view) which is using a few joins to get each column. Performance degrades quickly (exponentially?) for each set of joins added.

What would be a good approach to make this query faster? Please see comments within the query.

If it helps, this is using the WordPress DB schema.

Here is a screenshot of EXPLAIN enter image description here

PRODUCTS TABLE

+--+----+
|id|name|
+--+----+
|1 |test|
+--+----+

METADATA TABLE

+----------+--------+-----+
|product_id|meta_key|value|
+----------+--------+-----+
|1         |price   |9.99 |
+----------+--------+-----+
|1         |sku     |ABC  |
+----------+--------+-----+

TERM_RELATIONSHIPS TABLE

+---------+----------------+
|object_id|term_taxonomy_id|
+---------+----------------+
|1        |1               |
+---------+----------------+
|1        |2               |
+---------+----------------+

TERM_TAXONOMY TABLE

+----------------+-------+--------+
|term_taxonomy_id|term_id|taxonomy|
+----------------+-------+--------+
|1               |1      |size    |
+----------------+-------+--------+
|2               |2      |stock   |
+----------------+-------+--------+

TERMS TABLE

+-------+-----+
|term_id|name |
+-------+-----+
|1      |500mg|
+-------+-----+
|2      |10   |
+-------+-----+

QUERY

SELECT 
  products.id,
  products.name,
  price.value AS price,
  sku.value AS sku,
  size.name AS size
FROM products

/* These joins are performing quickly */

INNER JOIN `metadata` AS price ON products.id = price.product_id AND price.meta_key = 'price'
INNER JOIN `metadata` AS sku ON products.id = sku.product_id AND sku.meta_key = 'sku'

/* Here's the part that is really slowing it down - I run this chunk about 5 times with different strings to match */

INNER JOIN `term_relationships` AS tr ON products.id = tr.object_id
  INNER JOIN `term_taxonomy` AS tt
  ON tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'size'
    INNER JOIN `terms` AS size
    ON tt.term_id = size.term_id
carleson

Your performance issue is most likely caused by the join with the 'term_taxonomy' table.
All other joins seems to use the primary key (where you probobly have working indexes on).

So my suggestion is to add a compound index on term_taxonomy_id and term_id (or if you must: taxonomy). Like this:

CREATE UNIQUE INDEX idx_term_taxonomy_id_taxonomy
ON term_taxonomy( term_taxonomy_id, taxonomy);

Hope this will help you.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to improve query performance for multiple inner joins?

How to force the order of the joins to improve query performance in MYSQL?

how to improve performance in pyspark joins

Is it possible to improve the performance of query with distinct and multiple joins?

How to improve performance of this query

How to improve order by performance with joins in mysql

Mysql Bad Query performance with many joins

Improve performance for query that includes many conditions with "or" operator

How to improve a select query performance?

How to improve performance of query execution

How to improve mongoDb query performance?

How to improve PostgreSQL query performance

How to improve performance of this sql query?

How to improve performance of mysql query?

How to Improve Performance of sql query

How to improve my query performance SQL Server

How to improve performance of a simple select query in oracle

How to improve query performance that compute haversine formula?

How to improve MongoDB find query performance?

How to improve the performance of a SQL Server Select query?

How can I improve the performance of this slow query

How to improve performance of GeoIP query in BigQuery?

Hive how to improve my query performance?

How to Improve SQL Azure Query Performance

How to improve performance on query that uses OUTER APPLY

How to improve performance of order by SQL query

How to improve query performance on a single node in MonetDB?

How to improve query performance for expensive table spool

How can I improve the performance of this query