Skip to content
Advertisement

Why is my SQL query executing indefinitely?

It seems that this code is correct and I am not sure why it is not executing. I thought it might be extremely inefficient but after 15 minutes it is still executing.

INSERT INTO dbo.WorkingOrderProducts (OrderID, OrderDate, OrderUnitPrice, ProductName)
    SELECT
        Orders.OrderID, OrderDate, [Order Details].UnitPrice * Quantity AS OrderUnitPrice, ProductName
    FROM 
        dbo.Orders, dbo.[Order Details], dbo.Products
    WHERE
        OrderDate > '1996-12-31'

edit: Thank you all for the input.

Advertisement

Answer

You are using implicit CROSS JOIN in your query and this can kill your performance if you don’t data merged like this.

Let’s say that the Orders table has 100 records, then Order Details have 100, and Products have 100.

The number of produced rows in the result will be 1 million because it is cartesian product.

You might think of rewriting your query and adding more clauses in your where condition.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement