PHP	
  +	
  MySQL	
  

Optimization	
  
Introduction
•  The report includes project-related (EU) queries, tables
or databases example.
•  We can learn simple php/...
Let’s start from simple
cases
Let’s start from simple
cases
Avoid excess variables
function	
  isSomething()	
  
{	
  
	
  	
  	
  	
  	
  	
  	
  	
  $...
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_...
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 th...
Index to remember
If there is a choice between multiple
indexes, MySQL normally uses the
index that finds the smallest num...
Index to remember
To retrieve rows from other tables
when performing joins, MySQL can
use indexes on columns more
efficien...
Index to remember
“Index has significant cost. So you should not
create index everywhere.”
- http://lab.klab.org/
mysql-bo...
Examples
Examples
Scenario : Imagine you have a table that will
store millions of records (eg. item_history)
and you don’t have ind...
Examples
The table structure :
id
int(10) unsigned
player_id
unsigned
item_master_id
unsigned
type
enum('INC','DEC')
reaso...
Examples
The problem : When you try to select data
from a table without index

See how many rows were checked after the se...
Examples
The solution : We are going to add index for the player_id
column
ALTER TABLE `item_history` ADD INDEX ( `player_...
Examples
Upon looking on the table structure
which column/s can be used as index
when you want to retrieved a player’s
ite...
Exercise
Table Name : unit_history
Description : Tracks player’s record when he/she gained, evolved,
etc.. a unit.
Table s...
Exercise
The Scenario :
List player ids who gained and evolved the unit
id 10
The Question :
What are the possible index w...
Using index in joins

•  columns that you will use as the join
• 
• 

condition ( ON a.id = b.id ) should be
indexed.
LEFT...
Did you know?
That we MySQL has a built-in profiling tool?
SET PROFILING = 1;
SELECT COUNT(*) FROM table_name WHERE id = 1...
Upcoming SlideShare
Loading in …5
×

Php + MySql Optimization

1,348 views
1,166 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,348
On SlideShare
0
From Embeds
0
Number of Embeds
656
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Php + MySql Optimization

  1. 1. PHP  +  MySQL   Optimization  
  2. 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. 3. Let’s start from simple cases
  4. 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. 5. Let’s start from simple cases Use cache (eg. APC, memcache)
  6. 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. 7. Let’s start from simple cases Select only needed columns.
  8. 8. Let’s start from simple cases count(*) vs count(col_name)
  9. 9. Let’s start from simple cases limit 1 when retrieving a unique row.
  10. 10. Index to remember
  11. 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. 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. 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. 14. Index to remember “Index has significant cost. So you should not create index everywhere.” - http://lab.klab.org/ mysql-bootcamp/key.html
  15. 15. Examples
  16. 16. Examples Scenario : Imagine you have a table that will store millions of records (eg. item_history) and you don’t have indexes.
  17. 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. 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. 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. 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. 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. 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. 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. 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;

×