SQL Server Compact 查询运行大约 45 分钟

标签 sql sql-server-ce

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# 代码,生成两个日期之间销售的所有商品的 Crystal 报告。

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

在我的 8 核 CPU 上,CPU 使用率仅为 7%。

我使用 CompactView 来测试此查询。执行此查询时应用程序将不再响应

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

我尝试在数据库中的几乎所有列上创建索引来加速,但仅略有改善

例如:

CREATE INDEX idxCustId ON tbl_customer(customer_id);

现在我已经没有技巧了..

任何人都可以建议我可能做错了什么吗?

最佳答案

在没有实际数据的情况下,很难在我这边测试这段代码......但是试试这个:

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 子句作为联接的一部分。这只是您可以用来加快逻辑速度的一些技巧。

关于SQL Server Compact 查询运行大约 45 分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51967959/

相关文章:

sql-server-2008 - 是否可以从 SSIS 包中将数据脚本化为 Insert 语句?

.net - 应用程序在 Windows Vista 上崩溃

sql - 为什么 SQL 字段名有时拼写为 `this`?

sql - 从表中查找最大值

mysql - LEFT JOIN 查询 MYSQL 不工作

sql - 复合键每组行的序列号

c# - 如何使用 C# 连接到 Winform 应用程序中的 .sdf 文件?

c# - 如何指定属性应生成 TEXT 列而不是 nvarchar(4000)

tsql - 如何在 SQL Server CE 中连接两个具有不同列的表?

sql - 将 MS Sql 结果设置为变量并重用它