Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query on Linked Server


Query on Linked Server

Author
Message
mallikachowdhary 98955
mallikachowdhary 98955
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 34
Hi,

I have a query if we can populate the same table A in a database D with data coming from 2 different Linked servers connected to 2 different data source (via ODBC) connection.

I'm new to SQL server and trying to figure the role of Linked server in SQL server 2008 scenario. I have a production SQL server 2008 in place with reports getting created through various procedures created on it. Currently it has a database with tables and procedures and a Linked server created over 1 data source which connects to a sybase database through ODBC.

In new scenario we'll have data devided over 2 data sources and thus we'll create 2 ODBC connections to fetch the data. It is required to get data loaded in the same table A of database D from the 2 data sources.

So is creating a new Linked server with new data connection the correct solution to this.

Thanks in advance.
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14933 Visits: 38926
certainly possible.
remember linked servers are slow, especially if you are joining the data together with local or other rmeote data.

the reason is if you did something like this:

SELECT * From myTable
INNER JOIN MyLinkedServer.SandBox.dbo.AnotherTable myAlias
ON MyTable.ID = MyAlias.ID
WHERE myAlias.CustomerID = 12



looks pretty simple, right? with a WHERE statement that specific, i'd expect very few rows, since how many rows have that specific customerID?

the problem is behind the scenes, the entire contents of the linked server table MyLinkedServer.SandBox.dbo.AnotherTable gets copied over into tempDB, and then joined to the local table, and then that gets filtered like a normal query.

so if the remote tables are MillionBillion row tables, you see a massive performance hit.


to avoid things like that, you want to use a divide and conquer strategy to minimize the data being copied.
using OPENQUERY for example,


SELECT *
INTO #tmp
FROM OPENQUERY( [MyLinkedServer],'SET FMTONLY OFF; EXEC (''USE SandBox; SELECT * FROM dbo.AnotherTable WHERe CustmerID = 12'')')


SELECT * From myTable
INNER JOIN #tmp myAlias
ON MyTable.ID = MyAlias.ID
WHERE myAlias.CustomerID = 12



in the above example, the linked server pre-filtered all the rows on it's server side, before passing the results....that's a smaller temp table, so it's quicker.
the rest of the command barely changed.

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

ScottPletcher
ScottPletcher
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3937 Visits: 6665
Lowell (7/24/2014)
SELECT * From myTable
INNER JOIN MyLinkedServer.SandBox.dbo.AnotherTable myAlias
ON MyTable.ID = MyAlias.ID
WHERE myAlias.CustomerID = 12



looks pretty simple, right? with a WHERE statement that specific, i'd expect very few rows, since how many rows have that specific customerID?

the problem is behind the scenes, the entire contents of the linked server table MyLinkedServer.SandBox.dbo.AnotherTable gets copied over into tempDB, and then joined to the local table, and then that gets filtered like a normal query.


I don't believe that's true. SQL does not always have to copy the rows over to the local server, and of course it won't if it can avoid it.

SQL DBA,SQL Server MVP('07, '08, '09)

Prosecutor James Blackburn, in closing argument in the "Fatal Vision" murders trial: "If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them."
mallikachowdhary 98955
mallikachowdhary 98955
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 34
Thanks Lowell and Scott for the reply.

Please help me with following queries:
1. Where and how can we access and check the temp table .
2. Why is Linked servers required.
3. And can we load a same table with data coming from 2 different data sources connected through ODBC.

If the scenario is non descriptive than do let me know what additional information is required.

Thanks in advance
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search