+ All Categories
Home > Technology > Slow Database in your PHP stack? Don't blame the DBA!

Slow Database in your PHP stack? Don't blame the DBA!

Date post: 07-Apr-2017
Category:
Upload: harald-zeitlhofer
View: 258 times
Download: 2 times
Share this document with a friend
45
Slow database in your PHP stack? Don’t blame the DBA! [email protected] @HZeitlhofer
Transcript
Page 1: Slow Database in your PHP stack? Don't blame the DBA!

SlowdatabaseinyourPHPstack?Don’tblametheDBA!

[email protected]@HZeitlhofer

Page 2: Slow Database in your PHP stack? Don't blame the DBA!
Page 3: Slow Database in your PHP stack? Don't blame the DBA!
Page 4: Slow Database in your PHP stack? Don't blame the DBA!

Lettheblamegamestart!

WebServer ApplicationServer Database

DEV Team DBA Team

Blame the database for all performance issues

Blame the SW/HW or system administrators

Network?

Page 5: Slow Database in your PHP stack? Don't blame the DBA!

function roomReservationReport($room)

{

$reservations = loadDataForRoom($room->id);

return generateReport($room, $reservations);

}

SlowReport

Page 6: Slow Database in your PHP stack? Don't blame the DBA!

function roomReservationReport($room)

{

$tstart = microtime(true);

$reservations = loadDataForRoom($room->id);

$dataLoadTime = microtime(true) - $tstart;

return generateReport($room, $reservations);

}

DEVteamaddlogoutput

Page 7: Slow Database in your PHP stack? Don't blame the DBA!

45sDEVteamresult

Page 8: Slow Database in your PHP stack? Don't blame the DBA!
Page 9: Slow Database in your PHP stack? Don't blame the DBA!

<1msDBAteamlooksatDBstatsperquery

Page 10: Slow Database in your PHP stack? Don't blame the DBA!
Page 11: Slow Database in your PHP stack? Don't blame the DBA!
Page 12: Slow Database in your PHP stack? Don't blame the DBA!

ExcessiveSQL:24889!CallstoDatabase

DatabaseHeavy:66.51%(40.27s)

TimeSpentinSQLExecs

Page 13: Slow Database in your PHP stack? Don't blame the DBA!

Server/Infrastructure

DBQueries

Applicationdesign

DBDesignDatabase

Performancehotspots

Page 14: Slow Database in your PHP stack? Don't blame the DBA!

DatabasePerformanceHotspots

Application Design Database Infrastructure

Page 15: Slow Database in your PHP stack? Don't blame the DBA!
Page 16: Slow Database in your PHP stack? Don't blame the DBA!

http://apmblog.dynatrace.com/2015/06/25/when-old-jira-tickets-killed-our-productivityuser-experience/

Toomanydatabasequeries

Page 17: Slow Database in your PHP stack? Don't blame the DBA!

Exercise:createlistofcourses

Page 18: Slow Database in your PHP stack? Don't blame the DBA!

Createlistofcourses$schools = new SchoolEntities();

foreach ($schools as $school) {

foreach ($school->departments as $department) {

foreach ($department->courses as $course) {

echo $department->name . ": " . $course->title;

}

}

} • 10schools• 20departmentsperschool• 50cursesperdepartment

Page 19: Slow Database in your PHP stack? Don't blame the DBA!

N+1problem• 10schools• 20departmentsperschool• 50cursesperdepartment

=>10x20x50=10001singledatabasestatements!!!

Page 20: Slow Database in your PHP stack? Don't blame the DBA!

UseaJOINquery$rows = $db->query ("

select department.name as department, course.title as course

from school

join department on school_id = school.id

join course on department_id = department.id

");

foreach ($rows as $row) {

echo $row->department . ": " . $row->course;

}

=>ONEdatabasestatement!!!

Page 21: Slow Database in your PHP stack? Don't blame the DBA!

Retrievingtoomanyrecords

Page 22: Slow Database in your PHP stack? Don't blame the DBA!

Retrievingtoomanyrecords$schools = new SchoolEntities();

foreach ($schools->departments as $department) {

foreach ($department->courses as $course) {

if ($course->category == "SQL" &&

$course->level == "expert") {

echo $department->name . ": " . $course->title);

}

}

}

=>3records,still10001queries

Page 23: Slow Database in your PHP stack? Don't blame the DBA!

UseaJOINquery$rows = $db->query ("

select department.name as department, course.title as course

from school

join department on school_id = school.id

join course on department_id = department.id

where course.category = 'SQL' and course.level = 'expert'

");

foreach ($rows as $row) {

echo $department . ": " . $course);

}

=>ONEdatabasestatement!!!

Page 24: Slow Database in your PHP stack? Don't blame the DBA!

Caching

Page 25: Slow Database in your PHP stack? Don't blame the DBA!

MySQLQueryCache• Cachesqueryresultsinmemory

Page 26: Slow Database in your PHP stack? Don't blame the DBA!

OracleSQLCache• OracleSQLcachestorestheexecutionplanforaSQLstatement• Evendifferentplansastoredfordifferentbindvariablevalues• Noparsingrequired• ParsingisthemostCPUconsumingprocess• CachekeyisfullSQLquerytext

select * from country where code = 'AT'

!=

SELECT * from country where code = 'AT'

Page 27: Slow Database in your PHP stack? Don't blame the DBA!

OracleSQLCache<?php

$db = new PDO('oci:dbname=sid', 'username', 'password');

$data = Array();

$data[] = $db->query("select * from country where code = 'AT'");

$data[] = $db->query("select * from country where code = 'AU'");

$data[] = $db->query("select * from country where code = 'NZ'");

$data[] = $db->query("select * from country where code = 'ES'");

?>

?>

Page 28: Slow Database in your PHP stack? Don't blame the DBA!
Page 29: Slow Database in your PHP stack? Don't blame the DBA!

OracleSQLCache<?php

$db = new PDO('oci:dbname=sid', 'username', 'password');

$data = Array();

$ps = $db->prepare("select * from country where code = :code");

$data[] = $ps->exec(array("code" => "AT"));

$data[] = $ps->exec(array("code" => "AU"));

$data[] = $ps->exec(array("code" => "NZ"));

$data[] = $ps->exec(array("code" => "ES"));

?>

Page 30: Slow Database in your PHP stack? Don't blame the DBA!

DatabaseHealth

Page 31: Slow Database in your PHP stack? Don't blame the DBA!

Indexes

Page 32: Slow Database in your PHP stack? Don't blame the DBA!

Indexesaredatastructuresforreferencingasearchvalue(inanindexedcolumn)to(a)row(s)inthetargettable

Page 33: Slow Database in your PHP stack? Don't blame the DBA!
Page 34: Slow Database in your PHP stack? Don't blame the DBA!

Indexesareusedtofindrecordsinatablewhenawhereclauseisused

Page 35: Slow Database in your PHP stack? Don't blame the DBA!
Page 36: Slow Database in your PHP stack? Don't blame the DBA!

Indexesarealsousedtojointablesinaquery,wheremultipletablesareused

Page 37: Slow Database in your PHP stack? Don't blame the DBA!
Page 38: Slow Database in your PHP stack? Don't blame the DBA!

UsefulcommandsinMySQL

explain select ...

show indexes for table_name

Page 39: Slow Database in your PHP stack? Don't blame the DBA!

I'madeveloperforbusinesslogic!Idon'thavetounderstandwhatthe

databasedoesandIdonotcareatall!

I'madeveloperforbusinesslogic!MycodecanbemuchmoreefficientifIunderstandwhatthedatabasedoesand

howitworks!

Page 40: Slow Database in your PHP stack? Don't blame the DBA!

Makedevelopersself-sufficient

Page 41: Slow Database in your PHP stack? Don't blame the DBA!

Makedevelopersself-sufficient

Page 42: Slow Database in your PHP stack? Don't blame the DBA!

Makedevelopersself-sufficient

Page 43: Slow Database in your PHP stack? Don't blame the DBA!

SlowSingleSQLquery–tuning/indexingmightberequired

Makedevelopersself-sufficient

Page 44: Slow Database in your PHP stack? Don't blame the DBA!

Takeaways• Muchtimespentindatabasedoesnotnecessarilymeandatabaseisslow!• Performancehotspots

• ToomanySQLstatements• Missingcaching• Badquerydesign• Index(mis)usage• Undersizeddatabaseserver

• Letdevelopersknowwhat’shappeninginthedatabase• TheDBAisourfriend!

Page 45: Slow Database in your PHP stack? Don't blame the DBA!

[email protected]@dynatrace.comhttp://blog.dynatrace.com

DynatraceFreeTrialFreePersonalLicensehttp://bit.ly/monitoring-2016


Recommended