how to troubleshoot & optimize database query performance for your application
Post on 17-Jan-2017
570 Views
Preview:
TRANSCRIPT
Confidential, Dynatrace, LLC
Asad AliDirector, Product Specialist@AsadThoughtsDynatrace
How to Troubleshoot & Optimize Database Query Performance for Your Application
Let the blame game start!
Web Server Application Server Database
DEV Team DBA Team
Blame the database for all performance issues
Blame the SW/HW or system administrators
Network?
Database Heavy: 66.51% (40.27s)
Time Spent in SQL Execs
Excessive SQL: 24889! Calls to Database
Database Heavy: 66.51% (40.27s)
Time Spent in SQL Execs
DatabasePerformance
hotspots
Application Design
DB Design DB Queries
Server/Infrastructure
Database Performance Hotspots
Application Design Database Infrastructure
Application
of databaseperformance issuescan be solved by developers70
%
Database Performance Hotspots
• Lack of use of bind values• Too many SQL statements per user action• Database server overloaded• DB connection pool monitoring
Example 1:performance tuning by using bind variables
Oracle SQL Cache<?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'");
?>
?>
Query Plan Evaluation
MSSQL Cache
MSSQL Cache
Little Bobby TablesaddStudentInfo(String lname, String fname) {
String query = “insert into Student(last_name, first_name)values (‘”+lname + ”’, ‘” + fname + “’)”;
}Normal Operation:
First Name: AsadLast Name : Aliinsert into Student (‘Ali’, ‘Asad’)
Malicious Operation:First Name: Asad’); Drop table Student;Last Name : Aliinsert into Student (‘Ali’, ‘Asad’); Drop table
Student
Little Bobby Tables
Oracle SQL Cache<?php
$db = new PDO('oci:dbname=sid', 'username', 'password');
$data = Array();$ps = $db->prepare("select * from country where code = :code");$data[] = $ps->execute(array("code" => "AT"));$data[] = $ps->execute(array("code" => "AU"));$data[] = $ps->execute(array("code" => "NZ"));$data[] = $ps->execute(array("code" => "ES"));
?>
?>
Example 2:application performing
too many SQL statements
Excessive SQL: 24889! Calls to Database
Database Heavy: 66.51% (40.27s)
Time Spent in SQL Execs
Why are there so many SQL statements?$schools = new SchoolEntities();
foreach ($schools as $school) { foreach ($school->departments as $department) { foreach ($department->courses as $course) { echo $department->name . ": " . $course->title); } }}
N+1 problem• 10 schools• 20 departments per school• 50 courses per department
=> 10 x 20 x 50 = 10001 single database statements !!!
Retrieving too many records$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); } }}
=> 3 records, still 10001 queries
Solution: use a JOIN query with a WHERE clause$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);}
=> ONE database statement !!!
Example 3:overloaded database server
Major database performance problem
Example 4: database connection pool settings
Database Connection Pool
Database Connection Pool
Database Connection Pool
Auto DB Pattern Detection
Live Demo
Q & A@AsadThoughts
Performance Tuning – best practices• Use bind variables to leverage SQL caching• Use join queries to avoid N+1 problems• Avoid index killers in queries• Use stored procedures for complex functionality• Leverage connection pooling• Watch for other processes / application on your database
http://apmblog.dynatrace.com/2016/05/23/improve-oracle-database-performance-using-bind-variables/
https://www.infoq.com/articles/Diagnose-Microservice-Performance-Anti-Patterns
top related