大型查询的 SQL Server 性能 [英] SQL Server Performance With Large Query

查看:61
本文介绍了大型查询的 SQL Server 性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我对某些报告有几个查询,其中每个查询都从 35 个以上的表中提取数据.每个表有近 10 万条记录.所有的查询都是 Union ALL 示例

Hi everyone I have a couple of queries for some reports in which each query is pulling Data from 35+ tables. Each Table has almost 100K records. All the Queries are Union ALL for Example

;With CTE
AS
(
Select col1, col2, col3 FROM Table1 WHERE Some_Condition
UNION ALL 
Select col1, col2, col3 FROM Table2 WHERE Some_Condition
UNION ALL 
Select col1, col2, col3 FROM Table3 WHERE Some_Condition
UNION ALL 
Select col1, col2, col3 FROM Table4 WHERE Some_Condition
.
.
. And so on 
)
SELECT col1, col2, col3 FROM CTE
ORDER BY col3 DESC

到目前为止,我只在 Dev Server 上测试了这个查询,我可以看到它需要时间来获得结果.所有这 35 个以上的表彼此都不相关,这是我能想到的在结果集中获取所有所需数据的唯一方法.

So far I have only tested this query on Dev Server and I can see It takes its time to get the results. All of these 35+ tables are not related with each other and this is the only way I can think of to get all the Desired Data in result set.

  1. 有没有更好的方法来做这种查询??

  1. Is there a better way to do this kind of query ??

如果这是进行此类查询的唯一方法,我该怎么办通过进行任何更改来提高此查询的性能,如果可能的??

If this is the only way to go for this kind of query how can I improve the performance for this Query by making any changes if possible??

我的意见
我不介意在这份报告中有一些脏读.我正在考虑使用 查询提示 with nolockTransaction Isolation Level 设置为 READ UNCOMMITED.

My Opinion
I Dont mind having a few dirty-reads in this report. I was thinking of using Query hints with nolock or Transaction Isolation Level set to READ UNCOMMITED.

这些有帮助吗???

编辑
每个表都有 5-10 位列和每个位列的对应日期列,我对每个 SELECT 语句的条件类似于

Edit
Every Table has 5-10 Bit columns and a Corresponding Date column to each Bit Column and my condition for each SELECT Statement is something like

WHERE BitColumn = 1 AND DateColumn IS NULL 

同行的建议

过滤索引

CREATE NONCLUSTERED INDEX IX_Table_Column
ON TableName(BitColumn)
WHERE BitColum = 1

包含列的过滤索引

CREATE NONCLUSTERED INDEX fIX_IX_Table_Column
ON TableName(BitColumn)
INCLUDE (DateColumn)
WHERE DateColumn IS NULL

这是最好的方式吗?或者有什么建议吗???

推荐答案

有很多事情可以做来加快速度.如果我假设您需要执行这些 UNION,那么您可以通过以下方式加快查询速度:

There are lots of things that can be done to make it faster. If I assume you need to do these UNIONs, then you can speed up the query by :

  1. 缓存结果,例如,
    • 你能从整个语句创建一个索引视图吗?或者有很多不同的 WHERE 条件,所以会有很多索引视图?但要知道这会减慢对这些表的修改(插入等)
    • 你能以不同的方式缓存它吗?也许在中间层?
    • 可以提前重新计算吗?
  • 请注意,也可以过滤覆盖索引,但如果查询中的 WHERE 将具有变量/参数,并且它们可能具有过滤索引未覆盖的值(即行不包括在内)
  • 如果您可以缓存它,那就没问题了 - 不需要排序(缓存已排序)
  • 否则,排序受 CPU 限制(如果不在内存中,则受 I/O 限制).为了加快速度,您是否使用快速整理?最慢和最快整理之间的性能差异甚至可以达到 3 倍.例如,SQL_EBCDIC280_CP1_CS_AS、SQL_Latin1_General_CP1251_CS_AS、SQL_Latin1_General_CP1_CI_AS 是最快的排序规则之一.但是,如果我不知道您需要的整理特征,则很难提出建议
  • 执行 SELECT 的连接的网络数据包大小"应该是可能的最大值 - 如果结果集(行数)很大,则为 32,767 字节.这可以在客户端设置,例如,如果您在连接字符串中使用 .NET 和 SqlConnection.这将最大限度地减少从 SQL Server 发送数据时的 CPU 开销,并将提高双方的性能 - 客户端和服务器.如果网络是瓶颈,这甚至可以将性能提高百分之几十
  • 如果客户端在 SQL Server 上,则使用共享内存端点;否则 TCP/IP 以获得最佳性能
  • 如您所说,使用隔离级别读取未提交将提高性能

...

除了重写查询等之外,您可能无法进行更改,但以防万一,添加更多内存以防现在不够用,或者在内存功能中使用 SQL Server 2014 :-),...会肯定有帮助.

Probably you can't do changes beyond rewriting the query, etc. but just in case, adding more memory in case it isn't sufficient now, or using SQL Server 2014 in memory features :-), ... would surely help.

可以调整的东西太多了,但如果问题不是很具体,就很难指出关键.

There are way too many things that could be tuned but it's hard to point out the key ones if the question isn't very specific.

希望对你有所帮助

这篇关于大型查询的 SQL Server 性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆