Sql 查询速度太慢

发布于 2024-10-15 00:44:56 字数 251 浏览 2 评论 0原文

几天前,我编写了一个查询,它执行得很快,但现在需要 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丢了幸福的猪 2024-10-22 00:44:56

如果没有看到查询和可能的数据,除了提供提示之外我无能为力。

  1. 您可以对查询施加更多限制吗?如果您可以减少所涉及的数据量,那么这将加快查询速度。
  2. 查看连接中使用的列、where 和having 子句以及order by。检查列所属的表是否包含这些列的索引。
  3. 您需要使用用户定义的函数还是可以通过其他方式完成?
  4. 您使用子查询吗?如果是这样,可以将它们拉到单独的视图中吗?

希望这有帮助。

Without seeing the query and probably the data I can't do a lot other than offer tips.

  1. Can you put more constraints on the query. If you can reduce the amount of data involved then this will speed up the query.
  2. Look at the columns used in your joins, where and having clauses and order by. Check that the tables that the columns belong to contain indices for these columns.
  3. Do you need to use the user defined function or can it be done another way?
  4. Are you using subquerys? If so can these be pulled out into separate views?

Hope this helps.

流星番茄 2024-10-22 00:44:56

如果不知道表中有多少数据,也不知道您的架构,则很难给出明确的答案,但需要注意的是:

  1. 尝试运行 UPDATE STATSDBCC REINDEX
  2. 表格上有索引吗?如果没有,请尝试向 WHERE 子句和 JOIN 谓词中使用的列添加索引。
  3. 避免跨表 OR 子句(即,您执行 WHERE table1.col1 = @somevalue OR table2.col2 = @someothervalue 的位置)。 SQL 无法通过此构造有效地使用索引,并且您可以通过将查询拆分为两个并对结果进行 UNION 操作来获得更好的性能。
  4. 您的函数 (UDF) 的作用是什么?您如何使用它们?值得注意的是,将它们放入查询的列部分会变得昂贵,因为该函数是按返回的行执行的:因此,如果函数对数据库进行选择,那么您最终会对数据库运行 n + 1 个查询(其中 n = 主选择中返回的行数)。如果可能的话,尝试设计该功能。
  5. 确保您的 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:

  1. Try running UPDATE STATS or DBCC REINDEX.
  2. Do you have any indexes on the tables? If not, try adding indexes to columns used in WHERE clauses and JOIN predicates.
  3. Avoid cross table OR clauses (i.e, where you do WHERE 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 and UNION'ing the results.
  4. What do your functions (UDFs) do and how are you using them? It's worth noting that dropping them in the columns part of a query gets expensive as the function is executed per row returned: thus if a function does a select against the database, then you end up running n + 1 queries against the database (where n = number of rows returned in the main select). Try and engineer the function out if possible.
  5. Make sure your JOINs are correct -- where you're using a 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 a WHERE 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文