[Solved] Remote query, view and where clause performance

EverSQL Database Performance Knowledge Base

Remote query, view and where clause performance

Database type:

I'm a bit puzzled over a performance problem with our SQL server when using remote query's and applying a where clause. When I run the query on the local server a clustered index seek is used, but from remote this is not the case.

So when running this on the local server it will take 2 seconds:

SELECT * FROM uv_order WHERE order_id > '0000200000'

But running this from a remote database takes 2 minutes:

SELECT * FROM RemoteServer.data.dbo.uv_order WHERE order_id > '0000200000'

Here uv_order is a quite complex view but since an index seek is used when executing from the local server I don't see why it can't use it when running a remote query. This only seams to apply to view since doing the same thing on a table will work as expected.

Any ideas why this happens and how to "fix" it?

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Selecting Unnecessary Columns (query line: 2): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  2. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  3. Use Numeric Column Types For Numeric Values (query line: 6): Referencing a numeric value (e.g. 0000200000) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
Optimal indexes for this query:
CREATE INDEX uv_order_idx_order_id ON uv_order (order_id);
The optimized query:
        uv_order.order_id > '0000200000'

Related Articles

* original question posted on StackOverflow here.