简体   繁体   中英

sql server - Openquery vs 4part name

A view that references a remote server

  1. 4part name ([ServerName], [DatabaseName], [Owner], [Object Name]

  2. OpenQuery

Which is better performance?

Why is performance good?

AFAIK, it depends a lot on your remote server type. With recent SQL version (2016) on both server (local and remote), I didn't noticed any difference. If your remote server is anything else (postgres, mysql...) your really should use OpenQuery as it executes the query on the remote server, getting only the correct resultset. If you use the 4 part name, SQL server will order and filter on local.

For example, take a 4 million record table and execute a query like :

SELECT * FROM reoteserver.database.schema.table where id = 4

With openquery, sql server will get only the record with id 4. Without, it will get all the table, and then filter it to get the id 4.

Late to the party here, but the difference essentially is that 4 part queries are executed locally, thus cannot utilise indexes or keys since the local server doesn't know about them. Instead it essentially retrieves the entire object, then applies the filter. On a small table, you would be unlikely to notice a difference, but on a table with millions of rows, you'd notice a difference. Openquery essentially tells the remote server to execute the query on it's behalf then pass the result back.

General rule I would say is; NEVER join on to a table using 4 part. Only join using Openquery and I would even avoid that where possible, but that's more of a personal preference.

However, 4 part SP execution ie EXEC ServerName.DBName.SchemaName.ObjectName is essentially the same since that also tells the remote server to execute the query on its behalf.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM