Author |
Topic |
klaine07
Starting Member
5 Posts |
Posted - 2011-12-25 : 21:03:19
|
SET ANSI_NULLS ONHi anybody who can help me.. this is my stored procedure in my project and it runs very slow when i have a more than 100 records of data...How can i optimize this so that i will run better ? thanks..GOSET QUOTED_IDENTIFIER ONGOALTER Proc [dbo].[SPInsert_Multiple_Indirect] @TempRequestNo nvarchar(30),@RequestorFunction nvarchar(50),@ComCode nvarchar(50),@PhoneNumber nvarchar(50),@RequestorName nvarchar(100),@CustomerType nvarchar(50)as begin Declare @tblReqNo nvarchar(25), @DocumentType nvarchar(50) set @DocumentType = 'Customer Master' set @tblReqNo = (Select top 1 RequestNo from tblCustomer)--################################ TBLCUSTOMER ########## START Select top 1 RequestNo, SalesOrg, AccountGroup, Title, BusinessName, OwnerName, HouseNo, Street, PostCode, City, Telephone,MobileNo,TranspoZone,CustClass, BusinessType, BusinessTypeExt, BusinessCompType, VisitStrategy, TradeGrpMem,CustSubTradeCh,ConSubTradeCh, AcctgClerk, SalesRoute, CustGrp, TaxClass, TradeName, SuppReason, OperMarTyp, Longitude, Latitude, TrainStation, AcctAssGrp, SalesLoc, SalesDist, SalesOffice, SalesGrp, SubDemArea, FixUnDur, VariaLoadUnTime, OperTradeCh into #tblCustomer from tblcustomer Delete from #tblCustomer where RequestNo = @tblReqNo Insert into #tblCustomer Select TempRequestNo, (Select * from FNgetCode (SalesOrganization, '-')), (Select * from FNgetCode (AccountGroup, '-')), Title, TradeName, OwnerName, HouseNo, StreetAddress, --PostalCode, PostalCodeDes, TelephoneAreaCode + Telephone, MobileNumberPrefix + MobileNumber, (Select * from FNgetCode (TranspoZone, '-')), (Select * from FNgetCode (CustomerClassification, '-')), PostalCode, PostalCodeDes, Telephone, MobileNumber, (Select * from FNgetCode (TranspoZone, '-')), (Select * from FNgetCode (CustomerClassification, '-')), (Select * from FNgetCode (BusinessType, '-')), (Select * from FNgetCode (BusinessTypeExtension, '-')), (Select * from FNgetCode (BusinessComplexType, '-')), (Select * from FNgetCode (VisitStrategy, '-')), (Select * from FNgetCode (TradeGroup, '-')), (Select * from FNgetCode (CustomerSubTradeChannel, '-')),(Select * from FNgetCode (CustomerSubTradeChannel, '-')), (Select * from FNgetCode (AccountingClerk, '-')), (Select * from FNgetCode (SalesRoute, '-')), (Select * from FNgetCode (CustomerGroup, '-')), (Select * from FNgetCode (TaxClassification, '-')), (Select * from FNgetCode (TradeNameNo, '-')), (Select * from FNgetCode (SuppressionReason, '-')), (Select * from FNgetCode (OperationalMarketType, '-')), Longitude, Latitude, TrainStation, (Select * from FNgetCode (AccountAssignmentGrp, '-')), '', '', '', '', '', '', '', '' from tmpMultiIndirect where TempRequestNo = @TempRequestNo order by CNT --################################ TBLCUSTOMER ########## END--################################ TBLREQUEST ########## START Select top 1 RequestNumber, Purpose, RequestType, Location, RequestFor into #TblRequest from TblRequest Truncate table #TblRequest Insert into #TblRequest Select TemprequestNo, Purpose, RequestType, Location, TradeName from tmpMultiIndirect Where TempRequestNo = @TempRequestNo order by CNT --################################ TBLREQUEST ########## END--################################ TBLCONTACTPERSON ########## START Select top 1 RequestNo, LastName, FirstName into #tblContactPerson from tblContactPerson Truncate table #tblContactPerson Insert into #tblContactPerson Select TemprequestNo, LastName, FirstName from tmpMultiIndirect Where TempRequestNo = @TempRequestNo order by CNT --################################ TBLCONTACTPERSON ########## END--################################ TBLPARTNERFUNCTION ########## START Select top 1 RequestNo, PFcode, PFRefNo, PFDescription Into #tblPartnerFunction from tblPartnerFunction Truncate table #tblPartnerFunction Insert into #tblPartnerFunction Select TempRequestNo, 'ZW', ZWMEPWSCustomer, MEPTradename from tmpMultiIndirect Where TempRequestNo = @TempRequestNo order by CNT Select top 1 RequestNo, PFcode, PFRefNo, PFDescription Into #tblPartnerFunction_ZR from tblPartnerFunction Truncate table #tblPartnerFunction_ZR Insert into #tblPartnerFunction_ZR Select TempRequestNo, 'ZR', (Select Employeeno from RefSaleRoute aa where aa.SalesRoute = (Select * from FNgetCode (temp.SalesRoute, '-'))), (Select EmployeeDescription from RefSaleRoute bb where bb.SalesRoute = (Select * from FNgetCode (temp.SalesRoute, '-'))) from tmpMultiIndirect temp Where TempRequestNo = @TempRequestNo order by CNT --################################ TBLPARTNERFUNCTION ########## END--################################ TBLCUSTOMERTYPE ########## START Select top 1 * into #tblCustomer_Type from tblCustomer_Type Truncate table #tblCustomer_Type Insert into #tblCustomer_Type Select TempRequestNo, Purpose From tmpMultiIndirect Where TempRequestNo = @TempRequestNo order by CNT --################################ TBLCUSTOMERTYPE ########## END Alter Table #tblCustomer Add CNT int Identity(1,1) not null Alter Table #tblRequest Add CNT int Identity(1,1) not null Alter Table #tblContactPerson Add CNT int Identity(1,1) not null Alter Table #tblPartnerFunction Add CNT int Identity(1,1) not null alter Table #tblPartnerFunction_ZR Add CNT int Identity(1,1) not null Alter Table #tblCustomer_Type Add CNT int Identity(1,1) not null declare @Blg int set @Blg = 0 declare @CNT int declare tmp_cursor cursor for SELECT CNT FROM #tblCustomer where RequestNo = @TempRequestNo open tmp_cursor; fetch next from tmp_cursor into @CNT while @@fetch_status = 0 begin --Waitfor delay '00:00:00:125' if @Blg = 9 begin set @Blg = 0 end set @Blg = @Blg + 1 declare @ReqNo Nvarchar(12) declare @ReqNoDate Nvarchar(8) declare @ReqNoAdd Nvarchar(20) declare @FinalReqNo Nvarchar(30) set @ReqNoDate = Replace(Convert(Varchar(8),GETDATE(),1),'/','') set @ReqNo = Replace(Convert(Varchar(12),GETDATE(),114),':','') set @ReqNoAdd = SUBSTRING(@ReqNo,1,4) + SUBSTRING(@ReqNo,7,3) set @FinalReqNo = 'CCMCI' + @ReqNoDate + @ReqNoAdd + Convert(Varchar(5),@Blg) Update #tblCustomer set RequestNo = @FinalReqNo where CNT = @CNT Update #tblRequest set RequestNumber = @FinalReqNo where CNT = @CNT Update #tblContactPerson set RequestNo = @FinalReqNo where CNT = @CNT Update #tblPartnerFunction set RequestNo = @FinalReqNo where CNT = @CNT Update #tblPartnerFunction_ZR set RequestNo = @FinalReqNo where CNT = @CNT Update #tblCustomer_Type set RequestNo = @FinalReqNo where CNT = @CNT Declare @TempSaleRoute nvarchar(50) set @TempSaleRoute = (Select SalesRoute from #tblCustomer where CNT = @CNT) Update #tblCustomer set SalesLoc = (Select SalesLocation from RefSaleRoute where SalesRoute = @TempSaleRoute), SalesDist = (Select SalesDisCode from RefSaleRoute where SalesRoute = @TempSaleRoute), SalesOffice = (Select SalesOffCode from RefSaleRoute where SalesRoute = @TempSaleRoute), SalesGrp = (Select SalesGrpCode from RefSaleRoute where SalesRoute = @TempSaleRoute), SubDemArea = (Select SubDemandArea from RefSaleRoute where SalesRoute = @TempSaleRoute) Where CNT = @CNT Declare @TempSubTradeCh nvarchar(50) set @TempSubTradeCh = (Select CustSubTradeCh from #tblCustomer where CNT = @CNT) Update #tblCustomer set FixUnDur = (Select FixUnload from RefSubTrade where SubTrade = @TempSubTradeCh), VariaLoadUnTime = (Select UnloadingTime from RefSubTrade where SubTrade = @TempSubTradeCh), OperTradeCh = (Select OperationalTrade from RefSubTrade where SubTrade = @TempSubTradeCh) Where CNT = @CNT Declare @FindDuplicate_Train int Set @FindDuplicate_Train = (Select COUNT(*) from MstCustomer where TrainStation = (Select TrainStation from #tblCustomer where CNT = @CNT)) Set @FindDuplicate_Train = @FindDuplicate_Train + (Select COUNT(*) from tblCustomer where TrainStation = (Select TrainStation from #tblCustomer where CNT = @CNT)) if @FindDuplicate_Train = 0 begin Declare @FindDuplicate int set @FindDuplicate = (Select Count(*) from MstCustomer where BusinessName = (Select BusinessName from #tblCustomer where CNT = @CNT) and OwnerName = (Select OwnerName from #tblCustomer where CNT = @CNT) and Street = (Select Street from #tblCustomer where CNT = @CNT)) set @FindDuplicate = @FindDuplicate + (Select Count(*) from tblCustomer inner join tblrequest on tblcustomer.RequestNo = tblrequest.RequestNumber where BusinessName = (Select BusinessName from #tblCustomer where CNT = @CNT) and OwnerName = (Select OwnerName from #tblCustomer where CNT = @CNT) and Street = (Select Street from #tblCustomer where CNT = @CNT)) --and --tblrequest.RequestorName = @RequestorName)-- like '%Awaiting%') if @FindDuplicate = 0 begin Declare @FindWorkflow int set @FindWorkflow = (Select COUNT(*) from WorkFlow WHERE Location = (Select Location from #TblRequest where CNT = @CNT) and DocumentType = @DocumentType and RequestType = (Select RequestType from #TblRequest where CNT = @CNT) and requestor = @RequestorName and SalesOfficeArea = (Select SalesOffice from #tblCustomer where CNT = @CNT) and BusinessType = (Select BusinessType from #tblCustomer where CNT = @CNT) and SalesGroup = (Select SalesGrp from #tblCustomer where CNT = @CNT) and OperMarkerType = (Select OperMarTyp from #tblCustomer where CNT = @CNT) and SalesOrg = (Select SalesOrg from #tblCustomer where CNT = @CNT)) if @FindWorkflow = 0 begin Update #tblCustomer set RequestNo = 'NOWORK' where CNT = @CNT Delete from #TblRequest where CNT = @CNT Delete from #tblContactPerson where CNT = @CNT Delete from #tblPartnerFunction where CNT = @CNT Delete from #tblPartnerFunction_ZR where CNT = @CNT Delete from #tblCustomer_Type where CNT = @CNT end else begin Insert into tblrequest_workflow (requestno,POSItion, Approver, appr_email, Hierarchy) SELECT @FinalReqNo, Position, Approver, appr_email, Hierarchy FROM WorkFlow WHERE Location = (Select Location from #TblRequest where CNT = @CNT) and DocumentType = @DocumentType and RequestType = (Select RequestType from #TblRequest where CNT = @CNT) and requestor = @RequestorName and SalesOfficeArea = (Select SalesOffice from #tblCustomer where CNT = @CNT) and BusinessType = (Select BusinessType from #tblCustomer where CNT = @CNT) and SalesGroup = (Select SalesGrp from #tblCustomer where CNT = @CNT) and OperMarkerType = (Select OperMarTyp from #tblCustomer where CNT = @CNT) and SalesOrg = (Select SalesOrg from #tblCustomer where CNT = @CNT) end end else begin begin Update #tblCustomer set RequestNo = 'DUPLICATE' where CNT = @CNT Delete from #TblRequest where CNT = @CNT Delete from #tblContactPerson where CNT = @CNT Delete from #tblPartnerFunction where CNT = @CNT Delete from #tblPartnerFunction_ZR where CNT = @CNT Delete from #tblCustomer_Type where CNT = @CNT end end end else begin Update #tblCustomer set RequestNo = 'DUPLICATE_TRAIN' where CNT = @CNT Delete from #TblRequest where CNT = @CNT Delete from #tblContactPerson where CNT = @CNT Delete from #tblPartnerFunction where CNT = @CNT Delete from #tblPartnerFunction_ZR where CNT = @CNT Delete from #tblCustomer_Type where CNT = @CNT end FETCH NEXT FROM tmp_Cursor into @CNT; end CLOSE tmp_Cursor; DEALLOCATE tmp_Cursor;BEGIN TRANSACTION--################################ TBLCUSTOMER ########## START Insert into tblCustomer (RequestNo, SalesOrg, AccountGroup, Title, BusinessName, OwnerName, HouseNo, Street, PostCode, City, Telephone,MobileNo,TranspoZone,CustClass, BusinessType, BusinessTypeExt, BusinessCompType, VisitStrategy, TradeGrpMem,CustSubTradeCh,ConSubTradeCh, AcctgClerk, SalesRoute, CustGrp, TaxClass, TradeName, SuppReason, OperMarTyp, Longitude, Latitude, TrainStation, AcctAssGrp, SalesLoc, SalesDist, SalesOffice, SalesGrp, SubDemArea, ComCode, DistriChannel, Division, SearchTerm1, DiscIndicator, FixUnDur, VariaLoadUnTime, OperTradeCh) Select RequestNo, SalesOrg, AccountGroup, Title, BusinessName, OwnerName, HouseNo, Street, PostCode, City, Telephone,MobileNo,TranspoZone,CustClass, BusinessType, BusinessTypeExt, BusinessCompType, VisitStrategy, TradeGrpMem,CustSubTradeCh,ConSubTradeCh, AcctgClerk, SalesRoute, CustGrp, TaxClass, TradeName, SuppReason, OperMarTyp, Longitude, Latitude, TrainStation, AcctAssGrp, SalesLoc, SalesDist, SalesOffice, SalesGrp, SubDemArea, @ComCode ,'Z1', 'Z0', BusinessName, 'X', FixUnDur, VariaLoadUnTime, OperTradeCh from #tblCustomer Where RequestNo <> 'NOWORK' AND RequestNo <> 'DUPLICATE' AND RequestNo <> 'DUPLICATE_TRAIN' order by CNT--################################ TBLCUSTOMER ########## END--################################ TBLREQUEST ########## START Insert into tblRequest (RequestNumber, Purpose,RequestType, Location, RequestFor, CompanyCode, PhoneNumber, RequestedDate, RequestorFunction, RequestorName, CurrentStatus, DocumentType) Select RequestNumber, Purpose,RequestType, Location, RequestFor, @ComCode, @PhoneNumber, GETDATE(), @RequestorFunction, @RequestorName, 'Created', @DocumentType from #TblRequest order by CNT --################################ TBLREQUEST ########## END--################################ TBLCONTACTPERSON ########## START Insert into tblContactPerson (RequestNo, LastName, FirstName) Select RequestNo, LastName, FirstName from #tblContactPerson order by CNT--################################ TBLCONTACTPERSON ########## END--################################ TBLPARTNERFUNCTION ########## START Select RequestNo, PFcode, PFRefNo, PFDescription into #ConsolidatePF from #tblPartnerFunction_ZR Insert into #ConsolidatePF Select RequestNo, PFcode, PFRefNo, PFDescription from #tblPartnerFunction Insert into tblPartnerFunction (RequestNo, PFcode, PFRefNo, PFDescription) Select RequestNo, PFcode, PFRefNo, PFDescription from #ConsolidatePF order by RequestNo--################################ TBLPARTNERFUNCTION ########## END --################################ TBLCUSTOMERTYPE ########## START Insert into tblcustomer_type Select RequestNo, @CustomerType from #tblCustomer_Type order by CNT--################################ TBLCUSTOMERTYPE ########## END Delete from tmpMultiIndirect where TempRequestNo = @TempRequestNo Insert into tmpMultiIndirect (TempRequestNo, SalesOrganization, AccountGroup, Title, TradeName, OwnerName, HouseNo, StreetAddress, PostalCode, PostalCodeDes, Telephone, MobileNumber, TranspoZone, CustomerClassification, BusinessType, BusinessTypeExtension, BusinessComplexType, VisitStrategy, TradeGroup, CustomerSubTradeChannel, AccountingClerk, SalesRoute, CustomerGroup, TaxClassification, TradeNameNo, SuppressionReason, OperationalMarketType, Longitude, Latitude, TrainStation, AccountAssignmentGrp, TelephoneAreaCode) Select @TempRequestNo, SalesOrg, AccountGroup, Title, BusinessName, OwnerName, HouseNo, Street, PostCode, City, Telephone,MobileNo,TranspoZone,CustClass, BusinessType, BusinessTypeExt, BusinessCompType, VisitStrategy, TradeGrpMem,CustSubTradeCh, AcctgClerk, SalesRoute, CustGrp, TaxClass, TradeName, SuppReason, OperMarTyp, Longitude, Latitude, TrainStation, AcctAssGrp ,RequestNo from #tblCustomerif @@ERROR <> 0 begin ROLLBACK TRANSACTIONendelsebegin COMMIT TRANSACTION end end |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-25 : 23:31:37
|
you mean you want somebody to look into above piece of code and optimise? I dont think anybody will take the pain of doing that for free in a public forum.To start off, what you can do is analyse the costly steps in procedure and trying to find out bottleneck. Also I see cursor in code above. In most cases cursors can be replaced by set based solution which would improve the query performance a lot. If you can replace it by set based equivalent, it might work much better. If you can explain with some sample data what cursor is doing, may be somebody will able to come up with set based equivalent query------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-12-26 : 05:35:04
|
You are going about this in the wrong way:set @tblReqNo = (Select top 1 RequestNo from tblCustomer)--################################ TBLCUSTOMER ########## STARTSelect top 1 RequestNo, SalesOrg, AccountGroup, Title, BusinessName, OwnerName, HouseNo, Street,PostCode, City, Telephone,MobileNo,TranspoZone,CustClass, BusinessType, BusinessTypeExt, BusinessCompType, VisitStrategy, TradeGrpMem,CustSubTradeCh,ConSubTradeCh, AcctgClerk,SalesRoute, CustGrp, TaxClass, TradeName, SuppReason, OperMarTyp,Longitude, Latitude, TrainStation, AcctAssGrp, SalesLoc, SalesDist, SalesOffice, SalesGrp, SubDemArea, FixUnDur, VariaLoadUnTime, OperTradeCh into #tblCustomer from tblcustomer Delete from #tblCustomer where RequestNo = @tblReqNo This will:Get a [RequestNo], at random, from [tblCustomer].Get some columns from a row, at random, from [tblcustomer] into #tblCustomerIf that row HAPPENS to match the [RequestNo] stored earlier then delete it - there is NOT guarantee that these will all be the same [RequestNo] - 99.9999% of the time they will be, the other 0.0001% of the time you will have a bug that you cannot reproduce / find / fix.If you want an empty table then CREATE it. You can create an empty table by adding WHERE 1=0 to your SELECT ... INTO ... statement; but that is only suitable for Quick and Dirty usage, for a real process use a CREATE statement for the table - much more efficient etc etc. won't catch you out by creating a column with IDENTITY attribute when you aren't expecting it, and a host of other side effects.OK, now I read further I see another example of the same thing:Select top 1 RequestNumber, Purpose, RequestType, Location, RequestFor into #TblRequest from TblRequestTruncate table #TblRequest and an example of exactly what I was saying:Alter Table #tblCustomer Add CNT int Identity(1,1) not null just pre-CREATE the #TempTable and you can have that extra column at the get-go. Any you can make it Primary Key or add an Index if that will be useful in your process.This will be dog-slow for any appreciable data size:Update #tblCustomersetSalesLoc = (Select SalesLocation from RefSaleRoute where SalesRoute = @TempSaleRoute), SalesDist = (Select SalesDisCode from RefSaleRoute where SalesRoute = @TempSaleRoute),SalesOffice = (Select SalesOffCode from RefSaleRoute where SalesRoute = @TempSaleRoute), SalesGrp = (Select SalesGrpCode from RefSaleRoute where SalesRoute = @TempSaleRoute), SubDemArea = (Select SubDemandArea from RefSaleRoute where SalesRoute = @TempSaleRoute)WhereCNT = @CNT just JOIN the [SalesRoute] table instead and update all (relevant) rows in #tblCustomer in one statement, rather than using a cursor.There is so much that is "wrong" here that you need to get some help / tuition from someone who is more experienced to get you to the next level of "thinking" in a set-based manner so that you can see how to write set-based statements from the outset. |
|
|
klaine07
Starting Member
5 Posts |
Posted - 2011-12-26 : 07:20:08
|
thank you in your reply.. its a big help to me.. anymore ideas for my improvement |
|
|
klaine07
Starting Member
5 Posts |
Posted - 2011-12-28 : 11:26:04
|
Hi Kristen is this what you mean in the last query of your comment?Update #tblCustomersetSalesLoc = RefSaleRoute.SalesLocation SalesDist = RefSaleRoute.SalesDisCodeSalesOffice = RefSaleRoute.SalesOffCodeSalesGrp = RefSaleRoute.SalesGrpCodeSubDemArea =RefSaleRoute.SubDemandAreafrom #tblCustomer,RefSaleRouteWhere#tblCustomer.CNT = @CNT and RefSaleRoute.SalesRoute =@TempSaleRoutetell me if i'm wrong or right...thanks.. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-28 : 11:49:28
|
quote: Originally posted by klaine07 Hi Kristen is this what you mean in the last query of your comment?Update #tblCustomersetSalesLoc = RefSaleRoute.SalesLocation SalesDist = RefSaleRoute.SalesDisCodeSalesOffice = RefSaleRoute.SalesOffCodeSalesGrp = RefSaleRoute.SalesGrpCodeSubDemArea =RefSaleRoute.SubDemandAreafrom #tblCustomer,RefSaleRouteWhere#tblCustomer.CNT = @CNT and RefSaleRoute.SalesRoute =@TempSaleRoutetell me if i'm wrong or right...thanks..
the problem with above query is you're not relating tables in any way so it will end up in cross join (cartesian product) effectively.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
X002548
Not Just a Number
15586 Posts |
|
Sachin.Nand
2937 Posts |
Posted - 2011-12-28 : 13:13:57
|
I am out of here...PBUH |
|
|
klaine07
Starting Member
5 Posts |
Posted - 2011-12-28 : 16:00:29
|
nobody taught that one.... i just pattern it how does oracle does.. :) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-29 : 00:34:32
|
in any case, so doesnt your update need to be based on some relation between tables?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|