简体   繁体   English

SQL Server查询可提高链接服务器上的性能

[英]SQL Server query improve performance on linked server

How can I improve the next SQL Server query? 如何改善下一个SQL Server查询?

SELECT SUM(Qty) 
FROM Products
WHERE Type = 'SODA'
AND (Code LIKE 'A5%'
OR Code IN('DHA2','JHU8','KML2','LQA1','ZSX2')) 

Takes a lot of time in execution. 执行需要很多时间。

Thanks in advance. 提前致谢。

I'm getting data from a DB2 database through Linked Server. 我正在通过链接服务器从DB2数据库中获取数据。 I can't build indexes just read. 我不能建立索引只是阅读。

Id   int
Name nvarchar(100)
Type nvarchar(100)
Code nvarchar(100)
Qty  int

First, try creating an index on products(type, code, qty) . 首先,尝试在products(type, code, qty)上创建索引。 That might greatly improve performance. 这可能会大大提高性能。

Then, if that doesn't work, try rewriting the query as: 然后,如果这样不起作用,请尝试将查询重写为:

select sum(qty)
from ((select qty
       from products
       where type = 'SODA' and
             code like 'A5%'
      ) union all
      (select qty
       from products
       where type = 'SODA' and
             code IN ('DHA2','JHU8','KML2','LQA1','ZSX2'))
      )
     ) t;

This may look more complicated but sometimes or interferes with query optimization. 这可能看起来比较复杂,但有时or优化查询干扰。

This is a linked server query. 这是一个链接服务器查询。 You don't state how you're executing it so I'm assuming it is using the linked server name in the FROM. 您没有说明如何执行它,所以我假设它正在使用FROM中的链接服务器名称。 If you look at the query plan, all you will see is Remote Query --> Computer Scalar --> Select. 如果查看查询计划,您将看到的只是远程查询->计算机标量->选择。 That basically means all of the data you are wanting to compute is copied into tempDB then calculated. 这基本上意味着将要计算的所有数据都复制到tempDB中,然后进行计算。

If you do the query as an EXEC at or OPENQUERY the query is executed remotely then the only thing to return is the result. 如果以EXEC at或OPENQUERY的形式执行查询,则查询将远程执行,那么唯一要返回的就是结果。

Try this: 尝试这个:

select *
from 
openquery(LinkedServerName, '   
SELECT SUM(Qty) Total
FROM Products
WHERE Type = ''SODA''
AND (Code LIKE ''A5%''
OR Code IN(''DHA2'',''JHU8'',''KML2'',''LQA1'',''ZSX2'')) ')

Since you using a linked server and also you are string comparisons, I am pretty sure your slow performance is because SQL Server has to bring the whole Product table from the DB2 database to a local copy, then compare the strings according to your current collation settings. 由于您使用的是链接服务器,而且也是字符串比较,所以我可以肯定,您的性能较慢是因为SQL Server必须将整个Product表从DB2数据库带到本地副本,然后根据当前的排序规则设置来比较字符串。 You see, comparing strings may have different results on different collation settings, and on different database systems. 您会看到,比较字符串在不同的排序规则设置和不同的数据库系统上可能会有不同的结果。 When linking between Microsoft SQL Servers databases you can enable Collation compatibility so when the query is executed it will trust the linked server decision about string comparison and sorting. 在Microsoft SQL Servers数据库之间进行链接时,可以启用归类兼容性,因此在执行查询时,它将信任链接服务器有关字符串比较和排序的决定。 I don't know if collation compatibility is possible between Microsoft SQL Server and DB2. 我不知道Microsoft SQL Server和DB2之间是否可以进行排序规则兼容性。 So the solution to the problem will be to use OPENROWSET instead of a linked server. 因此,解决该问题的方法是使用OPENROWSET而不是链接服务器。

Something like this: 像这样:

 SELECT * FROM OPENROWSET 
 ('DB2OLEDB',Netlib=SNA;NetAddr=;NetPort=;RemoteLU=OLYMPIA;LocalLU=LOCAL;ModeName=QPCSUPP;User ID=WNW3XX;Password=WNW3XX;InitCat=OLYMPIA;Default Schema=WNW3XX;PkgCol=WNW3XX;TPName=;Commit=YES;IsoLvl=NC;AccMode=;CCSID=37;PCCodePage=1252;BinAsChar=NO;Data Source=Sample',
 'SELECT SUM(Qty) 
FROM Products
WHERE Type = ''SODA''
AND (Code LIKE ''A5%''
OR Code IN(''DHA2'',''JHU8'',''KML2'',''LQA1'',''ZSX2'')) ' )

See more info in linked server to DB2 using Microsoft OLE DB provider for DB2 使用用于DB2的Microsoft OLE DB提供程序查看链接到DB2的服务器中的更多信息

And in OPENROWSET (Transact-SQL) 并在OPENROWSET(Transact-SQL)中

If it takes that long, I suggest you create a table with the same structure, then build some helpful indexes, then execute your query against this local table: 如果花费那么长时间,建议您创建一个具有相同结构的表,然后构建一些有用的索引,然后针对此本地表执行查询:

DELETE FROM Products

INSERT INTO Products
SELECT * FROM RemoteServer.database_name..Products

SELECT SUM(Qty) 
FROM Products
WHERE Type = 'SODA'
AND (Code LIKE 'A5%'
OR Code IN('DHA2','JHU8','KML2','LQA1','ZSX2')) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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