2. What is Performance Tuning
âą Performance tuning can involve configuration
changes to software components.
âą Performance tuning can also include tuning SQL
queries and tuning an applications underlying code
to cater for concurrency and to improve efficiency.
4. Pagination
âą Fetching lots of records can cause slow down
the speed of system but its necessary because
database is systematically organized or
structure repository of indexed information.
6. Pagination
Two things you must do:
âą Decide on the maximum number of database
rows that can be included in each page. You
may hard code this value, or (my preferred
method) you can define it in a variable so that
the value may be changed at runtime.
7. Pagination
âą You then need to inform the user that other
'pages' are available and provide a mechanism
whereby the user is able to select a different
'page' of details. I currently use a set of
hyperlinks in a separate pagination area which
looks like this:
8. Pagination
How to do it :
1. Obtain the required page number.
- if(isset($_GET[âpagenoâ])) {
$pageno = $_GET[âpagenoâ];
} else {
$pageno = 1;
}
9. Pagination
2. Identify how many database rows are
available:
-$query = âSELECT count(1) FROM table
WHEREâŠâ;
$result = mysql_query($query, $db);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];
10. Pagination
3. Calculate number of $lastpage:
- $rows_per_page = 15;
$lastpage = ceil($numrows/$rows_per_page);
4.Ensure that $pageno is within range:
-$pageno = (int)$pageno;
If ($pageno > $lastpage) {
$pageno = $lastpage;
}
If ($pageno < 1 ){
$pageno = 1;
}
14. Performance: N+1 Query Problem
âą Select N+1 is a data access anti-pattern
where the database is accessed in a
suboptimal way.
âą Detecting Select N+1 problem usually means
that the data fetch strategy of the application
can be optimized.
15. Performance: N+1 Query Problem
Example:
$test = new test();
$hats = $test->getHats();
$cats = [];
foreach ($hats as $value) {
$cats[] = $test->getCats($value['id']);
}
16. Performance: N+1 Query Problem
Assuming $hats() has an implementation that boils
to:
SELECT * FROM hats WHERE âŠ
.. And $cats($hats_id) has an implementation like
this:
SELECT * FROM cats WHERE hats_id = ..
17. Performance: N+1 Query Problem
.. You will issue âN+1â queries when the code executes, Where
N is the number of cats:
SELECT * FROM cats WHERE ..
SELECT * FROM cats WHERE hats_id = 1
SELECT * FROM cats WHERE hats_id = 2
SELECT * FROM cats WHERE hats_id = 3
SELECT * FROM cats WHERE hats_id = 4
âŠ.
19. Performance: N+1 Query Problem
That is issue these queries:
SELECT * FROM cats WHERE âŠ
SELECT cats.id, cats.name, cats.created
FROM cats INNER JOIN hats
ON hats.id = cats.hats_id
20. Performance: N+1 Query Problem
âą It is much faster to issue 1 query which
returns thousands results than to issue t
queries which each thousands return 1
result.