【发布时间】:2019-12-28 11:45:58
【问题描述】:
我使用 MsSQL。我有一个“工作”表,它有 140 列,其中包含超过 400 万条记录。该表的列主要是 varchar 和 bit。
该表的 40 列连接到其他一些表。就像“issuers”表中的“issuerid”,“files”中的“fileid”......
表的索引只在“fileid”上,它是非唯一且非集群的。
我的基本查询如下:
select issuerid,count(id) as total , sum(case when X_Status=1 then 1 else 0 end) P_Count
from jobs where 1=1 and issuerid='1001' and creationdate between '01/01/2019 12:00:01 AM' and '06/30/2019 11:59:59 PM' group by issuerid
查询时长为:1分20秒(PC有SSD和4GB内存)
所以我尝试在 issuerid 上建立索引,但影响不大。
我有很多关于我的 asp 页面的表的查询。例如,总和情况发生了很大变化;
sum(case when Y_Status=1 then 1 else 0 end) P_Count
像这样。
所以甚至尝试让表中的 2 列并执行此查询
select count(id) as, sum(case when X_Status=1 then 1 else 0 end) P_Count from newjobs where 1=1
这需要大约 30 秒。
我阅读了许多主题和文章以提高查询性能,但没有奏效。有人有什么想法可以分享吗?
谢谢。
【问题讨论】:
-
id、X_Status、创建日期都应用了哪些索引? issuerid='1001' 也是这个数字吗?如果它是一个 int 会更好,然后你可以过滤一个更快的 int。我提到的所有列都将受益于索引
-
包括表和它的索引 DDL,查询执行计划会很棒。使用brentozar.com/pastetheplan。
-
@jimmy8ball 首先,ID 已经是主键,我在 fileid 上有。我尝试将所有内容分别索引到creationdate、issuerid 和X_Status。但不影响。我在没有创建日期条件的情况下运行查询,但仍然需要很长时间。 issuerid 是“bigint” 感谢您的关注。
-
你的 issuerid 是 int 吗?
-
如果 isserid 是 int 则不要使用单引号,只需 issuerid=1001 会更快。使用单引号会强制在文本中处理它
标签: sql sql-server sql-server-2008 ssms ssms-2014