# Write Efficient Apex and SOQL

To improve the overall performance of a Visualforce page, write efficient Apex and SOQL.

When you write Apex or SOQL for use within a Visualforce page:

- Perform calculations in SOQL instead of in Apex whenever possible.
- Never perform Data Manipulation Language (DML) operations inside a loop.
- Filter in SOQL first, then in Apex, and finally in Visualforce.

**See Also**

- [*Apex Developer Guide*: Working with Data in Apex](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_data_intro.htm)
- [*Apex Developer Guide*: Working with Very Large SOQL Queries](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_VLSQ.htm)
- [*SOQL and SOSL Reference*](https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_sosl_intro.htm)
