Sybase - How to join 2 tables from 2 different server - sybase

I have 2 Sybase db like this:
10.0.0.140:4300/dbA and 20.0.0.140:4300/dbB
Now I need to join 2 these tables
SELECT t1.Id, t1.name , t2.id, t2.value FROM 10.0.0.140:4300.dbA.dbo.table1 t1 INNER JOIN
20.0.0.140:4300.dbb.dbo.table2 t2 ON t1.id = t2.id
How could I do it with Sybase query?
Thanks

You need to create a proxy table in one of the servers, mapping to the table in the other server. Then join the proxy table with the local 'resl' table.

Related

SQL get counts using subqueries from multiple linked tables

Suppose I have tables 1-4, all the other tables are linked to table1. For what its worth, table1, table2 and table3 are relatively small but table4 contains a lot of data.
Now I have the following query:
SELECT t1.id
, (SELECT COUNT(*) FROM table2 WHERE table1_id = t1.id) AS t2_count
, (SELECT COUNT(*) FROM table3 WHERE table1_id = t1.id) AS t3_count
, (SELECT COUNT(*) FROM table4 WHERE table1_id = t1.id) AS t4_count
FROM table1 t1
Due to the fact that the subqueries are dependent/correlated I assumed that there must be a better way (performance wise) to get the data.
I tried to do the following but it drastically increased the execution time (from about 2s to 35s). I'm guessing that the multiple left joins creates a very big data set?!
SELECT t1.id
, COUNT(t2.id) AS t2_count
, COUNT(t3.id) AS t3_count
, COUNT(t4.id) AS t4_count
FROM table1 t1
LEFT JOIN table2 t2 ON t2.table1_id = t1.id
LEFT JOIN table3 t3 ON t3.table1_id = t1.id
LEFT JOIN table4 t4 ON t4.table1_id = t1.id
GROUP BY t1.id
Is there better way to get the counts? I don't need the data from the other tables.
UPDATE:
Bart's answer got me thinking that the table1_id columns are nullable. I added a IS NOT NULL check to the WHERE clauses and this brought the time down to 1s.
SELECT t1.id
, (SELECT COUNT(*) FROM table2 WHERE table1_id IS NOT NULL AND table1_id = t1.id) AS t2_count
, (SELECT COUNT(*) FROM table3 WHERE table1_id IS NOT NULL AND table1_id = t1.id) AS t3_count
, (SELECT COUNT(*) FROM table4 WHERE table1_id IS NOT NULL AND table1_id = t1.id) AS t4_count
FROM table1 t1
I guess not. If you execute a SELECT COUNT(*) FROM [table], it should perform a count on the table's PK. That should be pretty fast, even for very large tables.
Is your table4 a real table (and not a view, or a table-valued function, or something else that looks like a table)? And does it have a primary key? If so, I don't think that the performance of a SELECT COUNT(*) FROM [table4] query can be increased significantly.
It may also be the case, that your table4 is heavily targeted (in concurrent transactions over multiple connections), or perhaps your SQL Server is doing some heavy IO or computations. I cannot assume anything about that, however. You may try to check if your query is also slow on a restored database backup on a physically separate test server.

rank() equivalent for SQL Server 2000

I am using SQL Server 2000 so sadly common table expressions are out but I have the following 2 table structure:
Invoices
id
InvoiceQueryReasons
id
invoice_id
reason
date_queried
I want to inner join from Invoices to InvoiceQueryReasons and only get 1 row for each invoice that selects the most recent InvoiceQueryReasons record.
If I was using a CTE with postgress I would do something like:
SELECT *
FROM
(SELECT
"i"."id", "iq"."reason", "iq"."date_queried",
rank() OVER (PARTITION BY "invoice_id" ORDER BY "date_queried" DESC) AS "invoice_query_rnk",
FROM "invoices" i
INNER JOIN "InvoiceQueryReasons" iq ON ("i"."id" = "iq"."invoice_id")
) AS "iqrs"
INNER JOIN
"invoices" ON ("iqrs"."id" = "invoices"."id")
WHERE
("invoice_query_rnk" = 1)
Apologies if the query is not exactly right, I am writing this on a non-dev machine.
How could I write a similar query in SQL Server 2000 where I do not have common table expression?
This is the way I always used to do this in SQL Server 2000:
FROM Table1
INNER JOIN Table2 ON Table2.PrimaryKey=(
SELECT TOP 1 t2.PrimaryKey
FROM Table2 t2
WHERE t2.ForeignKey=Table1.PrimaryKey
ORDER BY t2 DateColumn DESC
)

SQL Server inner join

How to choose main table when joining multiple tables using inner join?
A) Should I choose main table depending on its number of columns/rows (For example large main as main table or to keep larger table as join table)?
B) If I choose the table containing column that I use in where condition as main table , will there be any performance benefit ?
For example lets say there are 2 tables. Table1 & Table2 . Will there be any performance difference between two solutions given below
Solution 1 :
select t1.empid , t1.name , t1.dept , t2.add , t2.city , t2.country
from Table1 t1
inner join Table2 t2 on t2.empid = t1.empid
where t1.year = 2010
Solution 2 :
select t1.empid , t1.name , t1.dept , t2.add , t2.city , t2.country
from Table2 t2
inner join Table1 t1 on t1.empid = t2.empid
where t1.year = 2010
There is no difference. SQL Server will pick "main" table and join type based on table statistics.
Example:
Table1 contains 5 rows (and only one with year 2010). Table2 contains 10000 rows.
SQL Server will generate Nested Loops join with Table1 as outer input, Table2 as inner input, to get 1 run over 1000 rows. It will definitely not generate 10000 cycles over 1 row.
You still can get different execution plans for statements above, but only in case if SQL Server will decide that plan should be trivial and will skip the optimization phase (because tables are almost empty, for example).
The main table should be the one that you are using the most columns from, so if you are using table1 with 4 columns and you need to get one column out of table2.

Select data (join?) from only one table using an id from another table

I have two tables, that we'll call t1 and t2. I want to select the data in t1 that has a certain ID that I can only find using a where clause in t2. I don't want to select the data in t2 (many duplicate column names with different data) so how do I do that?
try this
select * from t1 where t1.Id in (select distinct Id from t2)
Another approach is to join the tables
SELECT * FROM t1
JOIN t2 on t1.id = t2.id
You are joining them on a specific ID common between the 2 tables.

How to update a Sql Server table column while join two tables?

I'm trying to update a column while joining it to another table. I've used below query, but it gave me error.
UPDATE TABLE_2
INNER JOIN
TABLE_1
ON (T2.ID=T1.ID)
SET TABLE_2.DEPT='HUMAN RESOURCE'
WHERE TABLE_2.DEPT='HR'
AND T1.COMPANY =1
Can anyone help me on this?
Thanks.
For one thing, you're using table aliases that aren't defined anywhere (T2, T1 etc) and that may very well solve your problem. If not, the correct syntax very much depends on SQL flavor.
For example, in SQL Server the syntax is
UPDATE T2
SET T2.dept = 'HUMAN RESOURCE'
FROM Table2 T2
INNER JOIN Table1 T1
ON T1.[ID] = T2.[ID]
Although you don't even need a join here really, you just want
UPDATE Table2 T2
SET T2.dept = 'HUMAN RESOURCE'
WHERE EXISTS(SELECT * FROM Table1 T1
ON T1.[ID] = T2.[ID])
In MySQL the syntax is
UPDATE FROM TABLE2 AS T2
INNER JOIN TABLE1 as T1
ON T2.id = T1.id
SET T2.Dept = 'Human Resources'
Of Course, the WHERE EXISTS approach also works for MySQL
UPDATE FROM Table2 AS T2
SET Dept="Human Resources"
WHERE EXISTS (SELECT * FROM Table1 T1
ON T1.[ID] = T2.[ID]);
If it is MSSQL, then the query should be
UPDATE TABLE_2 SET DEPT='Human Resource'
FROM TABLE_1
WHERE TABLE_2.ID = TABLE_1.ID
AND TABLE_2.DEPT = 'HR'
AND TABLE_1.COMPANY = 1
UPDATE TABLE_2 SET DEPT='Human Resource'
FROM TABLE_1,Table2
WHERE TABLE_2.ID = TABLE_1.ID
AND TABLE_2.DEPT = 'HR'
AND TABLE_1.COMPANY = 1
Because when we update a table in joining then we use both tables in from close

Resources