I have the following view:
SELECT
poHeader.No_ AS PONumber,
poHeader.[Buy-from Vendor No_] AS VendorNumber,
poHeader.[Document Date] AS DocumentDate,
vendor.Name AS VendorName,
vendor.Contact AS VendorContact,
vendor.[E-Mail] AS VendorEmail,
vendor.Address AS VendorAddress,
vendor.[Address 2] AS VendorAddress2,
vendor.City AS VendorCity,
vendor.County AS VendorCounty,
vendor.[Post Code] AS VendorPostCode,
vendor.[Phone No_] AS VendorPhone,
vendor.[Fax No_] AS VendorFax,
salesHeader.No_ AS SONumber,
poHeader.[Order Date] AS OrderDate,
salesHeader.[Crocus Comment] AS CrocusComment,
salesHeader.GiftMessage,
salesHeader.[Delivery Comment] AS DeliveryComment,
salesHeader.[Shipment Date] AS DeliveryDate,
COALESCE (salesHeader.[Ship-to Name],
poHeader.[Ship-to Name]) AS DeliveryName,
COALESCE (salesHeader.[Ship-to Address],
poHeader.[Ship-to Address]) AS DeliveryAddress,
COALESCE (salesHeader.[Ship-to Address 2],
poHeader.[Ship-to Address 2]) AS DeliveryAddress2,
COALESCE (salesHeader.[Ship-to City],
poHeader.[Ship-to City]) AS DeliveryCity, COALESCE (salesHeader.[Ship-to County],
poHeader.[Ship-to County]) AS DeliveryCounty,
COALESCE (salesHeader.[Ship-to Post Code],
poHeader.[Ship-to Post Code]) AS DeliveryPostcode,
salesHeader.DeliveryPhoneNo, poForEmailing.Processed,
poForEmailing.Copied
FROM
Navision4.dbo.[Crocus Live$Purch_ orders for e-mailing] AS poForEmailing
LEFT OUTER JOIN
Navision4.dbo.[Crocus Live$Purchase Header] AS poHeader ON poForEmailing.No_ = poHeader.No_
INNER JOIN
Navision4.dbo.[Crocus Live$Vendor] AS vendor ON poHeader.[Buy-from Vendor No_] = vendor.No_
LEFT OUTER JOIN
Navision4.dbo.[Crocus Live$Sales Header] AS salesHeader ON salesHeader.No_ = dbo.fnGetSalesOrderNumber(poHeader.No_)
This view is created in a database called NavisionMeta It queries a database (on the same server) claled Navision4
I have recently moved both databases on to new (better) hardware. Not sure if this is relevant, but the new hardware has SQL 2008, and the old hardware was running SQL 2000
If I query it using this query in SQL Management Studio it takes over 2 minutes:
SELECT *
FROM [NavisionMeta].[dbo].[PurchaseOrders]
WHERE Processed=0 AND Copied=0
Which is wayyy too long!
The following query in LINQ times out all-together, even if I adjust the timeout to 5 mins!
var purchaseOrdersNotProcessed = (from p in db.PurchaseOrders
where p.Copied.Equals(0)
&& p.Processed.Equals(0)
select p).ToList();
What puzzles me, is, on the previous hardware, it worked fine!
Just in case it is relevant, the udf used above is:
CREATE FUNCTION [dbo].[fnGetSalesOrderNumber](@PONumber varchar(20))
RETURNS varchar(20)
AS
BEGIN
RETURN (
SELECT
TOP 1 [Sales Order No_]
FROM
Navision4.dbo.[Crocus Live$Purchase Line]
WHERE
[Document No_] = @PONumber
)