-2

I have to index a view to query much faster, but this view has underlying view with a linked server that I cannot schema bind. Which I can't because it is in a linked server and don't have direct access to the linked server. Does replication will work? That underlying linked view have 1000 - 1700 rows that changes.

Haven't tried replication cause I don't want to mess up the database. How can copy/replicate this view so I can schema bind?

7
  • A linked view with 1000 rows shouldn't be slow in the first place... you might want to investigate the cause of the performance issue and directly address that. Commented Sep 25, 2024 at 1:54
  • sorry, the linked server is not slow. there are 3 tables that i'm joining, table 1 with 60,000+ rows and table 2 view with linked server and table 3 with one to many relation with table 1. and this view union with another table. Commented Sep 25, 2024 at 2:54
  • So investigate what is causing the performance issues, you may just be missing a regular index on one of your actual tables... Don't assume you need an indexed view. Commented Sep 25, 2024 at 3:00
  • 60,000 rows isn't many. Question, do you union or union all? union all performs a lot better... but doesn't make the results distinct. Commented Sep 25, 2024 at 3:04
  • 1
    From an old comment of mine: Indexed views aren’t magical spells that make all queries against views faster. They are intended for a very specific use case and the speed improvement relies on the indexed view’s ability to drastically reduce the number of reads required to produce the end result (typically through massive aggregation). Most of the time the way to fix a view is NOT to put an index on it, but rather to focus on the indexes on the table(s) involved in the view. Commented Sep 25, 2024 at 11:18

1 Answer 1

1

the linked server is not slow.

Great! You've already eliminated one potential bottleneck.

Instead of going to great lengths to materialize the data - which you can't do via an indexed view, since every object referenced by the view must exist within the same database as the view - and an indexed view is unlikely to be a magical fix anyway - you need to focus on finding the actual bottleneck.

Look at the execution plans. Is the query running remote or bringing all the data local and then filtering? If the query joins linked server tables to local objects, compare the difference using REMOTE JOIN. You could also consider experimenting with materializing and indexing the data locally at runtime before joining, e.g.:

SELECT [cols] INTO #foo FROM [linked_server]...;
CREATE INDEX whatever on #foo(...);
-- now join to #foo

If the query doesn't join to local tables (and doesn't rely on local resolution of functions like DB_NAME(), OBJECT_NAME(), etc.), consider using OPENQUERY(), OPENROWSET(), EXECUTE AT, or sp_executesql against the linked server, e.g.:

EXEC [linked_server].database_name.sys.sp_executesql N'SELECT 
     ...
     FROM {table(s) without server references}
     ...';

This will force optimization and filtering to happen at the destination. This will also allow you (or someone with access) to take that exact query and tune it on the linked server directly to get better insight into index opportunities to make it perform better no matter where it runs. You might not have control over the linked server, but surely you can request better indexes to support your workloads. This arms those users with a lot more information than just saying "my linked server query is not performing well."

You could also combine them, e.g.

INSERT #foo(cols)
  EXEC [linked_server].database.sys.sp_executesql N'SELECT ...';

-- or

INSERT #foo(cols) SELECT cols FROM OPENROWSET(...);

Some other answers that might potentially help (and a search for many more):

And also check out this broader topic on Microsoft Learn:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.