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+分钟。
我的 7 核 CPU 上的 CPU 使用率仅为 8%。
我使用 CompactView 来测试此查询。执行此查询时,应用程序将变为无响应
我读到SQL Server压缩版内部连接可能很慢,并尝试了左连接
我尝试在数据库中的几乎列上创建索引以加快速度,但它只略有改进
例如:
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 子句成为连接的一部分。这只是您可以使用的几个技巧,以加快逻辑速度。