Link to home
Start Free TrialLog in
Avatar of mainrotor
mainrotor

asked on

I need help with a VB function

Hi Experts,
I have a report that takes too long to generate.  Below is the fuctions that generates the report.
How can I update my function to speed it?
Could I use a static variable to speed it up?
Should I use Temp tables?

Sub Main()
   dim dt as datatable = "select * from orders,items where orders.itemid = item.itemid and invdate between Jan and Dec"
   for each dr as datarow in dt.rows
      call ProcFunc(dr)
   next dr
end sub
 
Sub ProcFunc(dr as datarow)
   dim dtProc as datatable = "select * from bigtable, itemdetail where bigtable.itemid = itemdetail.itemid and location = " & dr!location
   for each drSlowProc in dtProc.rows
       dim Cost as decimal = CalcLanded(dr!cost, drSlowProc!duty, dr!location)
       dim dtThis as datatable = "select * from hugetable where location = " & dr!location & " and Cost between 10 and 20"
       call OtherFunc(Cost, drThis)
   next drSlowProc
end sub

Open in new window


Thank you in advance,
mrotor
Avatar of ChloesDad
ChloesDad
Flag of United Kingdom of Great Britain and Northern Ireland image

Any process that has nested searches is going to be slow. Can you show us what you are trying to generate as this will allow us to perhaps come up with a better solution.
Avatar of Qlemo
Using repetive SQL calls to get data is almost always a costly operation, because of the overhead involved for each SQL call.
So the first try is to make it a single SQL getting all data as required. And only the data required, that is you should restrict the columns to retrieve to allow for SQL, disk I/O, network I/O and memory optimization.

Also, in ProcFunc you are calling the SQL for hugetable in a loop though it does not have any changing parameters. You need to run that SQL only once per DR record, so move it infront of the FOR loop.
ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the other thing you can do is Index the tables that you are calling to in SQL serer to Optimize the Query