How can I improve this query where multiple joins on the same table appears to be affecting performance

Active3 hr before
Viewed126 times

9 Answers


Filesort of 10 records can't influence on performance.,Approach 1 will be better in your scenario, as it will give the result by one query run and seems to be properly joined as required by the business, though same table is joined multiple times.An index on id column will perform better.,Please be sure to answer the question. Provide details and share your research!,Approach 2: Or is it better to fetch without left joining, then on application level, I do another query to fetch the creator/updator/deletor, like so:

To ensure max performance (assuming id is primary/unique) I'll recommend to move LIMITation into subquery:

u.*, as 'creator', as 'updator', as 'deletor'
LEFT JOIN users a on u.created_by =
LEFT JOIN users b on u.updated_by =
LEFT JOIN users c on u.deleted_by =
load more v

A join refers to the same table multiple times,You replace joins with IN() lists on large tables,Figure 4-3. One way to join multiple tables,Figure 4-4. How MySQL joins multiple tables

mysql > SELECT * FROM -
   > INNER JOIN sakila.film_actor USING(actor_id) -
   > INNER JOIN USING(film_id) -
   > WHERE = 'Academy Dinosaur';
mysql > SELECT*FROM;
load more v

Stack Overflow Public questions & answers ,Give it a try. Please share the expected performance, number of records you have & also what performance you get with my asnwer.,"joins" is the killer for performance, the bigger your data is, the more pain you will feel; , Meta Stack Overflow

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);
load more v

The theory behind query run time,Table size: If your query hits one or more tables with millions of rows or more, it could affect performance.,Database software and optimization: This is something you probably can't control, but if you know the system you're using, you can work within its bounds to make your queries more efficient.,Query your connected data sources with SQL

Filtering the data to include only the observations you need can dramatically improve query speed. How you do this will depend entirely on the problem you're trying to solve. For example, if you've got time series data, limiting to a small time window can make your queries run much more quickly:

   FROM benn.sample_event_table
WHERE event_date >= '2014-03-01'
AND event_date < '2014-04-01'
load more v

The optimizer chooses one of the tables as the outer table, or the driving table. The other table is called the inner table. , To perform a nested loops join, Oracle follows these steps: ,If the view has only one base table, the optimizer can use view merging. ,If there is a tie, the optimizer chooses the execution plan with the fewest sort-merge operations.

Figure 24-1 shows the execution plan for this statement using a nested loops join:

   FROM emp, dept
WHERE emp.deptno = dept.deptno;
load more v

SQL Server Career Plan, Follow Get Free SQL Tips Twitter LinkedIn Facebook Pinterest RSS ,SQL Server Join Hints ,SQL Server Join Example


   --cleanup statements
   --DROP INDEX Child.idxChild_ParentID
load more v

For example, if you had tables BIG, MEDIUM, SMALL, and TINY, the logical join order to try would be BIG, TINY, SMALL, MEDIUM. , Specify the largest table first. This table is read from disk by each Impala node and so its size is not significant in terms of memory usage during the query. , Join the next smallest table, then the next smallest, and so on. , With statistics in place, Impala can choose a more effective join order rather than following the left-to-right sequence of tables in the query, and can choose BROADCAST or PARTITIONED join strategies based on the overall sizes and number of rows in the table:

In this example, the subselect from the BIG table produces a very small result set, but the table might still be treated as if it were the biggest and placed first in the join order. Using STRAIGHT_JOIN for the last join clause prevents the final table from being reordered, keeping it as the rightmost table in the join order.

select straight_join x from medium join small join(select * from big where c1 < 10) as big
where = and =;

--If the query contains[DISTINCT | ALL], the hint goes after those keywords.
select distinct straight_join x from medium join small join(select * from big where c1 < 10) as big
where = and =;
load more v

...I've been wondering if it really matters from a performance standpoint where I start my queries. For example, if I join from A-B-C, would I be better off starting at table B and then going to A & C? , Let's use the following query from WideWorldImporters for our examples: , Performance Tuning ▾ ▾ Execution Plans Indexing Query Rewrites , Basically, we write a subquery around the tables we want to join together first and make sure to include a TOP clause.

-- Run if if you want to follow along - add  a computed column and index for CountryOfManufacture
ALTER TABLE Warehouse.StockItems
ADD CountryOfManufacture AS CAST(JSON_VALUE(CustomFields,'$.CountryOfManufacture') AS NVARCHAR(10)) 
CREATE INDEX IX_CountryOfManufacture ON Warehouse.StockItems (CountryOfManufacture)

Sales.Orders o--73595 rows
INNER JOIN Sales.OrderLines l--231412 rows
ON o.OrderID = l.OrderID--231412 rows after join
INNER JOIN Warehouse.StockItems s--227 rows
ON l.StockItemID = s.StockItemID--1036 rows after join
AND s.CountryOfManufacture = 'USA'--8 rows
for USA
load more v

A join condition defines the way two tables are related in a query by:,A columnstore index scan used to provide rows for the Hash join build phase.,If an Adaptive Join switches to a Nested Loops operation, it uses the rows already read by the Hash Join build. The operator does not re-read the outer reference rows again.,The query returns one row. Enabling Live Query Statistics displays the following plan:

Specifying the join conditions in the FROM clause helps separate them from any other search conditions that may be specified in a WHERE clause, and is the recommended method for specifying joins. A simplified ISO FROM clause join syntax is:

FROM first_table < join_type > second_table[ON(join_condition)]
load more v

Other "table-performance" queries related to "How can I improve this query where multiple joins on the same table appears to be affecting performance"