I have 1 SQL server machine having 2 instances [Instance1] and [Instance2]
If I need to execute a cross query joining tables like below
[Instance1].[db1].[dbo].[table1]
joins
[Instance2].[db2].[dbo].[table2]
Will there be lot of performance hit in above query compared to if both databases are in same instance?
Please note that both instances are in same machine (server).
Thanks for help!
Related
I have 3 databases hosted across 3 different servers, all of which have different data and structures. I need to perform a query that will draw data from various tables across all three of them.
I've registered the three servers into a server group, and I've confirmed that all of the connections are working properly.
Here's an example of the problem I'm facing. For the first part of this query, I need to retrieve a list of records from the 'Applications' table in DB1 so I write:
SELECT * FROM [DB1].[dbo].[Application]
I know that this query works partially because it starts returning results from the correct table. The problem is that I haven't specified the server that DB1 is on, so once the query has finished querying DB1.dbo.Application, it looks for the same database and table on the next server. The database and table don't exist on the other servers so the query fails.
So how do I specify the server that I want the query to run on? I've tried [server_name].[DB1].[dbo].[Application], but it still runs the query across all of them.
Server groups are for maintenance purposes, what you need to use is a linked server
On one of the servers, say SERVER1, you will need to set up two linked servers - one to SERVER2 and one to SERVER3
From SERVER1 you will then be able to query the other servers using the four part name in a normal query window:
SELECT * FROM DatabaseName.dbo.Table1;
SELECT * FROM SERVER2.DatabaseName.dbo.Table2;
SELECT * FROM SERVER3.DatabaseName.dbo.Table3;
You can also use the tables from the remote server in JOINs etc as though they were on the local server and the remote servers don't even need to run SQL Server - they can be Oracle, MYSQL etc.
Be aware though, remote servers are slow and you may struggle with large datasets
I have a query that currently runs successfully on SQL Server Management Studio, gathering information from 2 databases on the SQL server where I run the query, and one database on a linked SQL Server. The query has this structure:
SELECT TOP 10
DB1.id_number as BLN,
DB1.field1,
DB2.field2,
DB3.field3
FROM
LinkedServer.database1.dbo.table1 DB1
INNER JOIN
database2.dbo.table2 DB2 ON DB1.id_number = DB2.id_number
INNER JOIN
database3.dbo.table3 DB3 ON DB3.id_number2 = DB1.id_number2
WHERE
DB1.id_number IS NOT NULL
AND DB1.field1 IS NOT NULL
How can I run this same query from a .Net application? I am looking for a solution that doesn't require saving a View on the database.
In whatever solution you propose, please detail connection strings and security issues.
Thank you.
You can run the query using SqlCommand. Although doing it with an ORM may be a little tricky, if it even can be done.
I have a question about how to perform database queries against different database systems?
For example, I have a following SQL query string:
SELECT A.F1, A.F2, B.F3, B.F4
FROM TableA A, TableB B
WHERE A.ID=B.ID AND B.ID=xyz
Is there any solution that I can perform the above query when:
TableA is from an Oracle database, TableB is also from an Oracle database from another instance
TableA is from a SQL Server database, TableB is from an Oracle database
TableA is from an Oracle database, TableB is from a SQL Server database
TableA is from a SQL Server database, TableB is also from a SQL Server database from another SQL Server instance
I know that for situation #1 I can use the ORACLE DATABASE LINK feature (also maybe #3). But is there any common solution which can address all of the scenarios above? For instance, maybe there is another scenario that I want to join two tables from MySQL and SQL Server databases.
For coding I am using C#/.NET, any recommendation is welcome, including joining the data in the code.
Thanks!
This is known as a federated query
You can use SQL Servers federation ability (linked servers) and run the query in SQL Server. You can use Oracles federation ability (Oracle heterogeneous services) and run the query in Oracle.
Either way you need to pick a database server, make the other (foreign) database known to that server then execute the query on the server.
Keep in mind you cannot expect good performance from this as in most cases the executing server is loading all the source records locally and joining locally.
Otherwise you need to write your own 'federation' ability in your app. This is non trivial, i.e. data types don't match exactly between vendors so you need to build some smarts. Also if you are joining particularly large tables you'd better make sure your algorithm is optimised or you'll run our of memory. Further to this, federated query ability in existing products (SQL, Oracle, Cognos etc.) is the result of very large companies doing a lot of development.
Can you tell us how many records you expect to join, and are the various source database servers mostly fixed or is this for an ad hoc query tool?
I have a simple Access database I use to create a number of reports. I've linked to a Teradata database server in our organization to pull some additional employee-level details. There is a simple left join on employee number, and all I pull is the name and the role.
The query without the connect takes maybe a minute or so to run and is very quick once loaded. Left joining on the Teradata connection slows everything down to a crawl. It can take 10 minutes or so to run the query through Excel. When the query is loaded in Access, scrolling through it is very slow.
I should note there's no performance issues with the Teradata server. I pull unrelated reports from the same and different tables, with complex joins and the speed is very quick.
I tried creating an even simpler query that does almost noting, and the performance issues are still there. Here is the code:
SELECT EMPL_DETAILS_CURR.NM_PREFX, EMPL_DETAILS_CURR.NM_GIVEN,
MC.DT_APP_ENTRY, MC.CHANNEL_IND
FROM MC LEFT JOIN EMPL_DETAILS_CURR ON MC.EMP_ID = EMPL_DETAILS_CURR.EMP_ID;
There are only 7000 records in MC.
If you are joining data between MS Access tables and Teradata tables the join has to be completed using the Microsoft JET Engine on your local machine. That means the data that exists in your Teradata tables is being brought down to your local machine to so that it can be joined.
If the tables are all on Teradata and accessed via linked tables in MS Access the join may still be occurring locally. I would suggest running the query as an ODBC Direct (I forget the exact term) query so that the SQL is passed on to Teradata to be executed and the results returned to MS Access when the query completes.
Here's the situation: we have an Oracle database we need to connect to to pull some data. Since getting access to said Oracle database is a real pain (mainly a bureaucratic obstacle more than anything else), we're just planning on linking it to our SQL Server and using the link to access data as we need it.
For one of our applications, we're planning on making a view to get the data we need. Now the data we need is joined from two tables. If we do this, which would be preferable?
This (in pseudo-SQL if such a thing exists):
OPENQUERY(Oracle, "SELECT [cols] FROM table1 INNER JOIN table2")
or this:
SELECT [cols] FROM OPENQUERY(Oracle, "SELECT [cols1] FROM table1")
INNER JOIN OPENQUERY(Oracle, "SELECT [cols2] from table2")
Is there any reason to prefer one over the other? One thing to keep in mind: we are on a limitation on how long the query can run to access the Oracle server.
I'd go with your first option especially if your query contains a where clause to select a sub-set of the data in the tables.
It will require less work on both servers, assuming there are indices on the tables in the Oracle server that support the join operation.
If the inner join significantly reduces the total number of rows, then option 1 will result in much less network traffic (since you won't have all the rows from table1 having to go across the db link
What hamishmcn said applies.
Also, SQL Server doesn't really know anything about the indexes or statistics or cache kept by the oracle server. Therefore, the oracle server can probably do a much more efficient job with the join than the sql server can.