Does Jet do joins efficiently?
I have a Jet database packed with 10 million records (account numbers) ,and I'm very impressed how fast a user can look up an account number.
On the other hand I was surprised by the following scenario. I have two tables (let's call them Customers and Orders to make this discussion easier). In this case, Orders has the ten million records.
Basically I wanted to do a regular join because Customers and Orders have a field in common (CustomerID). In other words:
SELECT Customers.Type, Orders.* FROM Customers
INNER JOIN Orders on Orders.CustomerID = Customers.CustomerID
Due to our IT dept's group policies (long story) I have to keep my queries short and quick - so I only pull one record at a time. I do this by adding a WHERE clause (to the above query) as to only pull one record at a time, like ths:
WHERE OrderID = @OrderID.
So I use a loop to pull multiple records, one by one, one record at a time. For example yesterday I had to look up a thousand records. The loop was taking several minutes. So I said to myself, "Hey I'll try something silly. Instead of doing a real join, I'll first pull the record from the Orders table (using a recordset) and then I'll pull the corresponding record from the Customers table (using another recordset). Once again I'll pull just one record at a time." And I told myself, "This new version of the loop should take twice as long, because there is no way it can be as fast as letting Jet do the join. After all, database engines are designed for efficient joins."
Well, I was wrong. The dual-recordset solution turned out to be three times faster than the join operation! (Actually I was using the VB.Net equivalent of a recordset, that is, a dataTable w/ dataAdapter).
I was so much in shock and disbelief that I erased my VB code, redid the old code, and ran it again - again the loop took several minutes. Then once again I put in the new code, and the loop completed in about one minute.
All the columns in both tables are indexed (using single-column indexes).
So I can find no explanation for this anomaly other than to ask myself, "Does Jet do joins inefficiently?"
I have a Jet database packed with 10 million records (account numbers) ,and I'm very impressed how fast a user can look up an account number.
On the other hand I was surprised by the following scenario. I have two tables (let's call them Customers and Orders to make this discussion easier). In this case, Orders has the ten million records.
Basically I wanted to do a regular join because Customers and Orders have a field in common (CustomerID). In other words:
SELECT Customers.Type, Orders.* FROM Customers
INNER JOIN Orders on Orders.CustomerID = Customers.CustomerID
Due to our IT dept's group policies (long story) I have to keep my queries short and quick - so I only pull one record at a time. I do this by adding a WHERE clause (to the above query) as to only pull one record at a time, like ths:
WHERE OrderID = @OrderID.
So I use a loop to pull multiple records, one by one, one record at a time. For example yesterday I had to look up a thousand records. The loop was taking several minutes. So I said to myself, "Hey I'll try something silly. Instead of doing a real join, I'll first pull the record from the Orders table (using a recordset) and then I'll pull the corresponding record from the Customers table (using another recordset). Once again I'll pull just one record at a time." And I told myself, "This new version of the loop should take twice as long, because there is no way it can be as fast as letting Jet do the join. After all, database engines are designed for efficient joins."
Well, I was wrong. The dual-recordset solution turned out to be three times faster than the join operation! (Actually I was using the VB.Net equivalent of a recordset, that is, a dataTable w/ dataAdapter).
I was so much in shock and disbelief that I erased my VB code, redid the old code, and ran it again - again the loop took several minutes. Then once again I put in the new code, and the loop completed in about one minute.
All the columns in both tables are indexed (using single-column indexes).
So I can find no explanation for this anomaly other than to ask myself, "Does Jet do joins inefficiently?"