SlideShare a Scribd company logo
Some useful tricks &
techniques for code
optimization
By Abu Saleh Muhammad Shaon
PHP developer at VroomVroomVroom
Today's goals
➢ How to run code faster
➢ How to optimize memory usages
➢ How to optimize query
➢ How to optimize use of caching
➢ Some tools that can help for better coding
Things to be consider while coding
➢ Always use single quote for string
Things to be consider while coding
➢ Always remove unused namespace
Things to be consider while coding
➢ Always remove unused codes, test codes, variables etc
Things to be consider while coding
➢ Don’t declare variables inside conditions which is used
out of the condition
Things to be consider while coding
➢ Use type hint for function parameters and return.
○ It will save parsing time
○ It will save you from unexpected in initial stage
○ One of the key performance point for PHP 7
Things to be consider while coding
➢ Use generator/yield for memory optimization
Things to be consider while coding
➢ For reading large file read line by line instead of read
whole file at once.
➢ For large array use chunk_array
➢ Unset large arrays when no longer needed
➢ Use reference if you want to modify array inside loop
Things to be consider while coding
➢ Use build in construct / functions as much as possible
○ PHP_EOL
○ array_*()
○ string_*()
○ FILTER_VALIDATE_*
Things to be consider while coding
➢ , is faster than .
Things to be consider while coding
➢ Avoid too much class abstraction/layer
➢ Don’t use else unless it needed
➢ Don’t run sql query inside loops
○ Special case if needed you can do batch operation
Things to be consider while coding
➢ Pre increment ++$i is faster than $++
➢ Avoid @ error suppress operator
➢ “foreach” is better than any other loops for Array
➢ “switch” is faster than if() else() condition
Things to be consider while coding
➢ “isset” is faster than “strlen”
Things to be consider while coding
➢ Use strict operator “===” “!==” while compare
something
➢ Normal replace/search function is faster than regular
expression based replace/search
○ Use ctype_alnum(), ctype_alpha() and ctype_digit() over regex
Things to be consider while coding
➢ Order of conditions can be fact
Things to be consider while coding
Caching
➢ Route cache
➢ Config cache
➢ Caching object/array
➢ Partial caching
➢ Use proper lifetime for caching
General optimization : Database
➢ Index columns those are using more frequent select
query
➢ Use columns lenght short as much as possible
○ INT instead of BIGINT
○ VARCHAR(10) instead of VARCHAR(255)
General optimization : Database
➢ Normalize redundant data from data
○ Can be possible by using separate table
➢ Remove create_at , deleted_at at from child table
General optimization : Database
➢ Pay special attention to columns those are using in join
➢ Define column as NOT NULL if possible
➢ An index on the whole column is not always necessary
○ Instead index just a prefix of a column
○ Prefix indexes take less space and the operation are
faster
General optimization : Database
➢ Query optimization tool Explain
➢ Explain tell you
○ In which order tables are reading
○ What types of query execute
○ Which indexes are using
○ How manys row are using to find the results
General optimization : Database
General optimization : Database
➢ Eager Loading the relational table
➢ It will solve n+1 problem
Demo - time : 5-7 mins
➢ DB Explain & Eager loading
General Tips
➢ Don’t make needless copies codes
➢ Use support of IDE’s as much as possible
➢ Don’t write code straight way without thinking very
common exceptional scenario.
➢ If possible use paper/pencil for find the solution, it will
give you more precise solutions.
Measuring Tools
➢ microtime()
➢ memory_get_usage()
➢ memory_get_peak_usage()
➢ Debugbar
➢ XDebug profiler
Some useful references
➢ https://www.sitepoint.com/php
➢ http://www.planet-php.net
➢ http://www.phpbuilder.com
➢ https://medium.com
➢ https://laravel-news.com
➢ https://www.digitalocean.com/community/tutorials
➢ https://en.wikipedia.org/wiki/PHP_Standard_Recommendation
PHP & Mysql Code optimization

More Related Content

Similar to PHP & Mysql Code optimization (20)

Accumulo14 15
Accumulo14 15
Sqrrl
 
Kill mysql-performance
Kill mysql-performance
kriptonium
 
Don't you (forget about me) - PHP Meetup Lisboa 2023
Don't you (forget about me) - PHP Meetup Lisboa 2023
Bernd Alter
 
Optimizing Python
Optimizing Python
AdimianBE
 
Sql killedserver
Sql killedserver
ColdFusionConference
 
My SQL Skills Killed the Server
My SQL Skills Killed the Server
devObjective
 
The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)
Ki Sung Bae
 
The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)
Ki Sung Bae
 
Search Engine-Building with Lucene and Solr, Part 2 (SoCal Code Camp LA 2013)
Search Engine-Building with Lucene and Solr, Part 2 (SoCal Code Camp LA 2013)
Kai Chan
 
Multicore
Multicore
Mark Veltzer
 
OutSystems Tips and Tricks
OutSystems Tips and Tricks
OutSystems
 
MySQL Query Optimisation 101
MySQL Query Optimisation 101
Federico Razzoli
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
Inductive Automation
 
Cómo se diseña una base de datos que pueda ingerir más de cuatro millones de ...
Cómo se diseña una base de datos que pueda ingerir más de cuatro millones de ...
javier ramirez
 
Try harder or go home
Try harder or go home
jaredhaight
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
Inductive Automation
 
Tahoe Dreamin 2018: It simply works... until it breaks!
Tahoe Dreamin 2018: It simply works... until it breaks!
Daniel Stange
 
The Parquet Format and Performance Optimization Opportunities
The Parquet Format and Performance Optimization Opportunities
Databricks
 
Top 10 Perl Performance Tips
Top 10 Perl Performance Tips
Perrin Harkins
 
Programming for Performance
Programming for Performance
Cris Holdorph
 
Accumulo14 15
Accumulo14 15
Sqrrl
 
Kill mysql-performance
Kill mysql-performance
kriptonium
 
Don't you (forget about me) - PHP Meetup Lisboa 2023
Don't you (forget about me) - PHP Meetup Lisboa 2023
Bernd Alter
 
Optimizing Python
Optimizing Python
AdimianBE
 
My SQL Skills Killed the Server
My SQL Skills Killed the Server
devObjective
 
The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)
Ki Sung Bae
 
The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)
Ki Sung Bae
 
Search Engine-Building with Lucene and Solr, Part 2 (SoCal Code Camp LA 2013)
Search Engine-Building with Lucene and Solr, Part 2 (SoCal Code Camp LA 2013)
Kai Chan
 
OutSystems Tips and Tricks
OutSystems Tips and Tricks
OutSystems
 
MySQL Query Optimisation 101
MySQL Query Optimisation 101
Federico Razzoli
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
Inductive Automation
 
Cómo se diseña una base de datos que pueda ingerir más de cuatro millones de ...
Cómo se diseña una base de datos que pueda ingerir más de cuatro millones de ...
javier ramirez
 
Try harder or go home
Try harder or go home
jaredhaight
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
Inductive Automation
 
Tahoe Dreamin 2018: It simply works... until it breaks!
Tahoe Dreamin 2018: It simply works... until it breaks!
Daniel Stange
 
The Parquet Format and Performance Optimization Opportunities
The Parquet Format and Performance Optimization Opportunities
Databricks
 
Top 10 Perl Performance Tips
Top 10 Perl Performance Tips
Perrin Harkins
 
Programming for Performance
Programming for Performance
Cris Holdorph
 

Recently uploaded (20)

Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Alluxio, Inc.
 
What is data visualization and how data visualization tool can help.pdf
What is data visualization and how data visualization tool can help.pdf
Varsha Nayak
 
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Intelli grow
 
Artificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across Industries
SandeepKS52
 
Zoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutions
reenashriee
 
Agile Software Engineering Methodologies
Agile Software Engineering Methodologies
Gaurav Sharma
 
How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0
Anchore
 
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
BradBedford3
 
AI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA Technologies
SandeepKS52
 
Shell Skill Tree - LabEx Certification (LabEx)
Shell Skill Tree - LabEx Certification (LabEx)
VICTOR MAESTRE RAMIREZ
 
GDG Douglas - Google AI Agents: Your Next Intern?
GDG Douglas - Google AI Agents: Your Next Intern?
felipeceotto
 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
Aarno Aukia
 
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Puppy jhon
 
dp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdf
pravkumarbiz
 
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
BradBedford3
 
Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4
Gaurav Sharma
 
Migrating to Azure Cosmos DB the Right Way
Migrating to Azure Cosmos DB the Right Way
Alexander (Alex) Komyagin
 
Microsoft Business-230T01A-ENU-PowerPoint_01.pptx
Microsoft Business-230T01A-ENU-PowerPoint_01.pptx
soulamaabdoulaye128
 
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
 
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Alluxio, Inc.
 
What is data visualization and how data visualization tool can help.pdf
What is data visualization and how data visualization tool can help.pdf
Varsha Nayak
 
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Intelli grow
 
Artificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across Industries
SandeepKS52
 
Zoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutions
reenashriee
 
Agile Software Engineering Methodologies
Agile Software Engineering Methodologies
Gaurav Sharma
 
How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0
Anchore
 
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
BradBedford3
 
AI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA Technologies
SandeepKS52
 
Shell Skill Tree - LabEx Certification (LabEx)
Shell Skill Tree - LabEx Certification (LabEx)
VICTOR MAESTRE RAMIREZ
 
GDG Douglas - Google AI Agents: Your Next Intern?
GDG Douglas - Google AI Agents: Your Next Intern?
felipeceotto
 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
Aarno Aukia
 
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Puppy jhon
 
dp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdf
pravkumarbiz
 
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
BradBedford3
 
Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4
Gaurav Sharma
 
Microsoft Business-230T01A-ENU-PowerPoint_01.pptx
Microsoft Business-230T01A-ENU-PowerPoint_01.pptx
soulamaabdoulaye128
 
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
 
Ad

PHP & Mysql Code optimization

  • 1. Some useful tricks & techniques for code optimization By Abu Saleh Muhammad Shaon PHP developer at VroomVroomVroom
  • 2. Today's goals ➢ How to run code faster ➢ How to optimize memory usages ➢ How to optimize query ➢ How to optimize use of caching ➢ Some tools that can help for better coding
  • 3. Things to be consider while coding ➢ Always use single quote for string
  • 4. Things to be consider while coding ➢ Always remove unused namespace
  • 5. Things to be consider while coding ➢ Always remove unused codes, test codes, variables etc
  • 6. Things to be consider while coding ➢ Don’t declare variables inside conditions which is used out of the condition
  • 7. Things to be consider while coding ➢ Use type hint for function parameters and return. ○ It will save parsing time ○ It will save you from unexpected in initial stage ○ One of the key performance point for PHP 7
  • 8. Things to be consider while coding ➢ Use generator/yield for memory optimization
  • 9. Things to be consider while coding ➢ For reading large file read line by line instead of read whole file at once. ➢ For large array use chunk_array ➢ Unset large arrays when no longer needed ➢ Use reference if you want to modify array inside loop
  • 10. Things to be consider while coding ➢ Use build in construct / functions as much as possible ○ PHP_EOL ○ array_*() ○ string_*() ○ FILTER_VALIDATE_*
  • 11. Things to be consider while coding ➢ , is faster than .
  • 12. Things to be consider while coding ➢ Avoid too much class abstraction/layer ➢ Don’t use else unless it needed ➢ Don’t run sql query inside loops ○ Special case if needed you can do batch operation
  • 13. Things to be consider while coding ➢ Pre increment ++$i is faster than $++ ➢ Avoid @ error suppress operator ➢ “foreach” is better than any other loops for Array ➢ “switch” is faster than if() else() condition
  • 14. Things to be consider while coding ➢ “isset” is faster than “strlen”
  • 15. Things to be consider while coding ➢ Use strict operator “===” “!==” while compare something ➢ Normal replace/search function is faster than regular expression based replace/search ○ Use ctype_alnum(), ctype_alpha() and ctype_digit() over regex
  • 16. Things to be consider while coding ➢ Order of conditions can be fact
  • 17. Things to be consider while coding
  • 18. Caching ➢ Route cache ➢ Config cache ➢ Caching object/array ➢ Partial caching ➢ Use proper lifetime for caching
  • 19. General optimization : Database ➢ Index columns those are using more frequent select query ➢ Use columns lenght short as much as possible ○ INT instead of BIGINT ○ VARCHAR(10) instead of VARCHAR(255)
  • 20. General optimization : Database ➢ Normalize redundant data from data ○ Can be possible by using separate table ➢ Remove create_at , deleted_at at from child table
  • 21. General optimization : Database ➢ Pay special attention to columns those are using in join ➢ Define column as NOT NULL if possible ➢ An index on the whole column is not always necessary ○ Instead index just a prefix of a column ○ Prefix indexes take less space and the operation are faster
  • 22. General optimization : Database ➢ Query optimization tool Explain ➢ Explain tell you ○ In which order tables are reading ○ What types of query execute ○ Which indexes are using ○ How manys row are using to find the results
  • 24. General optimization : Database ➢ Eager Loading the relational table ➢ It will solve n+1 problem
  • 25. Demo - time : 5-7 mins ➢ DB Explain & Eager loading
  • 26. General Tips ➢ Don’t make needless copies codes ➢ Use support of IDE’s as much as possible ➢ Don’t write code straight way without thinking very common exceptional scenario. ➢ If possible use paper/pencil for find the solution, it will give you more precise solutions.
  • 27. Measuring Tools ➢ microtime() ➢ memory_get_usage() ➢ memory_get_peak_usage() ➢ Debugbar ➢ XDebug profiler
  • 28. Some useful references ➢ https://www.sitepoint.com/php ➢ http://www.planet-php.net ➢ http://www.phpbuilder.com ➢ https://medium.com ➢ https://laravel-news.com ➢ https://www.digitalocean.com/community/tutorials ➢ https://en.wikipedia.org/wiki/PHP_Standard_Recommendation