title: Slow response when querying a View - Using Linq to SQL
body: <p>I have the following view:</p>
  
  <pre><code>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_)
  </code></pre>
  
  <p>This view is created in a database called NavisionMeta
  It queries a database (on the same server) claled Navision4</p>
  
  <p>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</p>
  
  <p>If I query it using this query in SQL Management Studio it takes over 2 minutes:</p>
  
  <pre><code>SELECT * 
  FROM [NavisionMeta].[dbo].[PurchaseOrders]
  WHERE Processed=0 AND Copied=0
  </code></pre>
  
  <p>Which is wayyy too long!</p>
  
  <p>The following query in LINQ times out all-together, even if I adjust the timeout to 5 mins!</p>
  
  <pre><code>            var purchaseOrdersNotProcessed = (from p in db.PurchaseOrders
                                                where p.Copied.Equals(0)
                                                &amp;&amp; p.Processed.Equals(0)
                                                select p).ToList();
  </code></pre>
  
  <p>What puzzles me, is, on the previous hardware, it worked fine!</p>
  
  <p>Just in case it is relevant, the udf used above is:</p>
  
  <pre><code>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
  )
  </code></pre>