如何提高这组SQL语句的效率
首先是自定义的一个函数
CREATE FUNCTION GetLastBalanceDate()
RETURNS Datetime AS
BEGIN
declare @Date datetime
select @Date=max(BillDate) from Balance
if @Date is null
set @Date='2007-1-31'
Return @Date
END
然后类似这样的视图多个:
CREATE VIEW dbo.VPayment
--本视图用于表示与供应商之间发生的应付加减情况
AS--收货<现金付款除外>
SELECT MainSeq,'收货' MeNO,SignDate BillDate,CustomerNO,Amt from StoreMain where BillType=1 and IsCash=0
UNION ALL--减去退货金额,由于存储的是负数,直接相加<除掉现金退货单据>
SELECT MainSeq,'退货',SignDate,CustomerNO,Amt from StoreMain where BillType=2 and IsCash=0
UNION ALL--减去结帐以后的付款
SELECT SeqNo,'付款',BillDate,CustomerNO,-Amt from Payment
UNION ALL--减去结帐以后店面直接汇款给供应商的
SELECT SeqNO,'直接汇款',BillDate,CustomerNO,-Amt from RecPayment where IsTran=1
UNION ALL--加上采购点的公摊费用
SELECT SeqNO,'公摊费用',BillDate,UnderTaker,Amt from FeeStock where IsNULL(UnderTaker,0)<>0 and IsPool=1
UNION ALL--需要加上非公摊的费用<现金付款的采购费用不计算在内>
SELECT A.RelateSeq,B.CName,C.SignDate,A.CustomerNO,A.Amt from FeeStock A,FCode B,StoreMain C where ISNULL(A.RelateSeq,0)<>0
and A.RelateSeq=C.MainSeq and A.BillType=B.Code and B.Type=2 and A.IsPool=0 And A.IsCash=0
-------------最后的引用如下:
CREATE VIEW dbo.VPayable
AS--
select A.CustomerNO,IsNULL(B.Amt,0)+IsNULL(C.Amt,0) Amt
from Customer A
left join Payable B on A.CustomerNO=B.CustomerNO and B.BillDate=DBO.GetLastBalanceDate()
left join
(select CustomerNO,SUM(Amt) Amt from VPayment where BillDate>DBO.GetLastBalanceDate() group by CustomerNO) C
on A.CustomerNO=C.CustomerNO
where A.Type=2
SQL Server似乎在对比每条数据的时候都会去执行一次DBO.GetLastBalanceDate() ,这样一来数据量很大的时候就会很慢甚至超时
CREATE FUNCTION GetLastBalanceDate()
RETURNS Datetime AS
BEGIN
declare @Date datetime
select @Date=max(BillDate) from Balance
if @Date is null
set @Date='2007-1-31'
Return @Date
END
然后类似这样的视图多个:
CREATE VIEW dbo.VPayment
--本视图用于表示与供应商之间发生的应付加减情况
AS--收货<现金付款除外>
SELECT MainSeq,'收货' MeNO,SignDate BillDate,CustomerNO,Amt from StoreMain where BillType=1 and IsCash=0
UNION ALL--减去退货金额,由于存储的是负数,直接相加<除掉现金退货单据>
SELECT MainSeq,'退货',SignDate,CustomerNO,Amt from StoreMain where BillType=2 and IsCash=0
UNION ALL--减去结帐以后的付款
SELECT SeqNo,'付款',BillDate,CustomerNO,-Amt from Payment
UNION ALL--减去结帐以后店面直接汇款给供应商的
SELECT SeqNO,'直接汇款',BillDate,CustomerNO,-Amt from RecPayment where IsTran=1
UNION ALL--加上采购点的公摊费用
SELECT SeqNO,'公摊费用',BillDate,UnderTaker,Amt from FeeStock where IsNULL(UnderTaker,0)<>0 and IsPool=1
UNION ALL--需要加上非公摊的费用<现金付款的采购费用不计算在内>
SELECT A.RelateSeq,B.CName,C.SignDate,A.CustomerNO,A.Amt from FeeStock A,FCode B,StoreMain C where ISNULL(A.RelateSeq,0)<>0
and A.RelateSeq=C.MainSeq and A.BillType=B.Code and B.Type=2 and A.IsPool=0 And A.IsCash=0
-------------最后的引用如下:
CREATE VIEW dbo.VPayable
AS--
select A.CustomerNO,IsNULL(B.Amt,0)+IsNULL(C.Amt,0) Amt
from Customer A
left join Payable B on A.CustomerNO=B.CustomerNO and B.BillDate=DBO.GetLastBalanceDate()
left join
(select CustomerNO,SUM(Amt) Amt from VPayment where BillDate>DBO.GetLastBalanceDate() group by CustomerNO) C
on A.CustomerNO=C.CustomerNO
where A.Type=2
SQL Server似乎在对比每条数据的时候都会去执行一次DBO.GetLastBalanceDate() ,这样一来数据量很大的时候就会很慢甚至超时
...全文
相关推荐
交叉表查询sql语句
* 优化查询语句:优化查询语句可以提高交叉表查询的效率。在本文中,我们使用了交叉表查询来解决实际问题。我们创建了两个表:表1和表2。
166、Python开发利器:NumPy在深度学习和神经网络中的应用
本文介绍了Python开发中常用的库NumPy,并探索了如何利用NumPy进行人工智能领域的深度学习和神经网络开发。我们从NumPy的基础知识开始,逐步深入到深度学习和神经网络的应用,并提供了一些实用的技巧和案例。通过这些案例,我们看到了NumPy在数据预处理、模型构建、训练和评估等环节发挥重要作用。希望这篇文章能够帮助你更好地理解和应用NumPy,在Python开发中更上一层楼。
SQL SERVER 分组求和sql语句
在实际工作中,根据具体需求灵活运用这些工具,可以大大提高数据处理的效率和准确性。
SQLServer中SELECT语句的执行顺序
此外,良好的SQL编写习惯不仅可以提高效率,还能使代码更具可读性和可维护性。遵循标准的执行顺序可以使SQL语句逻辑清晰,更容易理解。
SQL语句优化在Oracle数据库中的应用.pdf
通常情况下,Oracle优化器会优先使用索引来提高查询效率,与全表扫描或表与表之间的连接操作相比,效率更高。2. SQL关键字的优化:对SQL查询语句中的关键字进行优化也很重要。


应用实例
