Hey all,
I've only been developing on SQL server for a couple of years, but in those years I have learnt that HASH joins are BAD for performance. This is because they seem to always select EVERY record from table one and then pass it to the HASH joiner which does the filtering/matching.
I've come across a situation where I simply can't get rid of the has joins.
I'm basicly creating some quick lookup tables, which are kind of like indexed views, they only contain 1 PK and FKs for about 5 tables.
The application is a Job Management System for a manufacturing company, and OrderItems has another table called WorkflowProcesses, and for each manufacturing process that an order item goes through, there is a correspending row in WorkflowProcesses. If the order is current one row in WorkflowProcesses will have it's "ActiveProcess" bit set.
With around 1,000,000 rows in WorkFlowProcesses, and around 200,000 orders, queries are getting a little slow (around 2-3 seconds).
So what I've done is created a CurrentWorkflowProcesses. I have SPs to insert and delete from CurrentWorkflowProcesses as needed. CurrentWorkFlowProcesses contains a PK, and FKs for OrderItemID, OrderID, ClientID AND JobID. This way queries on a particalar client benifit also.
The problem is that every table that is joined to CurrentWorkflowProcesses is being HASH JOINED.
This is resulting in query plans with around 2000 rows comming from CurrentWorkFlowProcesses, 200,000 rows comming from tblOrderItems and being HASH joined to the 2000 rows, then 50,000 rows comming from tblOrders and being HASH joined.
If I use a INNER LOOP JOIN hint, the performance is a lot better, so how do I get SQL to choose a LOOP join everytime for these queries? (without using a hint) What should be indexed?
I've only been developing on SQL server for a couple of years, but in those years I have learnt that HASH joins are BAD for performance. This is because they seem to always select EVERY record from table one and then pass it to the HASH joiner which does the filtering/matching.
I've come across a situation where I simply can't get rid of the has joins.
I'm basicly creating some quick lookup tables, which are kind of like indexed views, they only contain 1 PK and FKs for about 5 tables.
The application is a Job Management System for a manufacturing company, and OrderItems has another table called WorkflowProcesses, and for each manufacturing process that an order item goes through, there is a correspending row in WorkflowProcesses. If the order is current one row in WorkflowProcesses will have it's "ActiveProcess" bit set.
With around 1,000,000 rows in WorkFlowProcesses, and around 200,000 orders, queries are getting a little slow (around 2-3 seconds).
So what I've done is created a CurrentWorkflowProcesses. I have SPs to insert and delete from CurrentWorkflowProcesses as needed. CurrentWorkFlowProcesses contains a PK, and FKs for OrderItemID, OrderID, ClientID AND JobID. This way queries on a particalar client benifit also.
The problem is that every table that is joined to CurrentWorkflowProcesses is being HASH JOINED.
This is resulting in query plans with around 2000 rows comming from CurrentWorkFlowProcesses, 200,000 rows comming from tblOrderItems and being HASH joined to the 2000 rows, then 50,000 rows comming from tblOrders and being HASH joined.
If I use a INNER LOOP JOIN hint, the performance is a lot better, so how do I get SQL to choose a LOOP join everytime for these queries? (without using a hint) What should be indexed?