【问题标题】:SQL Server Compact query runs for around 45 minsSQL Server Compact 查询运行大约 45 分钟
【发布时间】:2018-08-22 13:25:10
【问题描述】:
SELECT 
    tbl_sale.SALES_ID,
    tbl_sale.Sales_Date,
    tbl_sale.Sales_Time,
    tbl_users.user_name,
    tbl_sale.customer_id,
    tbl_customer.customer_name,
    tbl_sale.grand_disc,
    tbl_sale.collection_full,
    tbl_sale.term_of_payment,
    tbl_sale.consin1,
    tbl_sale.consin2,
    tbl_sale.narration,
    tbl_sale_details.item_id,
    tbl_item.item_name,
    tbl_sale_details.quantity,
    tbl_sale_details.cost,
    tbl_sale_details.price,
    tbl_sale_details.vat,
    tbl_sale_details.disc,
    tbl_sale_details.total_cost,
    tbl_sale_details.total_price,
    tbl_sale_details.sub_total
FROM
    tbl_customer
INNER JOIN 
    tbl_sale ON tbl_customer.customer_id = tbl_sale.customer_id
INNER JOIN 
    tbl_users ON tbl_sale.User_ID = tbl_users.User_ID
LEFT OUTER JOIN 
    tbl_item 
INNER JOIN 
    tbl_sale_details ON tbl_item.item_id = tbl_sale_details.item_id
    ON tbl_sale.SALES_ID = tbl_sale_details.SALES_ID
WHERE 
    (tbl_sale.Sales_Date >= '1/1/2018'
     AND tbl_sale.Sales_Date <= ' 08/22/2018');

我是一名学习者。我有一个 C# 代码生成两个日期之间销售的所有商品的水晶报告。

当日期范围较小时,大约需要 3 分钟,而大约需要 8 个月。 大约需要 45 分钟以上。

在我的 8 核 CPU 上,CPU 使用率只有 7%。

我使用 CompactView 来测试这个查询。执行此查询时,应用程序将无响应

我读到 SQL Server Compact Edition 内连接可能很慢,并尝试了左连接

我尝试在数据库中的几乎列上创建索引以加快速度,但它只是略微改进

例如:

CREATE INDEX idxCustId ON tbl_customer(customer_id);

现在我的技巧已经用完了..

任何人都可以就我可能做错的事情提出建议吗?

【问题讨论】:

  • 扩展@DragandDrop 所说的内容;在列上应用索引 - 可以解决问题,但如果你只是尝试应用索引失败,而不了解你实际在做什么,你最终可能会弊大于利。您不仅需要查看表格的设计,还需要查看该表格的使用方式。索引应该像手术刀一样使用,而不是霰弹枪。
  • 为了扩展现有答案,我很抱歉告诉他们没有一个接近答案。查询计划缓存将发生,并且两者将很快具有相同的性能。实际上问题查询和答案查询之间没有区别(除了日期间隔的打开和关闭/范围是另一个主题之外)。 “唯一的区别”是缓存的计划。 Using lower case for SELECT AND WHERE 可能具有相同的效果。对sql的工作原理有很深的误解..
  • 嗨@rajeswan-perumal,您应该尝试查询的解释计划,它应该让您更深入地了解如何优化查询。欢迎来到 SO,您的问题表述得很好。

标签: sql sql-server-ce


【解决方案1】:

如果没有实际数据,我很难测试这段代码......但试试这个:

SELECT tbl_sale.SALES_ID,
   tbl_sale.Sales_Date,
   tbl_sale.Sales_Time,
   tbl_users.user_name,
   tbl_sale.customer_id,
   tbl_customer.customer_name,
   tbl_sale.grand_disc,
   tbl_sale.collection_full,
   tbl_sale.term_of_payment,
   tbl_sale.consin1,
   tbl_sale.consin2,
   tbl_sale.narration,
   tbl_sale_details.item_id,
   tbl_item.item_name,
   tbl_sale_details.quantity,
   tbl_sale_details.cost,
   tbl_sale_details.price,
   tbl_sale_details.vat,
   tbl_sale_details.disc,
   tbl_sale_details.total_cost,
   tbl_sale_details.total_price,
   tbl_sale_details.sub_total
FROM tbl_customer
INNER JOIN tbl_sale        ON tbl_customer.customer_id = tbl_sale.customer_id 
                            and tbl_sale.Sales_Date between  '1/1/2018' and ' 08/22/2018'
INNER JOIN tbl_users        ON tbl_sale.User_ID = tbl_users.User_ID
INNER JOIN tbl_sale_details ON tbl_sale.SALES_ID = tbl_sale_details.SALES_ID
LEFT OUTER JOIN tbl_item ON tbl_item.item_id = tbl_sale_details.item_id

我基本上把内连接放在第一位,并将 WHERE 子句作为连接的一部分。这只是您可以用来加快逻辑速度的一些技巧。

【讨论】:

  • 成功了,但我不知道为什么这个很快而我的很慢。如果你有时间,有什么解释吗?
  • 查看我在下方编辑中所做的评论。简而言之,您只是在早期过滤掉数据,以使以后的工作“更少”。
  • 可能是因为witch语句中的顺序是进程docs.microsoft.com/en-us/sql/t-sql/queries/…
  • 但是他们的执行计划应该是一样的!我推荐阅读stackoverflow.com/questions/2509987/…
  • 我猜这个新查询有一个新的执行计划,不是缓存等。所以它解决了这个问题而不修复它。
猜你喜欢
  • 1970-01-01
  • 2012-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多