1. Avoid nested loop. Make use of Map data type instead of nested loop.
2. Make use of future methods.
3. Try to use Aggregate SOQL.
Check the below link for more information
https://help.salesforce.com/articleView?id=000232681&language=en_US&type=1