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
mainrotorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ChloesDadCommented:
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.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
aikimarkCommented:
Please try this.
Sub ProcFunc(dr as datarow)
   dim Cost as decimal
   dim dtThis as datatable
   dim dtProc as datatable = "select * from bigtable, itemdetail where bigtable.itemid = itemdetail.itemid and location = " & dr!location
   for each drSlowProc in dtProc.rows
       Cost = CalcLanded(dr!cost, drSlowProc!duty, dr!location)
       dtThis = "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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
plusone3055Commented:
the other thing you can do is Index the tables that you are calling to in SQL serer to Optimize the Query
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.