问题
I have a view that retrieves data from/through a linked server, thus when calling the linked table through the view I will use select * from linkerserver.linkedtable
. My query works and I get a table of info back.
I am however concerned about speed and contention when using a linked server especially if there are multiple users accessing/calling a sp (Stored Procedure) that makes use of the Linked Server View.
I am unsure as to how I could solve this problem or even if it is a problem? (Will one face speed or contention issues when using a linked server view?).
The options to get around this could be:
- Create a persistent temporary table (##MYTEMP) that is a duplicate of the data/table from the view, but exists locally.
- All calls are then directed to this temp table existing locally.
- or create a duplicate table locally and have a trigger to populate this table after the first call of the day.
Any advise about how to solve this problem or best use case would be greatly appreciated.
回答1:
The best bet would be to ask the other team to push you the data after they update it. If they are only adding new records this is all you need added to your table. If they are also updating data then they could use Merge so that only new records and changes are processed.
My second choice would be to have a table on your server that gets dropped and re-pulled (or use Merge) from the linked server once they do their data load. You would need to coordinate with their team on this.
My last choice would be to have this data pulled nightly by SQL Agent. Unless the source data was also updated nightly, then this would be my second choice. If they processed between midnight and 2 am I would have my job run at 3 or 4am.
回答2:
It is very easy to write the result of a SELECT
into a table. All columns will be created automatically:
SELECT * INTO NewTable FROM linkerserver.linkedtable
The NewTable
can be a normal table as well as a #
or a ##
.
After usage (or right before) you just drop this table...
回答3:
I've ran into this problem in the past. What I notice from my experience is that the sql server doesn't generate the best query plans when it comes to link server.
How big is the database that have this table ? Is it possible to do replicate read only ?
If yes, then it's better to do that. If not, then you will need just like you said, a local table and a trigger. Or an incremental update using sql agent every 5 minutes or so.
来源:https://stackoverflow.com/questions/38720770/what-is-the-best-way-to-cache-a-table-from-a-sql-linked-server-view