Sql 查询速度太慢
几天前,我编写了一个查询,它执行得很快,但现在需要 1 小时。 该查询在我的 SQL7 服务器上运行,大约需要 10 秒。 该查询存在于另一台 SQL7 服务器上,直到上周,它花费了大约 10秒。 两台服务器的配置相同。只是硬件不同。
现在,在第二台服务器上,此查询需要大约 30 分钟才能提取 s 细节相同,但任何人都改变了任何细节。
如果我在没有Where的情况下执行此查询,它将显示7中的详细信息 秒。 如果问题出在哪里,此查询仍然需要大约相同的时间
Few days ago I wrote one query and it gets executes quickly but now a days it takes 1 hrs.
This query run on my SQL7 server and it takes about 10 seconds.
This query exists on another SQL7 server and until last week it took about
10 seconds.
The configuration of both servers are same. Only the hardware is different.
Now, on the second server this query takes about 30 minutes to extract the s
ame details, but anybody has changed any details.
If I execute this query without Where, it'll show me the details in 7
seconds.
This query still takes about same time if Where is problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有看到查询和可能的数据,除了提供提示之外我无能为力。
希望这有帮助。
Without seeing the query and probably the data I can't do a lot other than offer tips.
Hope this helps.
如果不知道表中有多少数据,也不知道您的架构,则很难给出明确的答案,但需要注意的是:
UPDATE STATS
或DBCC REINDEX
。WHERE
子句和JOIN
谓词中使用的列添加索引。LEFT JOIN
的地方,重新访问逻辑并查看它是否需要为 LEFT 或者是否可以转换为 INNER JOIN。有时人们使用 LEFT JOIN,但是当您检查查询其余部分中的逻辑时,有时会发现 LEFT JOIN 没有给您带来任何好处(因为,例如,有人可能添加了WHERE col IS NOT NULL
针对连接表的谓词)。 INNER JOIN 可以更快,因此值得回顾所有这些。如果我们可以看到查询,那么提出建议就会容易得多。
Without knowing how much data is going into your tables, and not knowing your schema, it's hard to give a definitive answer but things to look at:
UPDATE STATS
orDBCC REINDEX
.WHERE
clauses andJOIN
predicates.OR
clauses (i.e, where you doWHERE table1.col1 = @somevalue OR table2.col2 = @someothervalue
). SQL can't use indexes effectively with this construct and you may get better performance by splitting the query into two andUNION
'ing the results.LEFT JOIN
, revisit the logic and see if it needs to be a LEFT or whether it can be turned into an INNER JOIN. Sometimes people use LEFT JOINs, but when you examine the logic in the rest of the query, it can sometimes be apparent that the LEFT JOIN gives you nothing (because, for example, someone may had added aWHERE col IS NOT NULL
predicate against the joined table). INNER JOINs can be faster, so it's worth reviewing all of these.It would be a lot easier to suggest things if we could see the query.