1 of 40

Deep dive into Query Performance

Peter Zaitsev,

Founder at Percona

10 June 2023

©2023 Percona | Confidential

2 of 40

© Percona

2

©2023 Percona | Confidential

3 of 40

Database is a Black Box

© Percona

3

©2023 Percona | Confidential

4 of 40

Meaning

© Percona

4

You can connect to the Database Service Point, Quickly

Run Queries you need to run

©2023 Percona | Confidential

5 of 40

Queries

5

Run them without errors

1

Run them with correct results

2

Run them with required response time

3

©2023 Percona | Confidential

6 of 40

Performance

Performance is about Response Time you get for your Queries

© Percona

6

©2023 Percona | Confidential

7 of 40

Great design is not only about Performance

  • Security
  • Availability
  • Costs
  • Maintability
  • Impact on other users

© Percona

7

©2023 Percona | Confidential

8 of 40

Response Time - Database View

“I see database responds to queries in 5ms in average”

© Percona

8

©2023 Percona | Confidential

9 of 40

Response Time – Business View

All Users have outstanding performance experience with all their application interactions

© Percona

9

©2023 Percona | Confidential

10 of 40

Downtime

Very Bad Performance is indistinguishable from downtime

© Percona

10

©2023 Percona | Confidential

11 of 40

Forget averages

There once lived a man who tried to cross a river, in average one meter deep

© Percona

11

©2023 Percona | Confidential

12 of 40

© Percona

12

©2023 Percona | Confidential

13 of 40

Percentile

© Percona

13

99 percentile does not translate in 99% users having great performance

If every user interaction has 10 database queries

User in average has 10 interactions

Roughly 50% of session will have query with p99 response time

©2023 Percona | Confidential

14 of 40

Errors

  • Look at Response time of Successful Queries, do not let “fast errors” to screw up your data
  • Measure response time of “slow errors” as it contributes to user experience

© Percona

14

©2023 Percona | Confidential

15 of 40

Over Time

15

LOOK AT RESPONSE TIME TRENDS OVER TIME

MINOR SLOWDOWN OFTEN HAPPENS BEFORE POOR PERFORMANCE “DOWNTIME”

PERFORMANCE CAN BE WORSE AT CERTAIN TIMES – BACKUPS, BATCH JOBS, MAINTENANCE

©2023 Percona | Confidential

16 of 40

© Percona

16

©2023 Percona | Confidential

17 of 40

Database or Application ?

  • Instrument on Application side to understand what drives performance of user interactions
  • Instrument of Database Size to understand what causes queries to be slow and what can be done about it

© Percona

17

©2023 Percona | Confidential

18 of 40

Response Time – Business View

All Users have outstanding performance experience with all their application interactions

© Percona

18

©2023 Percona | Confidential

19 of 40

Enhancing Query Meta Data

SQL Commenter project by Google

https://per.co.na/SQLcommenter

© Percona

19

©2023 Percona | Confidential

20 of 40

Query Meta Data Possibilities

20

Actual User/Tenant

Application/Functionality

Version Information (A/B Testing)

Responsible Engineer/Team

©2023 Percona | Confidential

21 of 40

Query

  • Different Queries have different performance profile
  • They also correspond to different “user actions”
  • And may have different acceptable level of Performance

© Percona

21

©2023 Percona | Confidential

22 of 40

Schema and Database

  • Different Applications/Services may be using different ones
  • In sharded environment can correspond to application “tenant”

© Percona

22

©2023 Percona | Confidential

23 of 40

Database view in Percona Monitoring and Management

© Percona

23

©2023 Percona | Confidential

24 of 40

Table/�Collection

  • Can help identify “problematic data”
  • Indexing changes impact queries hitting object
  • Maintenance often impacts specific table

© Percona

24

©2023 Percona | Confidential

25 of 40

Database User

© Percona

25

IDENTIFY SERVICE/APPLICATION

FIND HUMAN TROUBLE MAKERS WITH INTERACTIVE ACCESS

©2023 Percona | Confidential

26 of 40

© Percona

26

©2023 Percona | Confidential

27 of 40

Database Host

© Percona

27

Sharded environments often have multiple hosts handling the same traffic

Yet Problems often can be limited to some hosts

Data/Traffic Balance, configuration, invisible differences

©2023 Percona | Confidential

28 of 40

Database Instances

28

©2023 Percona | Confidential

29 of 40

App Server/�Web Server/�Service Instance

29

You may expect all instances of the same type causing same even load

It may not be the case

Code versions, configuration, load balancer behavior, security incidents

©2023 Percona | Confidential

30 of 40

Client Hosts

30

©2023 Percona | Confidential

31 of 40

Custom Tags

31

Assign custom tags to database instances if you can

Instance Type

Database Version

Configuration

©2023 Percona | Confidential

32 of 40

Query Plan

  • One Query Can have Multiple Different Query Plans
  • Sometimes it is good, in other cases it is a problem
  • Measure Query Performance by Query Plan
  • Can take action to correct query plan if this is the issue

© Percona

32

©2023 Percona | Confidential

33 of 40

Where Response Time Comes From ?

33

Data Crunching/CPU

Waits on CPU Availability

Disk IO

Row Locks

Contention

Network

©2023 Percona | Confidential

34 of 40

Other Things to Consider

© Percona

34

©2023 Percona | Confidential

35 of 40

“Bad Queries” vs Victims

  • Query might be slow because it is heavy on its own
  • Or it might be victim of other queries or their volume

© Percona

35

©2023 Percona | Confidential

36 of 40

Do not forget currently running queries

  • Response time is measured when query completes
  • You can write queries which “never” complete
  • Consider killing runaway queries and whitelisting queries which need to run long

© Percona

36

©2023 Percona | Confidential

37 of 40

Do not Ignore “Invisible”

  • Database Background Activities
  • Maintenance Operations
  • Cloud Noise

© Percona

37

©2023 Percona | Confidential

38 of 40

Avoid Biased Sampling

38

“Let’s Look only on slow queries”

Focus on Outliers

Likely to ignore queries causing most load, typical impact

©2023 Percona | Confidential

39 of 40

  • Good Luck
  • Get your query performance under control
  • Do not over-do scaling by Credit Card

© Percona

39

©2023 Percona | Confidential

40 of 40