When I run this query on a SQL Server 2022 server with a linked server connection to PostgreSQL:
SELECT TOP 1 *
FROM PGSTACK.stackoverflow.[public].users
WHERE Id = 1;
SQL Server fetches the entire contents of the remote users table - all of the rows - over the network, and filters it all locally. The query takes >3 minutes to run.
The actual query plan doesn't show a local filter - it implies that SQL Server is only getting 1 row from the remote server:
- Question 1: Can that (getting the entire contents of the remote table) be avoided without rewriting the query to use OPENQUERY?
- Question 2: Even just trying to get the estimated execution plan actually fetches all of the rows in the remote table, and takes >3 minutes just to get the estimated plan. Can that be avoided?
Additional technical details:
- The remote server has a primary key on id, and the query runs in milliseconds on Postgres
- The remote Postgres server's pg_stat_activity shows that SQL Server is running this query:
select * from "stackoverflow"."public"."users"
- note the lack of any filter on the table - Postgres ODBC driver 16.00 2023-09-16, latest version from here
- SQL Server 2022 build 16.0.4095.4
- Network control panel even shows throughput going through the roof once the query starts - and again, I'm only pulling one row here:
users
table in the remote server?