为什么运行此查询时会出现“超时已过期”错误?

发布于 2024-07-19 07:48:37 字数 1470 浏览 7 评论 0原文

我正在使用 ODBC 在 SQL Server 2008 上运行此查询。 如果我取出最后一部分(OR li.DATEGENERATED >= (...))),那么我不会收到错误...但我可以在 SSMS 中运行它。

SELECT COUNT(*) FROM dbo.POITEMST li
 WHERE (? IS NULL OR li.DATEGENERATED >= ?)
   AND (? IS NULL OR (li.DATEGENERATED >= (
    SELECT MAX(t.DATEGENERATED) FROM dbo.POITEMST t WHERE t.PONUM = ?)))

以下是我设置参数的方式

DbParameter startDateParam = cmd.CreateParameter();
startDateParam.ParameterName = "StartDate";
startDateParam.Value = (StartDate.HasValue ? (object)StartDate.Value : (object)(new DateTime(1753, 2, 2)));
cmd.Parameters.Add(startDateParam);

startDateParam = cmd.CreateParameter();
startDateParam.ParameterName = "StartDate";
startDateParam.Value = (StartDate.HasValue ? (object)StartDate.Value : (object)(new DateTime(1753, 2, 2)));
cmd.Parameters.Add(startDateParam);

DbParameter startPoParam = cmd.CreateParameter();
startPoParam.ParameterName = "StartPO";
startPoParam.DbType = DbType.String;
startPoParam.Value = (string.IsNullOrEmpty(StartPurchaseOrder) ? (object)DBNull.Value : (object)StartPurchaseOrder);
cmd.Parameters.Add(startPoParam);

startPoParam = cmd.CreateParameter();
startPoParam.ParameterName = "StartPO";
startPoParam.DbType = DbType.String;
startPoParam.Value = (string.IsNullOrEmpty(StartPurchaseOrder) ? (object)DBNull.Value : (object)StartPurchaseOrder);
cmd.Parameters.Add(startPoParam);

编辑:这些日期仅在那里,因为我试图弄清楚这一点...如果没有开始日期,它之前会传入 DBNull。

I'm running this query on SQL Server 2008 using ODBC. If I take out the last part (OR li.DATEGENERATED >= (...))) then I don't get the error... but I can run it in SSMS just fine.

SELECT COUNT(*) FROM dbo.POITEMST li
 WHERE (? IS NULL OR li.DATEGENERATED >= ?)
   AND (? IS NULL OR (li.DATEGENERATED >= (
    SELECT MAX(t.DATEGENERATED) FROM dbo.POITEMST t WHERE t.PONUM = ?)))

Here is how I'm setting up parameters

DbParameter startDateParam = cmd.CreateParameter();
startDateParam.ParameterName = "StartDate";
startDateParam.Value = (StartDate.HasValue ? (object)StartDate.Value : (object)(new DateTime(1753, 2, 2)));
cmd.Parameters.Add(startDateParam);

startDateParam = cmd.CreateParameter();
startDateParam.ParameterName = "StartDate";
startDateParam.Value = (StartDate.HasValue ? (object)StartDate.Value : (object)(new DateTime(1753, 2, 2)));
cmd.Parameters.Add(startDateParam);

DbParameter startPoParam = cmd.CreateParameter();
startPoParam.ParameterName = "StartPO";
startPoParam.DbType = DbType.String;
startPoParam.Value = (string.IsNullOrEmpty(StartPurchaseOrder) ? (object)DBNull.Value : (object)StartPurchaseOrder);
cmd.Parameters.Add(startPoParam);

startPoParam = cmd.CreateParameter();
startPoParam.ParameterName = "StartPO";
startPoParam.DbType = DbType.String;
startPoParam.Value = (string.IsNullOrEmpty(StartPurchaseOrder) ? (object)DBNull.Value : (object)StartPurchaseOrder);
cmd.Parameters.Add(startPoParam);

EDIT: Those dates are only in there because I was trying to figure this out... it was passing in DBNull before if there wasn't a start date.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

吻风 2024-07-26 07:48:37

分而治之。 从具有硬编码值(NULL 和非空)的 T-SQL 开始。 从最简单的查询开始并构建,在每一步检查查询计划。 您可能会发现存在索引问题等。解决该问题然后返回代码。

如果没有更多架构信息(表布局、索引等),仅使用查询确实很难提供太多帮助。

Divide and conquer. Start with T-SQL with hard-coded values, both NULL and non-null. Start with the simplest query and build up, examing the query plan at each step. You may find you have indexing problems, etc. Resolve that then go back to the code.

It's really hard to help much with just a query without more schema info - table layouts, indexes, etc.

宁愿没拥抱 2024-07-26 07:48:37

在 ssms 中运行需要多长时间? 如果您可以在启用执行计划的情况下运行查询(查询菜单、执行计划——不是估计的计划)。如果存在索引问题,这应该会让您在执行计划窗格中知道。

您或您的 dba 可以运行 SQL 分析器吗? 并捕获实际传递到 sql server 的内容,以确认它符合您的期望。

How long did it take to run in ssms ? If you can run the query with the execution plan enabled (query menu, execution plan -- not the estimated one.) If there is an index issue this should let you know on the execution plan pane.

Can you or your dba run SQL profiler; and capture what is actually being passed into sql server to confirm it matches what you expect.

初心未许 2024-07-26 07:48:37

我认为您可以重构您的查询以提高性能:

SELECT COUNT(*) FROM dbo.POITEMST li
WHERE li.DATEGENERATED >= ISNULL(?,'1753-02-02')
AND li.DATEGENERATED >= ISNULL((SELECT MAX(t.DATEGENERATED) FROM dbo.POITEMST t WHERE t.PONUM = ?), '1753-02-02')

将与 null 的比较与 OR 结合起来似乎是问题的一部分。

希望这有帮助,

比尔

I think you can refactor your query to improve performance:

SELECT COUNT(*) FROM dbo.POITEMST li
WHERE li.DATEGENERATED >= ISNULL(?,'1753-02-02')
AND li.DATEGENERATED >= ISNULL((SELECT MAX(t.DATEGENERATED) FROM dbo.POITEMST t WHERE t.PONUM = ?), '1753-02-02')

Combining comparison against null with OR looks to be part of the problem.

Hope this helps,

Bill

愁杀 2024-07-26 07:48:37

执行以下操作:

  1. 重构查询
  2. 使其成为存储过程
  3. 在适当的列上创建索引

根据我的经验,当同一查询的 SSMS 和 ODBC 之间出现性能差异时,索引会产生巨大差异。

Do the following:

  1. Refactor the query
  2. Make it into a stored procedure
  3. Create indexes on the appropriate columns

In my experience, the indexes make a massive difference when experiencing performance differences between SSMS and ODBC of the same query.

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