Performance implications of using OPENQUERY in a view Please see this question on stackoverflow :


  I am using an EasySoft ODBC driver to link a SQL Server 2008 R2
  Express instance to Interbase and I am having some difficulty with
  getting metadata from the remote server. From looking about on the net
  the main sugestions all mention using OPENQUERY instead of the four
  part linked server syntax. 
  
  E.G. My current (problematic) approach is...

CREATE VIEW [LIVE].[vwPRDETS]
AS

SELECT *
FROM [LBLIVE]...[PRDETS] WITH (NOLOCK)

  
  But on some tables I get the error when calling the view...
  
  
    Msg 7353, Level 16, State 1, Line 1 The OLE DB provider "MSDASQL" for
    linked server "LBLIVE" supplied inconsistent metadata. An extra column
    was supplied during execution that was not found at compile time.
  
  
  Also, some views I cannot even create because I get the following...
  
  
    Msg 7315, Level 16, State 1, Line 1 The OLE DB provider "MSDASQL" for
    linked server "LBLIVE" contains multiple tables that match the name
    ""SYSDBA"."AUDIT_LBABKP"".
  
  
  Although there is only one of the tables mentioned.
  
  The alternative approach from searching the net seems to be more
  like...

SELECT *
FROM OPENQUERY(&lt;linked sevrer&gt;, 'SELECT &lt;column list&gt; FROM MyTable')

  
  So, my question is, if I use OPENQUERY in my view definition will SQL
  Server be able to optimise the resulting SQL being sent to Interbase?
  Or is there really not much difference between the two approaches?


It's a cross over subject and would love a dba's POV.
Summary

Let the linked server do as much as possible.
It is impossible for SQL Server to optimise a query on a linked server, even another SQL Server

Long

The key factor is where the query runs.

In this case, it is a trivial SELECT so all rows from a table will be sent down the wire. It doesn't matter.

When you add JOINs and WHEREs then it can matter. You want SQL Server to allow the linked server to do as much filtering as possible to reduce the size of the data coming over the network.

For example, the 2nd case here should be more efficient.

SELECT *
FROM OPENQUERY(&lt;linked server&gt;, 
            'SELECT &lt;column list&gt; FROM MyTable') T1
     JOIN
     SomeLocalTable T2 ON ...
WHERE T1.foo = 'bar'

SELECT *
FROM OPENQUERY(&lt;linked server&gt;, 
           'SELECT &lt;column list&gt; FROM MyTable WHERE foo = ''bar''')
     JOIN
     SomeLocalTable T2 ON ...


A limitation of OPENQUERY is that you can't parametrise: so you need dynamic SQL to add WHERE clauses etc.

Linked servers performance can be affected by sp_serveroption. The setting collation compatible says it all


  If this option is set to true, SQL Server assumes that all characters in the linked server are compatible with the local server, with regard to character set and collation sequence (or sort order). This enables SQL Server to send comparisons on character columns to the provider. If this option is not set, SQL Server always evaluates comparisons on character columns locally.


That is, try not to let SQL Server process the data locally.

Note: On my foo = 'bar' 2nd example above, the filter is sent to the linked server because it just a string constant to SQL Server. The real WHERE clause in the first example may or not be sent remotely.

Finally, I've also found that staging the data into a temp table and joining that onto local tables is often better then joining directly onto the OPENQUERY.