SlideShare a Scribd company logo
1 of 52
Download to read offline
Performance Tuning for Visualforce
and Apex
Stuart Bernstein
ISV Technical Evangelist
Thomas Harris
LMTS, CCE Production Support
John Tan
SMTS, CCE Technical Enablement
Safe Harbor
 Safe harbor statement under the Private Securities Litigation Reform Act of 1995:

 This presentation may contain forward-looking statements that involve risks, uncertainties, and assumptions. If any such uncertainties
 materialize or if any of the assumptions proves incorrect, the results of salesforce.com, inc. could differ materially from the results
 expressed or implied by the forward-looking statements we make. All statements other than statements of historical fact could be
 deemed forward-looking, including any projections of product or service availability, subscriber growth, earnings, revenues, or other
 financial items and any statements regarding strategies or plans of management for future operations, statements of belief, any
 statements concerning new, planned, or upgraded services or technology developments and customer contracts or use of our services.

 The risks and uncertainties referred to above include – but are not limited to – risks associated with developing and delivering new
 functionality for our service, new products and services, our new business model, our past operating losses, possible fluctuations in our
 operating results and rate of growth, interruptions or delays in our Web hosting, breach of our security measures, the outcome of
 intellectual property and other litigation, risks associated with possible mergers and acquisitions, the immature market in which we
 operate, our relatively limited operating history, our ability to expand, retain, and motivate our employees and manage our growth, new
 releases of our service and successful customer deployment, our limited history reselling non-salesforce.com products, and utilization
 and selling to larger enterprise customers. Further information on potential factors that could affect the financial results of
 salesforce.com, inc. is included in our annual report on Form 10-Q for the most recent fiscal quarter ended July 31, 2012. This
 documents and others containing important disclosures are available on the SEC Filings section of the Investor Information section of
 our Web site.

 Any unreleased services or features referenced in this or other presentations, press releases or public statements are not currently
 available and may not be delivered on time or at all. Customers who purchase our services should make the purchase decisions based
 upon features that are currently available. Salesforce.com, inc. assumes no obligation and does not intend to update these forward-
 looking statements.
Ask Questions on the Chatter Feed
Stuart Bernstein
ISV Technical Evangelist
Thomas Harris
LMTS, CCE Production Support
John Tan
SMTS, CCE Technical Enablement
Introduction

 This session is designed to help with common Visualforce/Apex
 performance issues targeted towards intermediate/advanced
 Force.com developers.
Agenda

  SOQL/SOSL
  Batch Apex
  Record Lock Contention
  Visualforce
  Debugging Tools
  Q&A
You Need to Build ON A Solid Foundation

  Follow SOQL Best practices
  Then tune Apex
  Then tune Visualforce
Indexes Are Vital for Well-performing SOQL

  Without an index the SOQL query may not perform well
  Standard indexes already exist on Record_Type_Id, Division,
   Created_Date, System_Modstamp (Last_Modified_Date),
   Name, Email (for Contacts and Leads), and the unique
   Salesforce record ID (Foreign Keys)
Administrators Can Create a Custom Index
Need Additional Indexes?
  Salesforce support can create single and two-column custom indexes
   on most fields (with the exception of multi-picklists, formula fields that
   reference other objects, and Long Text Area/Rich Text Area)
  Open a case
     -   Sample Query
     -   Bind Variables
     -   Org ID and user ID who would run the query
     -   Suggested index - OPTIONAL
  If they create an index, then SAVE and document where the index
   was created
Will an Index Be Used?

 • Whether an index is used is based on the expected number of
   records that will be returned.
 • A standard index will generally be used:
     • If the filter matches less than 30% of the total records, up to 1M records.
 • A custom index will generally be used:
     • If the filter matches less than 10% of the total records, up to 333K
       records.
 • OR conditions require that all columns be indexed
Use a Standard Index

Select name from account where Id=‘00530000003hQLJ’


Performs quickly because ID is an standard index
Use a Custom Index

Select name from account where
SLA_Serial_Number__c=‘1234567’


Performs quickly because SLA_Serial_Number__c is a custom
index
Use a Custom Index

Select name from account where SLA_Serial_Number__c=‘’


Doesn’t work because null prevents the index from being used.
Use a Custom Index

Select name from account where SLA_Serial_Number__c IN
:ListOfSerialNums


May work well
if no null variable in ListOfSerialNums
filter matches less than 10% of the total records up to 333,000 records
30% of the total records up to one million records if this was a standard index
(it’s not)
Don’t Use NOT IN

Select name from account where SLA_Status__C NOT IN (‘Valid’)


NOT IN also prevents the index from being used
One (best) Index Will Be Used

Select name from account where SLA_Status__C NOT IN
(‘Processed’) AND SLA_Serial_Number__c=‘1234567’


If one index is not usable, then the best remaining index will be
used.
One (best) Index Will Be Used

Select name from account where SLA_Status__C IN
(‘Processed’) AND SLA_Serial_Number__c=‘1234567’


If all indexes are usable, the best index will be used.
Cross-object Formula Fields Can Prevent Index
Usage
Contact.SLA_Formula__c is a formula field that copies the value
in Account.SLA_Serial_Number__c.
    Select name from contact where SLA_Formula__c IN
     :ListOfSerialNums
    A custom index on Account.SLA_Serial_Number__c cannot be used
     in this query.
Use this query instead, where the index can be used:
    Select name from contact where Account.SLA_Serial_Number__c IN
     :ListOfSerialNums
Denormalizing Data Increases Performance

Select name from contact where Account.SLA_Serial_Number__c
IN :ListOfSerialNums


Solution
Copy SLA_Serial_Number to a field in Contact (don’t use a
formula) and make the field an External Id
OR query for the account IDs with the SLA Serial number first
Query Only The Necessary Fields
SELECT
AccountNumber,AccountSource,Active__c,AnnualRevenue,BillingCity,BillingCountry,BillingPo
stalCode,BillingState,BillingStreet,CreatedById,CreatedDate,CustomerPriority__c,Description,
Fax,Id,Industry,IsDeleted,Jigsaw,JigsawCompanyId,LastActivityDate,LastModifiedById,LastM
odifiedDate,MasterRecordId,Name,NumberOfEmployees,NumberofLocations__c,OwnerId,O
wnership,ParentId,Phone,Rating,ShippingCity,ShippingCountry,ShippingPostalCode,Shipping
State,ShippingStreet,Sic,SicDesc,Site,SLAExpirationDate__c,SLA_Serial_Number__c,SLA__
c,SystemModstamp,TickerSymbol,Type,UpsellOpportunity__c,Website FROM Account
SOSL Performance Best Practices

  SOSL queries for the data using a different technique but then
   takes the data and adds the where clause using SOQL
    - Eg FIND {foo} RETURNING Account (Id WHERE City='Rome') is
      analogous to SELECT Id FROM Account WHERE ID IN (<list of Ids
      from search>) AND City='Rome’
  Follow SOQL best practices for the where clause
Batch Apex

 Most common issue is performance of SOQL used in the start()
  method.
 Avoid the use of non-selective filters in the start() method.
   - Queries will perform significantly worse as more data is added.

 Don’t add a non-selective filter to filter out a few rows relative to
  the overall size of the result set. Instead, use Apex to filter out
  rows in the execute() method.
Batch Apex – Start Method Queries

 Suppose you have 5M records in Account. You want to process the
  80% of those records represented by this query:
   - SELECT Id FROM Account WHERE Rating__c IN ('Excellent','Good','Moderate')

 Since there is no way to use a selective filter in a query for these
  records, this query will not run quickly even if Rating__c is indexed.
 As an alternative, use this query in the start method:
   - SELECT Id FROM Account

 Filter out the unwanted records in the execute method.
Batch Apex – Start Method Queries – Sample Class
   global Database.QueryLocator<SObject> start(Database.BatchableContext bc) {
       return Database.getQueryLocator('SELECT Id FROM Account');
   }


   global void execute(Database.BatchableContext bc, List<Account> scope) {
       List<Account> actualScope = [SELECT Id, Name, Description FROM Account
                                    WHERE Rating__c IN
                                        ('Excellent','Good','Moderate’)
                                    AND Id IN :scope];
       for(Account acc : actualScope) { /* do something */ }
   }
Batch Apex – Avoid Errors in Bulk DML
 global void execute(Database.BatchableContext bc, List<Account>
 scope) {
     for(Account acc : scope) { /* do something */ }
    Database.SaveResult[] results = Database.update(scope,
 false);
     /* look at results */
 }

  If an error occurs in this save operation, Salesforce will try to save it one
   record at a time.
      This performs significantly slower than a bulk save without errors.
Parent-Child Locking
  Insert of Contact requires locking the parent Account.
  Insert or Update of Event requires locking both the parent Contact and the
   parent Account.
  Insert or Update of Task requires locking both the parent Contact and
   parent Account, if the Task is marked as complete.
  Insert of Case requires locking the parent Contact and parent Account.
  In objects that are part of a master/detail relationship, updating a detail
   record requires locking the parent if roll-up summary fields exist.
Sharing Calculations

  “Implicit” Sharing
    - Record access built into Salesforce applications
  Parent Implicit Sharing (Account only, private sharing model)
    - Access to Contact/Opportunity/Case = Read access to parent
      account
    - not used when sharing on the child is controlled by parent
    - when user loses access to a child, need to check all other children
      to see if we can delete the parent implicit share
Should we delete Jane’s parent implicit share?
 Sharing calculations                                    Universal
                                                          Containers
  impacted by Data Skew
 300K unassigned contacts                    X
  under account
 Private sharing model           ?
 Jane has access to contact
  Bob Smith
                                      Bob
                                      Smith
                                                  Fred
                                                  Green
                                                                Betty
                                                                Brown   …   300K
                                                                            Contact

 Jane losses access to contact
 Should we delete Jane’s
  parent implicit share for       X
  Universal Containers?
 Can’t code around Data Skew
Effects of data skew

  Sharing calculations run longer
  Concurrent Request Limit error
    - Synchronous Apex request starts counting against the limit waiting >
      5 sec for lock.
  UNABLE_TO_LOCK_ROW error
    - Timed out waiting 10 seconds to acquire lock.
    - Even if no errors occur, waiting for locks can significantly slow DML
      operations.
Recommendations

  Consider a public read/write sharing model
    - Parent will still be locked. No sharing calculations.
  10K children or less
  Prevent parent-child data skews
    - Detect whether existing accounts already have "reached their limit"
      of child records, and create new accounts.
Visualforce – Best Practices

  http://developer.force.com/dreamforce/11/session/Blazing-
   Fast-Visualforce-Pages
  View State
    - Filter and Paginate
    - Transient keyword
    - Simplify component hierarchy
    - JavaScript Remoting
Analyzing Performance

  Measure performance in the browser first to see if the
   problem is client-side or server-side.
    - Chrome Developer Tools and the Firebug plugin for Firefox will both
      work for this.
  After confirming the problem is on the server, use the
   Salesforce Developer Console to find areas you can tune.
Analyzing Performance
 Example Visualforce Page: DebugPage
Analyzing Performance
    In Chrome, press F12 to open Developer Tools.
    Use the Network tab to measure performance.
         It takes 8.74 seconds to get a response from the server when pressing the “Do BOTH Slow Query and
          Callout” button.
Analyzing Performance – Developer Console
  Use the Developer Console to analyze server-side performance.
  Apex debug logs will be generated for every server request you perform
   while the window is open.
  Several tools are available in the Developer Console to find performance
   hotspots.
Analyzing Performance – Developer Console
1) Open the log for
the slow request.
2) Open the Timeline tab.
3) Identify the slow
portion and click on the
bar. This loads the
relevant log lines into
the Execution Log.
4) Click on a line in
the Execution Log to
show the stack trace
for the slow query on
the left.
Analyzing Performance – Log Levels
  The default log levels generate a huge amount of output, most of which
   is unnecessary for debugging performance issues.
      This also makes the console slower.
  Try lowering the log levels so you can look for slow queries and callouts
   first.
Analyzing Performance – Heap Dumps

  The Developer Console can also be used to create Apex
   heaps that help in debugging.
  For example, you can use it look at the bind variables for a
   query.
Analyzing Performance – Heap Dumps
Analyzing Performance – Heap Dumps

 The bind variables will
  not show up in the
  debug console and may
  not be easy to find.
 You can set a heap
  dump breakpoint to get
  the variable values at
  the time the query is
  run.
Analyzing Performance – Heap Dumps




Set a breakpoint
on the line where
the query is run.
Analyzing Performance – Heap Dumps

 The runtime values
  of the variables can
  be found in the
  Symbols tab of the
  heap dump viewer.
Analyzing Performance – Useful Tools
  Developer Console
  Firebug (http://getfirebug.com)
     -   Web development plugin for Firefox.
  Chrome Developer Tools
     -   Included with Chrome.
  Workbench (https://workbench.developerforce.com)
     -   Good for testing SOQL queries.
  Fiddler (http://www.fiddler2.com/fiddler2)
     -   Windows tool for debugging HTTP traffic, integrates well with IE and Firefox.
Working with Support

  SOQL performance
    - Open a case with org Id, user Id, SOQL and bind variables.
    - Can add custom single and two column indexes.
  Record locks
    - Provide support the org ID and time frame where the issues
      occurred.
    - Provide the IDs of affected records if possible.
Useful Links

 Architect Core Resources
   http://wiki.developerforce.com/page/Architect_Core_Resources
     - Guide to Sharing Architecture
     - Record Access: Under the Hood
     - Best Practices for Deployments with Large Data Volumes
     - Asynchronous Processing in Force.com
     - VF Performance
Stuart Bernstein           Thomas Harris            John Tan
ISV Technical Evangelist   LMTS, CCE Production   SMTS, CCE Technical
                                 Support             Enablement
Performance Tuning for Visualforce and Apex

More Related Content

What's hot

006 isobaric process
006 isobaric process006 isobaric process
006 isobaric processphysics101
 
ETAP - Power system modeling
ETAP - Power system modelingETAP - Power system modeling
ETAP - Power system modelingHimmelstern
 
Carbon Brushes Technical Guide
Carbon Brushes Technical GuideCarbon Brushes Technical Guide
Carbon Brushes Technical GuideBari Dominguez
 
ETAP - device coordination
ETAP - device coordinationETAP - device coordination
ETAP - device coordinationHimmelstern
 
[Juan Martinez] Transient Analysis of Power Systems.pdf
[Juan Martinez] Transient Analysis of Power Systems.pdf[Juan Martinez] Transient Analysis of Power Systems.pdf
[Juan Martinez] Transient Analysis of Power Systems.pdfTIRSOROJAS4
 
Breaker_Failure_Protection.ppt
Breaker_Failure_Protection.pptBreaker_Failure_Protection.ppt
Breaker_Failure_Protection.pptThien Phan Bản
 
ETAP - Short circuit ansi standard
ETAP - Short circuit ansi standardETAP - Short circuit ansi standard
ETAP - Short circuit ansi standardHimmelstern
 
Short Circuit, Protective Device Coordination
Short Circuit, Protective Device CoordinationShort Circuit, Protective Device Coordination
Short Circuit, Protective Device Coordinationmichaeljmack
 
Customization of LES turbulence model in OpenFOAM
Customization of LES turbulence	 model in OpenFOAMCustomization of LES turbulence	 model in OpenFOAM
Customization of LES turbulence model in OpenFOAMmmer547
 
Module 5, Spring 2020.pdf
Module 5, Spring 2020.pdfModule 5, Spring 2020.pdf
Module 5, Spring 2020.pdfMohammad Javed
 
Catalogo elettromeccanica- piossasco
Catalogo elettromeccanica- piossascoCatalogo elettromeccanica- piossasco
Catalogo elettromeccanica- piossascoFilippo Pusset
 
Thermodynamic solutions
Thermodynamic solutionsThermodynamic solutions
Thermodynamic solutionsnac94
 
Strength of Materials
Strength of MaterialsStrength of Materials
Strength of MaterialsAditya .
 
183710439 friction-from-meriam-pdf
183710439 friction-from-meriam-pdf183710439 friction-from-meriam-pdf
183710439 friction-from-meriam-pdfJasim Almuhandis
 
ETAP - Transient stability 2
ETAP - Transient stability 2ETAP - Transient stability 2
ETAP - Transient stability 2Himmelstern
 
Vapour Permeable Air Barriers: Real World Evaluation - What Works, What Doesn...
Vapour Permeable Air Barriers: Real World Evaluation - What Works, What Doesn...Vapour Permeable Air Barriers: Real World Evaluation - What Works, What Doesn...
Vapour Permeable Air Barriers: Real World Evaluation - What Works, What Doesn...Lorne Ricketts
 

What's hot (20)

Multi degree of freedom systems
Multi degree of freedom systemsMulti degree of freedom systems
Multi degree of freedom systems
 
006 isobaric process
006 isobaric process006 isobaric process
006 isobaric process
 
ETAP - Power system modeling
ETAP - Power system modelingETAP - Power system modeling
ETAP - Power system modeling
 
Carbon Brushes Technical Guide
Carbon Brushes Technical GuideCarbon Brushes Technical Guide
Carbon Brushes Technical Guide
 
ETAP - device coordination
ETAP - device coordinationETAP - device coordination
ETAP - device coordination
 
[Juan Martinez] Transient Analysis of Power Systems.pdf
[Juan Martinez] Transient Analysis of Power Systems.pdf[Juan Martinez] Transient Analysis of Power Systems.pdf
[Juan Martinez] Transient Analysis of Power Systems.pdf
 
Ch03
Ch03Ch03
Ch03
 
Breaker_Failure_Protection.ppt
Breaker_Failure_Protection.pptBreaker_Failure_Protection.ppt
Breaker_Failure_Protection.ppt
 
ETAP - Short circuit ansi standard
ETAP - Short circuit ansi standardETAP - Short circuit ansi standard
ETAP - Short circuit ansi standard
 
Short Circuit, Protective Device Coordination
Short Circuit, Protective Device CoordinationShort Circuit, Protective Device Coordination
Short Circuit, Protective Device Coordination
 
Basic openfoa mtutorialsguide
Basic openfoa mtutorialsguideBasic openfoa mtutorialsguide
Basic openfoa mtutorialsguide
 
It 05104 digsig_1
It 05104 digsig_1It 05104 digsig_1
It 05104 digsig_1
 
Customization of LES turbulence model in OpenFOAM
Customization of LES turbulence	 model in OpenFOAMCustomization of LES turbulence	 model in OpenFOAM
Customization of LES turbulence model in OpenFOAM
 
Module 5, Spring 2020.pdf
Module 5, Spring 2020.pdfModule 5, Spring 2020.pdf
Module 5, Spring 2020.pdf
 
Catalogo elettromeccanica- piossasco
Catalogo elettromeccanica- piossascoCatalogo elettromeccanica- piossasco
Catalogo elettromeccanica- piossasco
 
Thermodynamic solutions
Thermodynamic solutionsThermodynamic solutions
Thermodynamic solutions
 
Strength of Materials
Strength of MaterialsStrength of Materials
Strength of Materials
 
183710439 friction-from-meriam-pdf
183710439 friction-from-meriam-pdf183710439 friction-from-meriam-pdf
183710439 friction-from-meriam-pdf
 
ETAP - Transient stability 2
ETAP - Transient stability 2ETAP - Transient stability 2
ETAP - Transient stability 2
 
Vapour Permeable Air Barriers: Real World Evaluation - What Works, What Doesn...
Vapour Permeable Air Barriers: Real World Evaluation - What Works, What Doesn...Vapour Permeable Air Barriers: Real World Evaluation - What Works, What Doesn...
Vapour Permeable Air Barriers: Real World Evaluation - What Works, What Doesn...
 

Similar to Performance Tuning for Visualforce and Apex

Performance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and ApexPerformance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and ApexSalesforce Developers
 
Apex Algorithms: Tips and Tricks (Dreamforce 2014)
Apex Algorithms: Tips and Tricks (Dreamforce 2014)Apex Algorithms: Tips and Tricks (Dreamforce 2014)
Apex Algorithms: Tips and Tricks (Dreamforce 2014)Mary Scotton
 
A G S006 Little 091807
A G S006  Little 091807A G S006  Little 091807
A G S006 Little 091807Dreamforce07
 
Mbf2 salesforce webinar 2
Mbf2 salesforce webinar 2Mbf2 salesforce webinar 2
Mbf2 salesforce webinar 2BeMyApp
 
Get ready for your platform developer i certification webinar
Get ready for your platform developer i certification   webinarGet ready for your platform developer i certification   webinar
Get ready for your platform developer i certification webinarJackGuo20
 
Programming Building Blocks for Admins
Programming Building Blocks for Admins Programming Building Blocks for Admins
Programming Building Blocks for Admins Salesforce Admins
 
The Need for Speed: Building Reports That Fly
The Need for Speed: Building Reports That FlyThe Need for Speed: Building Reports That Fly
The Need for Speed: Building Reports That FlySalesforce Developers
 
Aan009 Contreras 091907
Aan009 Contreras 091907Aan009 Contreras 091907
Aan009 Contreras 091907Dreamforce07
 
Improving Enterprise Findability: Presented by Jayesh Govindarajan, Salesforce
Improving Enterprise Findability: Presented by Jayesh Govindarajan, SalesforceImproving Enterprise Findability: Presented by Jayesh Govindarajan, Salesforce
Improving Enterprise Findability: Presented by Jayesh Govindarajan, SalesforceLucidworks
 
Understanding the Salesforce Architecture: How We Do the Magic We Do
Understanding the Salesforce Architecture: How We Do the Magic We DoUnderstanding the Salesforce Architecture: How We Do the Magic We Do
Understanding the Salesforce Architecture: How We Do the Magic We DoSalesforce Developers
 
Solving Complex Data Load Challenges
Solving Complex Data Load ChallengesSolving Complex Data Load Challenges
Solving Complex Data Load ChallengesSunand P
 
Salesforce Winter '23 Release Highlights.pptx
Salesforce Winter '23 Release Highlights.pptxSalesforce Winter '23 Release Highlights.pptx
Salesforce Winter '23 Release Highlights.pptxOm Prakash
 
OData: A Standard API for Data Access
OData: A Standard API for Data AccessOData: A Standard API for Data Access
OData: A Standard API for Data AccessPat Patterson
 
Data quality and bi
Data quality and biData quality and bi
Data quality and bijeffd00
 
Intro to AppExchange - Building Composite Apps
Intro to AppExchange - Building Composite AppsIntro to AppExchange - Building Composite Apps
Intro to AppExchange - Building Composite Appsdreamforce2006
 
Use Custom Metadata Types for Easy ALM & Compliance for Your Custom Apps
Use Custom Metadata Types for Easy ALM & Compliance for Your Custom AppsUse Custom Metadata Types for Easy ALM & Compliance for Your Custom Apps
Use Custom Metadata Types for Easy ALM & Compliance for Your Custom AppsSalesforce Developers
 
Salesforce Spring'15 release overview
Salesforce Spring'15 release overviewSalesforce Spring'15 release overview
Salesforce Spring'15 release overviewRakesh Gupta
 
S-Controls for Dummies
S-Controls for DummiesS-Controls for Dummies
S-Controls for Dummiesdreamforce2006
 

Similar to Performance Tuning for Visualforce and Apex (20)

Df12 Performance Tuning
Df12 Performance TuningDf12 Performance Tuning
Df12 Performance Tuning
 
Performance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and ApexPerformance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and Apex
 
Apex Algorithms: Tips and Tricks (Dreamforce 2014)
Apex Algorithms: Tips and Tricks (Dreamforce 2014)Apex Algorithms: Tips and Tricks (Dreamforce 2014)
Apex Algorithms: Tips and Tricks (Dreamforce 2014)
 
A G S006 Little 091807
A G S006  Little 091807A G S006  Little 091807
A G S006 Little 091807
 
Mbf2 salesforce webinar 2
Mbf2 salesforce webinar 2Mbf2 salesforce webinar 2
Mbf2 salesforce webinar 2
 
Get ready for your platform developer i certification webinar
Get ready for your platform developer i certification   webinarGet ready for your platform developer i certification   webinar
Get ready for your platform developer i certification webinar
 
Programming Building Blocks for Admins
Programming Building Blocks for Admins Programming Building Blocks for Admins
Programming Building Blocks for Admins
 
The Need for Speed: Building Reports That Fly
The Need for Speed: Building Reports That FlyThe Need for Speed: Building Reports That Fly
The Need for Speed: Building Reports That Fly
 
Building Reports That Fly
Building Reports That FlyBuilding Reports That Fly
Building Reports That Fly
 
Aan009 Contreras 091907
Aan009 Contreras 091907Aan009 Contreras 091907
Aan009 Contreras 091907
 
Improving Enterprise Findability: Presented by Jayesh Govindarajan, Salesforce
Improving Enterprise Findability: Presented by Jayesh Govindarajan, SalesforceImproving Enterprise Findability: Presented by Jayesh Govindarajan, Salesforce
Improving Enterprise Findability: Presented by Jayesh Govindarajan, Salesforce
 
Understanding the Salesforce Architecture: How We Do the Magic We Do
Understanding the Salesforce Architecture: How We Do the Magic We DoUnderstanding the Salesforce Architecture: How We Do the Magic We Do
Understanding the Salesforce Architecture: How We Do the Magic We Do
 
Solving Complex Data Load Challenges
Solving Complex Data Load ChallengesSolving Complex Data Load Challenges
Solving Complex Data Load Challenges
 
Salesforce Winter '23 Release Highlights.pptx
Salesforce Winter '23 Release Highlights.pptxSalesforce Winter '23 Release Highlights.pptx
Salesforce Winter '23 Release Highlights.pptx
 
OData: A Standard API for Data Access
OData: A Standard API for Data AccessOData: A Standard API for Data Access
OData: A Standard API for Data Access
 
Data quality and bi
Data quality and biData quality and bi
Data quality and bi
 
Intro to AppExchange - Building Composite Apps
Intro to AppExchange - Building Composite AppsIntro to AppExchange - Building Composite Apps
Intro to AppExchange - Building Composite Apps
 
Use Custom Metadata Types for Easy ALM & Compliance for Your Custom Apps
Use Custom Metadata Types for Easy ALM & Compliance for Your Custom AppsUse Custom Metadata Types for Easy ALM & Compliance for Your Custom Apps
Use Custom Metadata Types for Easy ALM & Compliance for Your Custom Apps
 
Salesforce Spring'15 release overview
Salesforce Spring'15 release overviewSalesforce Spring'15 release overview
Salesforce Spring'15 release overview
 
S-Controls for Dummies
S-Controls for DummiesS-Controls for Dummies
S-Controls for Dummies
 

More from Salesforce Developers

Sample Gallery: Reference Code and Best Practices for Salesforce Developers
Sample Gallery: Reference Code and Best Practices for Salesforce DevelopersSample Gallery: Reference Code and Best Practices for Salesforce Developers
Sample Gallery: Reference Code and Best Practices for Salesforce DevelopersSalesforce Developers
 
Maximizing Salesforce Lightning Experience and Lightning Component Performance
Maximizing Salesforce Lightning Experience and Lightning Component PerformanceMaximizing Salesforce Lightning Experience and Lightning Component Performance
Maximizing Salesforce Lightning Experience and Lightning Component PerformanceSalesforce Developers
 
Local development with Open Source Base Components
Local development with Open Source Base ComponentsLocal development with Open Source Base Components
Local development with Open Source Base ComponentsSalesforce Developers
 
TrailheaDX India : Developer Highlights
TrailheaDX India : Developer HighlightsTrailheaDX India : Developer Highlights
TrailheaDX India : Developer HighlightsSalesforce Developers
 
Why developers shouldn’t miss TrailheaDX India
Why developers shouldn’t miss TrailheaDX IndiaWhy developers shouldn’t miss TrailheaDX India
Why developers shouldn’t miss TrailheaDX IndiaSalesforce Developers
 
CodeLive: Build Lightning Web Components faster with Local Development
CodeLive: Build Lightning Web Components faster with Local DevelopmentCodeLive: Build Lightning Web Components faster with Local Development
CodeLive: Build Lightning Web Components faster with Local DevelopmentSalesforce Developers
 
CodeLive: Converting Aura Components to Lightning Web Components
CodeLive: Converting Aura Components to Lightning Web ComponentsCodeLive: Converting Aura Components to Lightning Web Components
CodeLive: Converting Aura Components to Lightning Web ComponentsSalesforce Developers
 
Enterprise-grade UI with open source Lightning Web Components
Enterprise-grade UI with open source Lightning Web ComponentsEnterprise-grade UI with open source Lightning Web Components
Enterprise-grade UI with open source Lightning Web ComponentsSalesforce Developers
 
TrailheaDX and Summer '19: Developer Highlights
TrailheaDX and Summer '19: Developer HighlightsTrailheaDX and Summer '19: Developer Highlights
TrailheaDX and Summer '19: Developer HighlightsSalesforce Developers
 
Lightning web components - Episode 4 : Security and Testing
Lightning web components  - Episode 4 : Security and TestingLightning web components  - Episode 4 : Security and Testing
Lightning web components - Episode 4 : Security and TestingSalesforce Developers
 
LWC Episode 3- Component Communication and Aura Interoperability
LWC Episode 3- Component Communication and Aura InteroperabilityLWC Episode 3- Component Communication and Aura Interoperability
LWC Episode 3- Component Communication and Aura InteroperabilitySalesforce Developers
 
Lightning web components episode 2- work with salesforce data
Lightning web components   episode 2- work with salesforce dataLightning web components   episode 2- work with salesforce data
Lightning web components episode 2- work with salesforce dataSalesforce Developers
 
Lightning web components - Episode 1 - An Introduction
Lightning web components - Episode 1 - An IntroductionLightning web components - Episode 1 - An Introduction
Lightning web components - Episode 1 - An IntroductionSalesforce Developers
 
Migrating CPQ to Advanced Calculator and JSQCP
Migrating CPQ to Advanced Calculator and JSQCPMigrating CPQ to Advanced Calculator and JSQCP
Migrating CPQ to Advanced Calculator and JSQCPSalesforce Developers
 
Scale with Large Data Volumes and Big Objects in Salesforce
Scale with Large Data Volumes and Big Objects in SalesforceScale with Large Data Volumes and Big Objects in Salesforce
Scale with Large Data Volumes and Big Objects in SalesforceSalesforce Developers
 
Replicate Salesforce Data in Real Time with Change Data Capture
Replicate Salesforce Data in Real Time with Change Data CaptureReplicate Salesforce Data in Real Time with Change Data Capture
Replicate Salesforce Data in Real Time with Change Data CaptureSalesforce Developers
 
Modern Development with Salesforce DX
Modern Development with Salesforce DXModern Development with Salesforce DX
Modern Development with Salesforce DXSalesforce Developers
 
Integrate CMS Content Into Lightning Communities with CMS Connect
Integrate CMS Content Into Lightning Communities with CMS ConnectIntegrate CMS Content Into Lightning Communities with CMS Connect
Integrate CMS Content Into Lightning Communities with CMS ConnectSalesforce Developers
 

More from Salesforce Developers (20)

Sample Gallery: Reference Code and Best Practices for Salesforce Developers
Sample Gallery: Reference Code and Best Practices for Salesforce DevelopersSample Gallery: Reference Code and Best Practices for Salesforce Developers
Sample Gallery: Reference Code and Best Practices for Salesforce Developers
 
Maximizing Salesforce Lightning Experience and Lightning Component Performance
Maximizing Salesforce Lightning Experience and Lightning Component PerformanceMaximizing Salesforce Lightning Experience and Lightning Component Performance
Maximizing Salesforce Lightning Experience and Lightning Component Performance
 
Local development with Open Source Base Components
Local development with Open Source Base ComponentsLocal development with Open Source Base Components
Local development with Open Source Base Components
 
TrailheaDX India : Developer Highlights
TrailheaDX India : Developer HighlightsTrailheaDX India : Developer Highlights
TrailheaDX India : Developer Highlights
 
Why developers shouldn’t miss TrailheaDX India
Why developers shouldn’t miss TrailheaDX IndiaWhy developers shouldn’t miss TrailheaDX India
Why developers shouldn’t miss TrailheaDX India
 
CodeLive: Build Lightning Web Components faster with Local Development
CodeLive: Build Lightning Web Components faster with Local DevelopmentCodeLive: Build Lightning Web Components faster with Local Development
CodeLive: Build Lightning Web Components faster with Local Development
 
CodeLive: Converting Aura Components to Lightning Web Components
CodeLive: Converting Aura Components to Lightning Web ComponentsCodeLive: Converting Aura Components to Lightning Web Components
CodeLive: Converting Aura Components to Lightning Web Components
 
Enterprise-grade UI with open source Lightning Web Components
Enterprise-grade UI with open source Lightning Web ComponentsEnterprise-grade UI with open source Lightning Web Components
Enterprise-grade UI with open source Lightning Web Components
 
TrailheaDX and Summer '19: Developer Highlights
TrailheaDX and Summer '19: Developer HighlightsTrailheaDX and Summer '19: Developer Highlights
TrailheaDX and Summer '19: Developer Highlights
 
Live coding with LWC
Live coding with LWCLive coding with LWC
Live coding with LWC
 
Lightning web components - Episode 4 : Security and Testing
Lightning web components  - Episode 4 : Security and TestingLightning web components  - Episode 4 : Security and Testing
Lightning web components - Episode 4 : Security and Testing
 
LWC Episode 3- Component Communication and Aura Interoperability
LWC Episode 3- Component Communication and Aura InteroperabilityLWC Episode 3- Component Communication and Aura Interoperability
LWC Episode 3- Component Communication and Aura Interoperability
 
Lightning web components episode 2- work with salesforce data
Lightning web components   episode 2- work with salesforce dataLightning web components   episode 2- work with salesforce data
Lightning web components episode 2- work with salesforce data
 
Lightning web components - Episode 1 - An Introduction
Lightning web components - Episode 1 - An IntroductionLightning web components - Episode 1 - An Introduction
Lightning web components - Episode 1 - An Introduction
 
Migrating CPQ to Advanced Calculator and JSQCP
Migrating CPQ to Advanced Calculator and JSQCPMigrating CPQ to Advanced Calculator and JSQCP
Migrating CPQ to Advanced Calculator and JSQCP
 
Scale with Large Data Volumes and Big Objects in Salesforce
Scale with Large Data Volumes and Big Objects in SalesforceScale with Large Data Volumes and Big Objects in Salesforce
Scale with Large Data Volumes and Big Objects in Salesforce
 
Replicate Salesforce Data in Real Time with Change Data Capture
Replicate Salesforce Data in Real Time with Change Data CaptureReplicate Salesforce Data in Real Time with Change Data Capture
Replicate Salesforce Data in Real Time with Change Data Capture
 
Modern Development with Salesforce DX
Modern Development with Salesforce DXModern Development with Salesforce DX
Modern Development with Salesforce DX
 
Get Into Lightning Flow Development
Get Into Lightning Flow DevelopmentGet Into Lightning Flow Development
Get Into Lightning Flow Development
 
Integrate CMS Content Into Lightning Communities with CMS Connect
Integrate CMS Content Into Lightning Communities with CMS ConnectIntegrate CMS Content Into Lightning Communities with CMS Connect
Integrate CMS Content Into Lightning Communities with CMS Connect
 

Performance Tuning for Visualforce and Apex

  • 1. Performance Tuning for Visualforce and Apex Stuart Bernstein ISV Technical Evangelist Thomas Harris LMTS, CCE Production Support John Tan SMTS, CCE Technical Enablement
  • 2. Safe Harbor Safe harbor statement under the Private Securities Litigation Reform Act of 1995: This presentation may contain forward-looking statements that involve risks, uncertainties, and assumptions. If any such uncertainties materialize or if any of the assumptions proves incorrect, the results of salesforce.com, inc. could differ materially from the results expressed or implied by the forward-looking statements we make. All statements other than statements of historical fact could be deemed forward-looking, including any projections of product or service availability, subscriber growth, earnings, revenues, or other financial items and any statements regarding strategies or plans of management for future operations, statements of belief, any statements concerning new, planned, or upgraded services or technology developments and customer contracts or use of our services. The risks and uncertainties referred to above include – but are not limited to – risks associated with developing and delivering new functionality for our service, new products and services, our new business model, our past operating losses, possible fluctuations in our operating results and rate of growth, interruptions or delays in our Web hosting, breach of our security measures, the outcome of intellectual property and other litigation, risks associated with possible mergers and acquisitions, the immature market in which we operate, our relatively limited operating history, our ability to expand, retain, and motivate our employees and manage our growth, new releases of our service and successful customer deployment, our limited history reselling non-salesforce.com products, and utilization and selling to larger enterprise customers. Further information on potential factors that could affect the financial results of salesforce.com, inc. is included in our annual report on Form 10-Q for the most recent fiscal quarter ended July 31, 2012. This documents and others containing important disclosures are available on the SEC Filings section of the Investor Information section of our Web site. Any unreleased services or features referenced in this or other presentations, press releases or public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make the purchase decisions based upon features that are currently available. Salesforce.com, inc. assumes no obligation and does not intend to update these forward- looking statements.
  • 3. Ask Questions on the Chatter Feed
  • 4. Stuart Bernstein ISV Technical Evangelist Thomas Harris LMTS, CCE Production Support John Tan SMTS, CCE Technical Enablement
  • 5. Introduction This session is designed to help with common Visualforce/Apex performance issues targeted towards intermediate/advanced Force.com developers.
  • 6. Agenda  SOQL/SOSL  Batch Apex  Record Lock Contention  Visualforce  Debugging Tools  Q&A
  • 7. You Need to Build ON A Solid Foundation  Follow SOQL Best practices  Then tune Apex  Then tune Visualforce
  • 8. Indexes Are Vital for Well-performing SOQL  Without an index the SOQL query may not perform well  Standard indexes already exist on Record_Type_Id, Division, Created_Date, System_Modstamp (Last_Modified_Date), Name, Email (for Contacts and Leads), and the unique Salesforce record ID (Foreign Keys)
  • 9. Administrators Can Create a Custom Index
  • 10. Need Additional Indexes?  Salesforce support can create single and two-column custom indexes on most fields (with the exception of multi-picklists, formula fields that reference other objects, and Long Text Area/Rich Text Area)  Open a case - Sample Query - Bind Variables - Org ID and user ID who would run the query - Suggested index - OPTIONAL  If they create an index, then SAVE and document where the index was created
  • 11. Will an Index Be Used? • Whether an index is used is based on the expected number of records that will be returned. • A standard index will generally be used: • If the filter matches less than 30% of the total records, up to 1M records. • A custom index will generally be used: • If the filter matches less than 10% of the total records, up to 333K records. • OR conditions require that all columns be indexed
  • 12. Use a Standard Index Select name from account where Id=‘00530000003hQLJ’ Performs quickly because ID is an standard index
  • 13. Use a Custom Index Select name from account where SLA_Serial_Number__c=‘1234567’ Performs quickly because SLA_Serial_Number__c is a custom index
  • 14. Use a Custom Index Select name from account where SLA_Serial_Number__c=‘’ Doesn’t work because null prevents the index from being used.
  • 15. Use a Custom Index Select name from account where SLA_Serial_Number__c IN :ListOfSerialNums May work well if no null variable in ListOfSerialNums filter matches less than 10% of the total records up to 333,000 records 30% of the total records up to one million records if this was a standard index (it’s not)
  • 16. Don’t Use NOT IN Select name from account where SLA_Status__C NOT IN (‘Valid’) NOT IN also prevents the index from being used
  • 17. One (best) Index Will Be Used Select name from account where SLA_Status__C NOT IN (‘Processed’) AND SLA_Serial_Number__c=‘1234567’ If one index is not usable, then the best remaining index will be used.
  • 18. One (best) Index Will Be Used Select name from account where SLA_Status__C IN (‘Processed’) AND SLA_Serial_Number__c=‘1234567’ If all indexes are usable, the best index will be used.
  • 19. Cross-object Formula Fields Can Prevent Index Usage Contact.SLA_Formula__c is a formula field that copies the value in Account.SLA_Serial_Number__c.  Select name from contact where SLA_Formula__c IN :ListOfSerialNums  A custom index on Account.SLA_Serial_Number__c cannot be used in this query. Use this query instead, where the index can be used:  Select name from contact where Account.SLA_Serial_Number__c IN :ListOfSerialNums
  • 20. Denormalizing Data Increases Performance Select name from contact where Account.SLA_Serial_Number__c IN :ListOfSerialNums Solution Copy SLA_Serial_Number to a field in Contact (don’t use a formula) and make the field an External Id OR query for the account IDs with the SLA Serial number first
  • 21. Query Only The Necessary Fields SELECT AccountNumber,AccountSource,Active__c,AnnualRevenue,BillingCity,BillingCountry,BillingPo stalCode,BillingState,BillingStreet,CreatedById,CreatedDate,CustomerPriority__c,Description, Fax,Id,Industry,IsDeleted,Jigsaw,JigsawCompanyId,LastActivityDate,LastModifiedById,LastM odifiedDate,MasterRecordId,Name,NumberOfEmployees,NumberofLocations__c,OwnerId,O wnership,ParentId,Phone,Rating,ShippingCity,ShippingCountry,ShippingPostalCode,Shipping State,ShippingStreet,Sic,SicDesc,Site,SLAExpirationDate__c,SLA_Serial_Number__c,SLA__ c,SystemModstamp,TickerSymbol,Type,UpsellOpportunity__c,Website FROM Account
  • 22. SOSL Performance Best Practices  SOSL queries for the data using a different technique but then takes the data and adds the where clause using SOQL - Eg FIND {foo} RETURNING Account (Id WHERE City='Rome') is analogous to SELECT Id FROM Account WHERE ID IN (<list of Ids from search>) AND City='Rome’  Follow SOQL best practices for the where clause
  • 23. Batch Apex  Most common issue is performance of SOQL used in the start() method.  Avoid the use of non-selective filters in the start() method. - Queries will perform significantly worse as more data is added.  Don’t add a non-selective filter to filter out a few rows relative to the overall size of the result set. Instead, use Apex to filter out rows in the execute() method.
  • 24. Batch Apex – Start Method Queries  Suppose you have 5M records in Account. You want to process the 80% of those records represented by this query: - SELECT Id FROM Account WHERE Rating__c IN ('Excellent','Good','Moderate')  Since there is no way to use a selective filter in a query for these records, this query will not run quickly even if Rating__c is indexed.  As an alternative, use this query in the start method: - SELECT Id FROM Account  Filter out the unwanted records in the execute method.
  • 25. Batch Apex – Start Method Queries – Sample Class global Database.QueryLocator<SObject> start(Database.BatchableContext bc) { return Database.getQueryLocator('SELECT Id FROM Account'); } global void execute(Database.BatchableContext bc, List<Account> scope) { List<Account> actualScope = [SELECT Id, Name, Description FROM Account WHERE Rating__c IN ('Excellent','Good','Moderate’) AND Id IN :scope]; for(Account acc : actualScope) { /* do something */ } }
  • 26. Batch Apex – Avoid Errors in Bulk DML global void execute(Database.BatchableContext bc, List<Account> scope) { for(Account acc : scope) { /* do something */ } Database.SaveResult[] results = Database.update(scope, false); /* look at results */ }  If an error occurs in this save operation, Salesforce will try to save it one record at a time.  This performs significantly slower than a bulk save without errors.
  • 27. Parent-Child Locking  Insert of Contact requires locking the parent Account.  Insert or Update of Event requires locking both the parent Contact and the parent Account.  Insert or Update of Task requires locking both the parent Contact and parent Account, if the Task is marked as complete.  Insert of Case requires locking the parent Contact and parent Account.  In objects that are part of a master/detail relationship, updating a detail record requires locking the parent if roll-up summary fields exist.
  • 28. Sharing Calculations  “Implicit” Sharing - Record access built into Salesforce applications  Parent Implicit Sharing (Account only, private sharing model) - Access to Contact/Opportunity/Case = Read access to parent account - not used when sharing on the child is controlled by parent - when user loses access to a child, need to check all other children to see if we can delete the parent implicit share
  • 29. Should we delete Jane’s parent implicit share?  Sharing calculations Universal Containers impacted by Data Skew  300K unassigned contacts X under account  Private sharing model ?  Jane has access to contact Bob Smith Bob Smith Fred Green Betty Brown … 300K Contact  Jane losses access to contact  Should we delete Jane’s parent implicit share for X Universal Containers?  Can’t code around Data Skew
  • 30. Effects of data skew  Sharing calculations run longer  Concurrent Request Limit error - Synchronous Apex request starts counting against the limit waiting > 5 sec for lock.  UNABLE_TO_LOCK_ROW error - Timed out waiting 10 seconds to acquire lock. - Even if no errors occur, waiting for locks can significantly slow DML operations.
  • 31. Recommendations  Consider a public read/write sharing model - Parent will still be locked. No sharing calculations.  10K children or less  Prevent parent-child data skews - Detect whether existing accounts already have "reached their limit" of child records, and create new accounts.
  • 32. Visualforce – Best Practices  http://developer.force.com/dreamforce/11/session/Blazing- Fast-Visualforce-Pages  View State - Filter and Paginate - Transient keyword - Simplify component hierarchy - JavaScript Remoting
  • 33. Analyzing Performance  Measure performance in the browser first to see if the problem is client-side or server-side. - Chrome Developer Tools and the Firebug plugin for Firefox will both work for this.  After confirming the problem is on the server, use the Salesforce Developer Console to find areas you can tune.
  • 34. Analyzing Performance Example Visualforce Page: DebugPage
  • 35. Analyzing Performance  In Chrome, press F12 to open Developer Tools.  Use the Network tab to measure performance.  It takes 8.74 seconds to get a response from the server when pressing the “Do BOTH Slow Query and Callout” button.
  • 36. Analyzing Performance – Developer Console  Use the Developer Console to analyze server-side performance.  Apex debug logs will be generated for every server request you perform while the window is open.  Several tools are available in the Developer Console to find performance hotspots.
  • 37. Analyzing Performance – Developer Console
  • 38. 1) Open the log for the slow request.
  • 39. 2) Open the Timeline tab.
  • 40. 3) Identify the slow portion and click on the bar. This loads the relevant log lines into the Execution Log.
  • 41. 4) Click on a line in the Execution Log to show the stack trace for the slow query on the left.
  • 42. Analyzing Performance – Log Levels  The default log levels generate a huge amount of output, most of which is unnecessary for debugging performance issues.  This also makes the console slower.  Try lowering the log levels so you can look for slow queries and callouts first.
  • 43. Analyzing Performance – Heap Dumps  The Developer Console can also be used to create Apex heaps that help in debugging.  For example, you can use it look at the bind variables for a query.
  • 45. Analyzing Performance – Heap Dumps  The bind variables will not show up in the debug console and may not be easy to find.  You can set a heap dump breakpoint to get the variable values at the time the query is run.
  • 46. Analyzing Performance – Heap Dumps Set a breakpoint on the line where the query is run.
  • 47. Analyzing Performance – Heap Dumps  The runtime values of the variables can be found in the Symbols tab of the heap dump viewer.
  • 48. Analyzing Performance – Useful Tools  Developer Console  Firebug (http://getfirebug.com) - Web development plugin for Firefox.  Chrome Developer Tools - Included with Chrome.  Workbench (https://workbench.developerforce.com) - Good for testing SOQL queries.  Fiddler (http://www.fiddler2.com/fiddler2) - Windows tool for debugging HTTP traffic, integrates well with IE and Firefox.
  • 49. Working with Support  SOQL performance - Open a case with org Id, user Id, SOQL and bind variables. - Can add custom single and two column indexes.  Record locks - Provide support the org ID and time frame where the issues occurred. - Provide the IDs of affected records if possible.
  • 50. Useful Links Architect Core Resources  http://wiki.developerforce.com/page/Architect_Core_Resources - Guide to Sharing Architecture - Record Access: Under the Hood - Best Practices for Deployments with Large Data Volumes - Asynchronous Processing in Force.com - VF Performance
  • 51. Stuart Bernstein Thomas Harris John Tan ISV Technical Evangelist LMTS, CCE Production SMTS, CCE Technical Support Enablement