SlideShare une entreprise Scribd logo
1  sur  24
Télécharger pour lire hors ligne
PHP	
  +	
  MySQL	
  

Optimization	
  
Introduction
•  The report includes project-related (EU) queries, tables
or databases example.
•  We can learn simple php/mysql optimization.
•  Know the main idea of using index in a table.
Let’s start from simple
cases
Let’s start from simple
cases
Avoid excess variables
function	
  isSomething()	
  
{	
  
	
  	
  	
  	
  	
  	
  	
  	
  $is_something	
  =	
  false;	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  if	
  ($some_condition_happens)	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  $is_something	
  =	
  true;	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  }	
  
	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  return	
  $is_something;	
  
}	
  
Let’s start from simple
cases

Use cache
(eg. APC, memcache)
Let’s start from simple
cases

Avoid queries inside a loop.
$player_ids = array(1, 2, 3, 4, 5, 6, 7, 8);
foreach ($player_ids as $id) {
$players_infos[] = $db->query(“SELECT * FROM player WHERE id = ?”,
array($id));
}
Let’s start from simple
cases

Select only needed columns.
Let’s start from simple
cases

count(*) vs count(col_name)
Let’s start from simple
cases

limit 1 when retrieving a
unique row.
Index to remember
Index to remember

Without an index, MySQL must begin
with the first row and then read
through the entire table to find the
relevant rows.
Index to remember
If there is a choice between multiple
indexes, MySQL normally uses the
index that finds the smallest number
of rows
Index to remember
To retrieve rows from other tables
when performing joins, MySQL can
use indexes on columns more
efficiently if they are declared as the
same type and size.
(Comparison of dissimilar columns may prevent use of
indexes if values cannot be compared directly without
conversion.)
Index to remember
“Index has significant cost. So you should not
create index everywhere.”
- http://lab.klab.org/
mysql-bootcamp/key.html
Examples
Examples
Scenario : Imagine you have a table that will
store millions of records (eg. item_history)
and you don’t have indexes.
Examples
The table structure :
id
int(10) unsigned
player_id
unsigned
item_master_id
unsigned
type
enum('INC','DEC')
reason

int(10)
int(10)
Examples
The problem : When you try to select data
from a table without index

See how many rows were checked after the search?
Examples
The solution : We are going to add index for the player_id
column
ALTER TABLE `item_history` ADD INDEX ( `player_id` )

See the advantage of adding an index?
The search took 1 row search only.
Examples
Upon looking on the table structure
which column/s can be used as index
when you want to retrieved a player’s
item history?
Exercise
Table Name : unit_history
Description : Tracks player’s record when he/she gained, evolved,
etc.. a unit.
Table structure :
id
player_id
unit_master_id
type
‘ITEM’)
created

int(10)
int(10)
int(10)
enum(‘INC’, ‘DEC’, ‘EVOLVED’, ‘EXP’,
timestamp
Exercise
The Scenario :
List player ids who gained and evolved the unit
id 10
The Question :
What are the possible index we might need in
order to retrieve the data faster.
Using index in joins

•  columns that you will use as the join
• 
• 

condition ( ON a.id = b.id ) should be
indexed.
LEFT JOIN will use indexed column from left
( a.id )
RIGHT JOIN will use indexed column from
right ( b.id )
Did you know?
That we MySQL has a built-in profiling tool?
SET PROFILING = 1;
SELECT COUNT(*) FROM table_name WHERE id = 1;
SHOW PROFILE FOR QUERY 1;
SHOW PROFILES;

Contenu connexe

Similaire à Php + MySql Optimization

15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performanceguest9912e5
 
Database Performance Tuning
Database Performance Tuning Database Performance Tuning
Database Performance Tuning Arno Huetter
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standardsAlessandro Baratella
 
Less08_Schema Advanced Databases and Management.pptx
Less08_Schema Advanced Databases and Management.pptxLess08_Schema Advanced Databases and Management.pptx
Less08_Schema Advanced Databases and Management.pptxMurtazaMughal13
 
MySQL Performance Optimization
MySQL Performance OptimizationMySQL Performance Optimization
MySQL Performance OptimizationMindfire Solutions
 
My SQL Skills Killed the Server
My SQL Skills Killed the ServerMy SQL Skills Killed the Server
My SQL Skills Killed the ServerdevObjective
 
Sql query tips or query optimization
Sql query tips or query optimizationSql query tips or query optimization
Sql query tips or query optimizationVivek Singh
 
SQL Server Select Topics
SQL Server Select TopicsSQL Server Select Topics
SQL Server Select TopicsJay Coskey
 
Postgresql Database Administration Basic - Day2
Postgresql  Database Administration Basic  - Day2Postgresql  Database Administration Basic  - Day2
Postgresql Database Administration Basic - Day2PoguttuezhiniVP
 
Data Structure In C#
Data Structure In C#Data Structure In C#
Data Structure In C#Shahzad
 
Indexing Strategies
Indexing StrategiesIndexing Strategies
Indexing Strategiesjlaspada
 
Database Application for La Salle
Database Application for La SalleDatabase Application for La Salle
Database Application for La SalleJimmy Chu
 
Chapter 1 - Introduction to Data Structure.ppt
Chapter 1 - Introduction to Data Structure.pptChapter 1 - Introduction to Data Structure.ppt
Chapter 1 - Introduction to Data Structure.pptNORSHADILAAHMADBADEL
 

Similaire à Php + MySql Optimization (20)

15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance
 
MySQL for beginners
MySQL for beginnersMySQL for beginners
MySQL for beginners
 
Mysql Optimization
Mysql OptimizationMysql Optimization
Mysql Optimization
 
Database Performance Tuning
Database Performance Tuning Database Performance Tuning
Database Performance Tuning
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
 
Less08_Schema Advanced Databases and Management.pptx
Less08_Schema Advanced Databases and Management.pptxLess08_Schema Advanced Databases and Management.pptx
Less08_Schema Advanced Databases and Management.pptx
 
MySQL Performance Optimization
MySQL Performance OptimizationMySQL Performance Optimization
MySQL Performance Optimization
 
My SQL Skills Killed the Server
My SQL Skills Killed the ServerMy SQL Skills Killed the Server
My SQL Skills Killed the Server
 
Sql killedserver
Sql killedserverSql killedserver
Sql killedserver
 
Cs341
Cs341Cs341
Cs341
 
Sql query tips or query optimization
Sql query tips or query optimizationSql query tips or query optimization
Sql query tips or query optimization
 
SQL Server Select Topics
SQL Server Select TopicsSQL Server Select Topics
SQL Server Select Topics
 
Postgresql Database Administration Basic - Day2
Postgresql  Database Administration Basic  - Day2Postgresql  Database Administration Basic  - Day2
Postgresql Database Administration Basic - Day2
 
Data Structure In C#
Data Structure In C#Data Structure In C#
Data Structure In C#
 
MySQL Indexes
MySQL IndexesMySQL Indexes
MySQL Indexes
 
Indexing Strategies
Indexing StrategiesIndexing Strategies
Indexing Strategies
 
Lecture 9.pptx
Lecture 9.pptxLecture 9.pptx
Lecture 9.pptx
 
Database Application for La Salle
Database Application for La SalleDatabase Application for La Salle
Database Application for La Salle
 
Chapter 1 - Introduction to Data Structure.ppt
Chapter 1 - Introduction to Data Structure.pptChapter 1 - Introduction to Data Structure.ppt
Chapter 1 - Introduction to Data Structure.ppt
 
Module 3
Module 3Module 3
Module 3
 

Plus de KLabCyscorpions-TechBlog (12)

Object Calisthenics in Objective-C
Object Calisthenics in Objective-CObject Calisthenics in Objective-C
Object Calisthenics in Objective-C
 
Auto Layout on Xcode 5
Auto Layout on Xcode 5Auto Layout on Xcode 5
Auto Layout on Xcode 5
 
Code Review for iOS
Code Review for iOSCode Review for iOS
Code Review for iOS
 
Object Calisthenics
Object CalisthenicsObject Calisthenics
Object Calisthenics
 
Why You're A Bad PHP Programmer
Why You're A Bad PHP ProgrammerWhy You're A Bad PHP Programmer
Why You're A Bad PHP Programmer
 
Redis Set Go
Redis Set GoRedis Set Go
Redis Set Go
 
Redis Beyond
Redis BeyondRedis Beyond
Redis Beyond
 
X-Debug in Php Storm
X-Debug in Php StormX-Debug in Php Storm
X-Debug in Php Storm
 
Software Testing
Software TestingSoftware Testing
Software Testing
 
MVC Web Application
MVC Web ApplicationMVC Web Application
MVC Web Application
 
AfNetworking vs. Native + Caching
AfNetworking vs. Native + CachingAfNetworking vs. Native + Caching
AfNetworking vs. Native + Caching
 
Bash
BashBash
Bash
 

Dernier

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 

Dernier (20)

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 

Php + MySql Optimization

  • 1. PHP  +  MySQL   Optimization  
  • 2. Introduction •  The report includes project-related (EU) queries, tables or databases example. •  We can learn simple php/mysql optimization. •  Know the main idea of using index in a table.
  • 3. Let’s start from simple cases
  • 4. Let’s start from simple cases Avoid excess variables function  isSomething()   {                  $is_something  =  false;                    if  ($some_condition_happens)  {                            $is_something  =  true;                    }                      return  $is_something;   }  
  • 5. Let’s start from simple cases Use cache (eg. APC, memcache)
  • 6. Let’s start from simple cases Avoid queries inside a loop. $player_ids = array(1, 2, 3, 4, 5, 6, 7, 8); foreach ($player_ids as $id) { $players_infos[] = $db->query(“SELECT * FROM player WHERE id = ?”, array($id)); }
  • 7. Let’s start from simple cases Select only needed columns.
  • 8. Let’s start from simple cases count(*) vs count(col_name)
  • 9. Let’s start from simple cases limit 1 when retrieving a unique row.
  • 11. Index to remember Without an index, MySQL must begin with the first row and then read through the entire table to find the relevant rows.
  • 12. Index to remember If there is a choice between multiple indexes, MySQL normally uses the index that finds the smallest number of rows
  • 13. Index to remember To retrieve rows from other tables when performing joins, MySQL can use indexes on columns more efficiently if they are declared as the same type and size. (Comparison of dissimilar columns may prevent use of indexes if values cannot be compared directly without conversion.)
  • 14. Index to remember “Index has significant cost. So you should not create index everywhere.” - http://lab.klab.org/ mysql-bootcamp/key.html
  • 16. Examples Scenario : Imagine you have a table that will store millions of records (eg. item_history) and you don’t have indexes.
  • 17. Examples The table structure : id int(10) unsigned player_id unsigned item_master_id unsigned type enum('INC','DEC') reason int(10) int(10)
  • 18. Examples The problem : When you try to select data from a table without index See how many rows were checked after the search?
  • 19. Examples The solution : We are going to add index for the player_id column ALTER TABLE `item_history` ADD INDEX ( `player_id` ) See the advantage of adding an index? The search took 1 row search only.
  • 20. Examples Upon looking on the table structure which column/s can be used as index when you want to retrieved a player’s item history?
  • 21. Exercise Table Name : unit_history Description : Tracks player’s record when he/she gained, evolved, etc.. a unit. Table structure : id player_id unit_master_id type ‘ITEM’) created int(10) int(10) int(10) enum(‘INC’, ‘DEC’, ‘EVOLVED’, ‘EXP’, timestamp
  • 22. Exercise The Scenario : List player ids who gained and evolved the unit id 10 The Question : What are the possible index we might need in order to retrieve the data faster.
  • 23. Using index in joins •  columns that you will use as the join •  •  condition ( ON a.id = b.id ) should be indexed. LEFT JOIN will use indexed column from left ( a.id ) RIGHT JOIN will use indexed column from right ( b.id )
  • 24. Did you know? That we MySQL has a built-in profiling tool? SET PROFILING = 1; SELECT COUNT(*) FROM table_name WHERE id = 1; SHOW PROFILE FOR QUERY 1; SHOW PROFILES;