Salesforce Batch Apex Class - Querying Against Large Data Sets

I have a batch apex class where i'm building collections of websites and emails, so that i can use those collections to filter other other queries which will be made into collections. With all collections set, i want to run through a final loop of the scope to perform business processes.

Mockup:

for(Object o : scope)
{
listEmails.add(o.Email);
listWebsites.add(o.Websites);
}

Map<String, Account> accounts = Gather all accounts where website not in :listWebsties; //Website is key
List<String, Contact> contacts = Gather all contacts where email not in :listEmails; //Email is key

for(Object o : scope)
{
   Account = accounts.get(o.website);
   Contact = contacts.get(o.Email);

   Perform business logic here
}

The problem is when i run this batch it stays processing for hours. When working with a rather small database this works fine. But in working in a larger environment perhaps this is not the best solution.

Can anyone help me speed up the batch process with a more effective approach?

Answers


Is there anyway to post the entire batch apex class? Or help understand the data more? It looks like from your map that all of your accounts (in theory) have unique websites and all of your contacts have unique emails? I assume you build those maps by hand? That is you loop over the accounts and do a map.put(account.website,account)?

Do you have any system debug statements to confirm your map sizes? What happens if there is no account or no contact when you call accounts.get()?

And the business logic - is it more looping?

And are you using Batch variables in a static manner - i.e. you can have a counter to count the total number of records processed. If so, is your variable a list? that can be dangerous of course.

Also what object is your scope object? Not that it matters, but I'd think you'd want to have your scope be the Accounts themselves or the Contacts themselves.

I'd try adding system.debug statements to your batch to verify it's running and to see where the infinite loop may be occurring.


Need Your Help

Is it possible for XML to have valid schema but no XML document?

xsd dtd relaxng schematron

I get doubt that are there some schemas which have a valid schema but don't have some XML documents?

Check if two timestamps are the same day in Ruby

ruby date time

I'm a bit confused between Date, Datetime, and Time in Ruby. What's more, my application is sensitive to timezones, and I'm not sure how to convert between these three while being timezone-robust.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.