SlideShare a Scribd company logo
7 Database Mistakes
You are making
Hello!
I am Dave Stokes
MySQL Community Manager
Slides for this talk online at
slideshare.net/davidmstokes
2
1. Backing up data != Safety
I do not care about your cron job, cute scripts, or any of that if what
you backup can not be used!
Backups are vital
Use mysqldump/mysqlpump, Xtrabackup, MySQL Enterprise Backup, etcetera
Async Replication, back up down stream server
LVM Snapshots
Something else in house
DO ALL THE ABOVE AND LOOK FOR MORE!!!
4
But restoration is more vital
Be able to restore:
β€’ Entire server
β€’ Schema
β€’ Table
β€’ Row
And make sure you are not the only one at your company with this knowledge
β€’ Document
β€’ Train
β€’ Practice!!!!
5
Paranoia is not your enemy!
6
Murphy’s
law is
optimistic
And make sure you remember to backup
β€’ Binary logs
β€’ Stored Procedures
β€’ Views
β€’ Admin databases
β€’ Encryption keys
β€’ Configuration files (my.cnf, firewall, etc.)
And store them someplace safe
(minimum two places)
7
What is your company’s record retention policy????!
β€’ Suppose your corporate lawyers ask (or are asked by a court) to show
what data you had on hand five years ago? (copy of data, database,
database software, hardware/software to support)
β€’ Are you subject to the new European rules where may have to β€˜forget’
data of individuals? What if the USA gets similar rules?
β€’ Medical/Legal/Research rules?
8
2. Keep on top of software updates
Do not skips updates
Keep your software updated!
β€’ New features
β€’ Bug Fixes
β€’ Security vulnerabilities
Good for your resume!
(ask your local FoxPro
or dBase II professional)
10
And Keep a copy of the server
backup before the upgrade
around (and a copy from right
after the upgrade)
11
3. Monitor
Those who do not study the past will pay heavily for the repeats
β€œNobody ever bitches that the
database is running too fast!”
13
Dave’s First Law of Databases
What is your database doing RIGHT NOW?!?!?!?
1. RDMS
a. Queries
b. Logins/connections
c. Index management
d. Analytics
e. Overhead
f. Logging
g. Replication
2. Hardware
A. RAM
B. Buffers
C. Logins
D. Network traffic
E. Logging
F. Paging
G. Disk I/O
H. Overhead
14
Big concept
Does your paycheck and/or the paycheck of your
boss depend on a reliable database? Then make
sure you know what the #%@$ is is doing!!
15
Options
Enterprise Level
MySQL Enterprise Manager, Percona Manager, Solar Winds, etc
- Yes, they cost money
Instance Level
MySQL Workbench
PHPMyAdmin (Say hi to the Script Kiddies)
If you do decide to β€˜roll your own’ you are not spending your time wisely. Your
time has value.
16
Options
Enterprise Level
MySQL Enterprise Manager, Percona Manager, Solar Winds, etc
- Yes, they cost money
Instance Level
MySQL Workbench
PHPMyAdmin (Say hi to the Script Kiddies)
If you do decide to β€˜roll your own’ you are not spending your time wisely. Your
time has value. If you do roll your own do you do your own dentistry?
17
4. Understand User Authentication
18
MySQL
Authentication is
Promiscuous
It is VERY easy to have the
same username with different
hosts (they are a pair) AND
different permissions for each of
those pairs!
19
Connection flow
β€’ $mysql –u joe –p -> sent to server
β€’ Server checks to see if host is okay to connect
β€’ Server may prompt for authentication string
β€’ User and auth string checked
β€’ Is user over resource limits?
β€’ Connection established -> prompt set to user
20
How you can get different privileges for β€˜the same’ account
Joe @ 172.12.10.x
Read, write, update, delete
Joe @ 10.10.x.x
Read, write, update, delete, drop
Joe @ %
Everything!!
21
Joe’s developer account when he
joined the company
Joe’s account for use in the data
center
Joe’s account created at 3AM
fighting problems with legacy
application
How you can get different privileges for β€˜the same’ account
Joe @ 172.12.10.x
Read, write, update, delete
Joe @ 10.10.x.x
Read, write, update, delete, drop
Joe @ %
EVERYTHING!!
22
Joe’s developer account when he
joined the company
Joe’s account for use in the data
center
Joe’s account created at 3AM
fighting problems with legacy
application
The Mysql daemon look
for the most generous
match First!!!!
Big concept
So someone guesses Joe’s password is β€˜JoeISNumber1’ from outside
the 10.10 and 172.12 networks and ends up with privs for
EVERYTHING!!
23
5. Indexes -> too many, too few
24
Big concept
The cost based optimizer determines the cheapest
way to return the requested data, sort of like a
GPS, based on past query statistics and available
indexes.
And like a GPS it can be fooled
And may not have latest
information.
25
Full Table Scan
A Full Table Scan is when every row
of a table (file) needs to be read to
see if it match the search criteria.
SELECT name
FROM city WHERE
District='Texas';
There is no INDEX on the District
column
26
Index Scan
An index lets you go directly to the
matching record(s).
SELECT name
FROM city WHERE
CountryCode='USA';
There is an INDEX on the
CountryCode column so it does not
need to read all the rows!
27
So let’s index every column!!!!
28
Indexes should be considered a parallel table to your data table.
So each time you add, delete, or modify an indexed column, that parallel table
has to be updated TOO!
That overhead is expensive!!!
And the more options you have for indexes, the more the optimizer has to
consider -- That adds a factorial to the complexity for each possible index
29
Why you do not want to index every column!!
Indexing options
1. Compound indexes use more than one column
a. A Year-Month-Date index works for Y-M-D, Y-M, and Y
b. But not for D or M-D (leftmost only!)
2. For β€˜multiple short’ columns, consider hashes
a.SELECT * FROM tbl_name WHERE
hash_col=MD5(CONCAT(val1,val2))
AND col1=val1 AND col2=val2;
1. Optimizer Hints
a. Comments in your query to direct optimizer
b.SELECT /*+ JOIN_ORDER(t1, t2) JOIN_PREFIX(t2, t1) */ ... FROM t1, t2;
30
Indexing options
1. Compound indexes use more than one column
1. A Year-Month-Date index works for Y-M-D, Y-M, and Y
2. But not for D or M-D (leftmost only!)
2. Index prefix of column for wide columns
3. For β€˜multiple short’ columns, consider hashes
1. SELECT * FROM tbl_name WHERE
hash_col=MD5(CONCAT(val1,val2))
AND col1=val1 AND col2=val2;
4. Optimizer Hints
1. Comments in your query to direct optimizer
2. SELECT /*+ JOIN_ORDER(t1, t2) JOIN_PREFIX(t2, t1) */ ... FROM t1, t2;
31
Sys Schema -- Performance statistics
You can use MySQL Workbench to access the SYS Schema to see a list of
UNUSED indexes
β€’ Do not look at after a reboot as there will not be enough usage data
collected
β€’ Make sure it is not a rarely used index that is not vital at quarter or
year end
32
Histograms - Like Indexes without the overhead (for some data)
A histogram is an accurate representation of the distribution of numerical
data. For databases, a histogram is an approximation of the data
distribution within a specific column.
33
Creating a histogram
mysql> ANALYZE TABLE customer UPDATE HISTOGRAM ON c_mktsegment WITH 1024 BUCKETS;
+---------------+-----------+----------+---------------------------------------------------------+
| Table | Op | Msg_type | Msg_text |
+---------------+-----------+----------+---------------------------------------------------------+
| dbt3.customer | histogram | status | Histogram statistics created for column 'c_mktsegment'. |
+---------------+-----------+----------+---------------------------------------------------------+
34
Creating a histogram
mysql> ANALYZE TABLE customer UPDATE HISTOGRAM ON c_mktsegment WITH 1024 BUCKETS;
+---------------+-----------+----------+---------------------------------------------------------+
| Table | Op | Msg_type | Msg_text |
+---------------+-----------+----------+---------------------------------------------------------+
| dbt3.customer | histogram | status | Histogram statistics created for column 'c_mktsegment'. |
+---------------+-----------+----------+---------------------------------------------------------+
35
Do you know the cardinality of your indexes?
Hit rate?
Active data set size?
Invisible indexes
Invisible indexes make it possible to test the effect of removing an index on
query performance, without making a destructive change that must be
undone should the index turn out to be required. Dropping and re-adding an
index can be expensive for a large table, whereas making it invisible and
visible are fast, in-place operations.
ALTER TABLE t1 ALTER INDEX i_idx INVISIBLE;
ALTER TABLE t1 ALTER INDEX i_idx VISIBLE;
36
6. Learn to Optimize Queries
37
Query optimization is a skill, not a
black art. Even if you use an
ORM (lazy), you need to learn
how to use EXPLAIN and read a
query plan.
38
1. Buy a copy of High Performance MySQL and/or the MySQL 5.0
Certification Guide and read sections on query optimizations
a. Both are dated but valuable
2. Learn to use VISUAL EXPLAIN from MySQL Workbench
3. Read DAILY your slow query log
a. Not all slow queries are bad (monthly report)
b. A query that was running well that now shows up on the slow
query log is BAD
4. Optimize the most frequently run queries FIRST
39
Best ways to learn how to optimize queries
7. Use JSON columns
40
Oracle, SQL Server, MySQL, PostgreSQL* have added JSON data types
● PG actually added two JSON data types
Vendors have added Native JSON data type
Mutability
JSON data type let you have a place
store rapidly evolving or undetermined
data.
JSON Data Types are extremely useful!
Replace many-to-many joins
If you have to do repeated index-
lookup/data-dives for β€˜stub’ data
consider refactoring that data into
a JSON column
42
MySQl Document Store
β€’ New API – The X Devapi
β€’ New Protocol
β€’ Allows you to use MySQL as a NoSQL JSON Document Store
β€’ No need to set up relational tables, indexes, or normalize data
β€’ Emphasis on CRUD – can use database before having β€˜perfect’
knowledge of the data
β€’ Easily mutable
β€’ Works with relational tables too (mix & match)
43
JSON_Table
THE JSON_TABLE is a function that lets you temporarily turn unstructured JSON
data into a relational table for processing with SQL commands
select country_name,
IndyYear from countryinfo,
json_table(doc,"$" columns
(country_name char(20) path "$.Name",
IndyYear int path "$.IndepYear")) as stuff
where IndyYear > 1992;
+----------------+----------+
| country_name | IndyYear |
+----------------+----------+
| Czech Republic | 1993 |
| Eritrea | 1993 |
| Palau | 1994 |
| Slovakia | 1993 |
+----------------+----------+
44
Generated Columns from JSON data
If you find some of that JSON data is needed to be
used in SQL searches then extract that information
into its own column using a Generated Column
CREATE TABLE stuff
(c JSON,
g INT GENERATED ALWAYS AS (c->"$.id")),
INDEX i (g))
;
45
Please buy my book
If you are interested in using the JSON
data type with MySQL but find the
documentation hard to understand or
are just looking for a compact reference
guide, then you need my book!
46
Thanks
David.stokes @oracle.com
@stoker
elephantdolphin.blogspot.com
slidehsare.net./davidmstokes
47

More Related Content

PDF
Five Database Mistakes and how to fix them -- Confoo Vancouver
PDF
Storage Methods for Nonstandard Data Patterns
PDF
Oracle in-Memory Column Store for BI
PDF
Testing Delphix: easy data virtualization
PDF
PostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
PDF
Exadata X3 in action: Measuring Smart Scan efficiency with AWR
PDF
Managing terabytes: When PostgreSQL gets big
PDF
How to Fine-Tune Performance Using Amazon Redshift
Five Database Mistakes and how to fix them -- Confoo Vancouver
Storage Methods for Nonstandard Data Patterns
Oracle in-Memory Column Store for BI
Testing Delphix: easy data virtualization
PostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
Exadata X3 in action: Measuring Smart Scan efficiency with AWR
Managing terabytes: When PostgreSQL gets big
How to Fine-Tune Performance Using Amazon Redshift

What's hot (17)

PDF
Scaling MySQL -- Swanseacon.co.uk
PDF
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
PPTX
ExtBase workshop
PPTX
Row Level Security in databases advanced edition
PPTX
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
PDF
Percona Live 2012PPT: MySQL Query optimization
PDF
MariaDB Optimizer
PDF
Dbvisit replicate: logical replication made easy
PPTX
Faceted search with Oracle InMemory option
PPTX
Scaling sql server 2014 parallel insert
PPT
Myth busters - performance tuning 103 2008
PDF
Why PostgreSQL for Analytics Infrastructure (DW)?
PPTX
Database Performance Tuning
PDF
MySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
PPTX
Summary of "Google's Big Table" at nosql summer reading in Tokyo
PPT
Bigtable
PDF
MySQL 8.0 Features -- Oracle CodeOne 2019, All Things Open 2019
Scaling MySQL -- Swanseacon.co.uk
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
ExtBase workshop
Row Level Security in databases advanced edition
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
Percona Live 2012PPT: MySQL Query optimization
MariaDB Optimizer
Dbvisit replicate: logical replication made easy
Faceted search with Oracle InMemory option
Scaling sql server 2014 parallel insert
Myth busters - performance tuning 103 2008
Why PostgreSQL for Analytics Infrastructure (DW)?
Database Performance Tuning
MySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
Summary of "Google's Big Table" at nosql summer reading in Tokyo
Bigtable
MySQL 8.0 Features -- Oracle CodeOne 2019, All Things Open 2019
Ad

Similar to 7 Database Mistakes YOU Are Making -- Linuxfest Northwest 2019 (20)

PDF
Silicon Valley Code Camp 2015 - Advanced MongoDB - The Sequel
PPTX
Boosting the Performance of your Rails Apps
PDF
[DBA]_HiramFleitas_SQL_PASS_Summit_2017_Summary
PDF
Storage Systems For Scalable systems
PPT
Designing, Building, and Maintaining Large Cubes using Lessons Learned
PPTX
What Your Database Query is Really Doing
PDF
MongoDB: What, why, when
PDF
The Proper Care and Feeding of MySQL Databases
PPTX
How to Achieve Scale with MongoDB
PDF
Silicon Valley Code Camp 2014 - Advanced MongoDB
PPTX
Machine Learning with ML.NET and Azure - Andy Cross
PPTX
http://www.hfadeel.com/Blog/?p=151
Β 
ODP
Vote NO for MySQL
PDF
MongoDB World 2019: Finding the Right MongoDB Atlas Cluster Size: Does This I...
PPTX
Ten query tuning techniques every SQL Server programmer should know
PDF
Taming the shrew Power BI
PDF
DATABASE AUTOMATION with Thousands of database, monitoring and backup
PDF
MySQL 8 Server Optimization Swanseacon 2018
PPT
7. Key-Value Databases: In Depth
PPTX
SQL Server 2008 Development for Programmers
Silicon Valley Code Camp 2015 - Advanced MongoDB - The Sequel
Boosting the Performance of your Rails Apps
[DBA]_HiramFleitas_SQL_PASS_Summit_2017_Summary
Storage Systems For Scalable systems
Designing, Building, and Maintaining Large Cubes using Lessons Learned
What Your Database Query is Really Doing
MongoDB: What, why, when
The Proper Care and Feeding of MySQL Databases
How to Achieve Scale with MongoDB
Silicon Valley Code Camp 2014 - Advanced MongoDB
Machine Learning with ML.NET and Azure - Andy Cross
http://www.hfadeel.com/Blog/?p=151
Β 
Vote NO for MySQL
MongoDB World 2019: Finding the Right MongoDB Atlas Cluster Size: Does This I...
Ten query tuning techniques every SQL Server programmer should know
Taming the shrew Power BI
DATABASE AUTOMATION with Thousands of database, monitoring and backup
MySQL 8 Server Optimization Swanseacon 2018
7. Key-Value Databases: In Depth
SQL Server 2008 Development for Programmers
Ad

More from Dave Stokes (20)

PDF
Json within a relational database
PDF
Database basics for new-ish developers -- All Things Open October 18th 2021
PDF
Php & my sql - how do pdo, mysq-li, and x devapi do what they do
PDF
Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...
PDF
MySQL 8.0 New Features -- September 27th presentation for Open Source Summit
PDF
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
PDF
Open Source World June '21 -- JSON Within a Relational Database
PDF
Dutch PHP Conference 2021 - MySQL Indexes and Histograms
PPTX
Validating JSON -- Percona Live 2021 presentation
PDF
Midwest PHP Presentation - New MSQL Features
PDF
Data Love Conference - Window Functions for Database Analytics
PPTX
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
PPTX
Confoo 2021 -- MySQL New Features
PPTX
Confoo 2021 - MySQL Indexes & Histograms
PDF
Datacon LA - MySQL without the SQL - Oh my!
PDF
MySQL Replication Update - DEbconf 2020 presentation
PDF
MySQL 8.0 Operational Changes
PPTX
cPanel now supports MySQL 8.0 - My Top Seven Features
PPTX
A Step by Step Introduction to the MySQL Document Store
PPTX
Discover The Power of NoSQL + MySQL with MySQL
Json within a relational database
Database basics for new-ish developers -- All Things Open October 18th 2021
Php & my sql - how do pdo, mysq-li, and x devapi do what they do
Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...
MySQL 8.0 New Features -- September 27th presentation for Open Source Summit
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
Open Source World June '21 -- JSON Within a Relational Database
Dutch PHP Conference 2021 - MySQL Indexes and Histograms
Validating JSON -- Percona Live 2021 presentation
Midwest PHP Presentation - New MSQL Features
Data Love Conference - Window Functions for Database Analytics
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
Confoo 2021 -- MySQL New Features
Confoo 2021 - MySQL Indexes & Histograms
Datacon LA - MySQL without the SQL - Oh my!
MySQL Replication Update - DEbconf 2020 presentation
MySQL 8.0 Operational Changes
cPanel now supports MySQL 8.0 - My Top Seven Features
A Step by Step Introduction to the MySQL Document Store
Discover The Power of NoSQL + MySQL with MySQL

Recently uploaded (20)

PPTX
Introduction to Information and Communication Technology
PPTX
presentation_pfe-universite-molay-seltan.pptx
PDF
An introduction to the IFRS (ISSB) Stndards.pdf
PDF
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
PPTX
Slides PPTX World Game (s) Eco Economic Epochs.pptx
PPTX
E -tech empowerment technologies PowerPoint
DOCX
Unit-3 cyber security network security of internet system
PPTX
Digital Literacy And Online Safety on internet
PDF
The Internet -By the Numbers, Sri Lanka Edition
Β 
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PPTX
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
PPT
tcp ip networks nd ip layering assotred slides
PPTX
Introuction about WHO-FIC in ICD-10.pptx
PPTX
PptxGenJS_Demo_Chart_20250317130215833.pptx
PPTX
Internet___Basics___Styled_ presentation
PPTX
Funds Management Learning Material for Beg
PDF
The New Creative Director: How AI Tools for Social Media Content Creation Are...
PDF
RPKI Status Update, presented by Makito Lay at IDNOG 10
Β 
PDF
Testing WebRTC applications at scale.pdf
Introduction to Information and Communication Technology
presentation_pfe-universite-molay-seltan.pptx
An introduction to the IFRS (ISSB) Stndards.pdf
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
Slides PPTX World Game (s) Eco Economic Epochs.pptx
E -tech empowerment technologies PowerPoint
Unit-3 cyber security network security of internet system
Digital Literacy And Online Safety on internet
The Internet -By the Numbers, Sri Lanka Edition
Β 
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
Tenda Login Guide: Access Your Router in 5 Easy Steps
tcp ip networks nd ip layering assotred slides
Introuction about WHO-FIC in ICD-10.pptx
PptxGenJS_Demo_Chart_20250317130215833.pptx
Internet___Basics___Styled_ presentation
Funds Management Learning Material for Beg
The New Creative Director: How AI Tools for Social Media Content Creation Are...
RPKI Status Update, presented by Makito Lay at IDNOG 10
Β 
Testing WebRTC applications at scale.pdf

7 Database Mistakes YOU Are Making -- Linuxfest Northwest 2019

  • 2. Hello! I am Dave Stokes MySQL Community Manager Slides for this talk online at slideshare.net/davidmstokes 2
  • 3. 1. Backing up data != Safety I do not care about your cron job, cute scripts, or any of that if what you backup can not be used!
  • 4. Backups are vital Use mysqldump/mysqlpump, Xtrabackup, MySQL Enterprise Backup, etcetera Async Replication, back up down stream server LVM Snapshots Something else in house DO ALL THE ABOVE AND LOOK FOR MORE!!! 4
  • 5. But restoration is more vital Be able to restore: β€’ Entire server β€’ Schema β€’ Table β€’ Row And make sure you are not the only one at your company with this knowledge β€’ Document β€’ Train β€’ Practice!!!! 5
  • 6. Paranoia is not your enemy! 6 Murphy’s law is optimistic
  • 7. And make sure you remember to backup β€’ Binary logs β€’ Stored Procedures β€’ Views β€’ Admin databases β€’ Encryption keys β€’ Configuration files (my.cnf, firewall, etc.) And store them someplace safe (minimum two places) 7
  • 8. What is your company’s record retention policy????! β€’ Suppose your corporate lawyers ask (or are asked by a court) to show what data you had on hand five years ago? (copy of data, database, database software, hardware/software to support) β€’ Are you subject to the new European rules where may have to β€˜forget’ data of individuals? What if the USA gets similar rules? β€’ Medical/Legal/Research rules? 8
  • 9. 2. Keep on top of software updates Do not skips updates
  • 10. Keep your software updated! β€’ New features β€’ Bug Fixes β€’ Security vulnerabilities Good for your resume! (ask your local FoxPro or dBase II professional) 10
  • 11. And Keep a copy of the server backup before the upgrade around (and a copy from right after the upgrade) 11
  • 12. 3. Monitor Those who do not study the past will pay heavily for the repeats
  • 13. β€œNobody ever bitches that the database is running too fast!” 13 Dave’s First Law of Databases
  • 14. What is your database doing RIGHT NOW?!?!?!? 1. RDMS a. Queries b. Logins/connections c. Index management d. Analytics e. Overhead f. Logging g. Replication 2. Hardware A. RAM B. Buffers C. Logins D. Network traffic E. Logging F. Paging G. Disk I/O H. Overhead 14
  • 15. Big concept Does your paycheck and/or the paycheck of your boss depend on a reliable database? Then make sure you know what the #%@$ is is doing!! 15
  • 16. Options Enterprise Level MySQL Enterprise Manager, Percona Manager, Solar Winds, etc - Yes, they cost money Instance Level MySQL Workbench PHPMyAdmin (Say hi to the Script Kiddies) If you do decide to β€˜roll your own’ you are not spending your time wisely. Your time has value. 16
  • 17. Options Enterprise Level MySQL Enterprise Manager, Percona Manager, Solar Winds, etc - Yes, they cost money Instance Level MySQL Workbench PHPMyAdmin (Say hi to the Script Kiddies) If you do decide to β€˜roll your own’ you are not spending your time wisely. Your time has value. If you do roll your own do you do your own dentistry? 17
  • 18. 4. Understand User Authentication 18
  • 19. MySQL Authentication is Promiscuous It is VERY easy to have the same username with different hosts (they are a pair) AND different permissions for each of those pairs! 19
  • 20. Connection flow β€’ $mysql –u joe –p -> sent to server β€’ Server checks to see if host is okay to connect β€’ Server may prompt for authentication string β€’ User and auth string checked β€’ Is user over resource limits? β€’ Connection established -> prompt set to user 20
  • 21. How you can get different privileges for β€˜the same’ account Joe @ 172.12.10.x Read, write, update, delete Joe @ 10.10.x.x Read, write, update, delete, drop Joe @ % Everything!! 21 Joe’s developer account when he joined the company Joe’s account for use in the data center Joe’s account created at 3AM fighting problems with legacy application
  • 22. How you can get different privileges for β€˜the same’ account Joe @ 172.12.10.x Read, write, update, delete Joe @ 10.10.x.x Read, write, update, delete, drop Joe @ % EVERYTHING!! 22 Joe’s developer account when he joined the company Joe’s account for use in the data center Joe’s account created at 3AM fighting problems with legacy application The Mysql daemon look for the most generous match First!!!!
  • 23. Big concept So someone guesses Joe’s password is β€˜JoeISNumber1’ from outside the 10.10 and 172.12 networks and ends up with privs for EVERYTHING!! 23
  • 24. 5. Indexes -> too many, too few 24
  • 25. Big concept The cost based optimizer determines the cheapest way to return the requested data, sort of like a GPS, based on past query statistics and available indexes. And like a GPS it can be fooled And may not have latest information. 25
  • 26. Full Table Scan A Full Table Scan is when every row of a table (file) needs to be read to see if it match the search criteria. SELECT name FROM city WHERE District='Texas'; There is no INDEX on the District column 26
  • 27. Index Scan An index lets you go directly to the matching record(s). SELECT name FROM city WHERE CountryCode='USA'; There is an INDEX on the CountryCode column so it does not need to read all the rows! 27
  • 28. So let’s index every column!!!! 28
  • 29. Indexes should be considered a parallel table to your data table. So each time you add, delete, or modify an indexed column, that parallel table has to be updated TOO! That overhead is expensive!!! And the more options you have for indexes, the more the optimizer has to consider -- That adds a factorial to the complexity for each possible index 29 Why you do not want to index every column!!
  • 30. Indexing options 1. Compound indexes use more than one column a. A Year-Month-Date index works for Y-M-D, Y-M, and Y b. But not for D or M-D (leftmost only!) 2. For β€˜multiple short’ columns, consider hashes a.SELECT * FROM tbl_name WHERE hash_col=MD5(CONCAT(val1,val2)) AND col1=val1 AND col2=val2; 1. Optimizer Hints a. Comments in your query to direct optimizer b.SELECT /*+ JOIN_ORDER(t1, t2) JOIN_PREFIX(t2, t1) */ ... FROM t1, t2; 30
  • 31. Indexing options 1. Compound indexes use more than one column 1. A Year-Month-Date index works for Y-M-D, Y-M, and Y 2. But not for D or M-D (leftmost only!) 2. Index prefix of column for wide columns 3. For β€˜multiple short’ columns, consider hashes 1. SELECT * FROM tbl_name WHERE hash_col=MD5(CONCAT(val1,val2)) AND col1=val1 AND col2=val2; 4. Optimizer Hints 1. Comments in your query to direct optimizer 2. SELECT /*+ JOIN_ORDER(t1, t2) JOIN_PREFIX(t2, t1) */ ... FROM t1, t2; 31
  • 32. Sys Schema -- Performance statistics You can use MySQL Workbench to access the SYS Schema to see a list of UNUSED indexes β€’ Do not look at after a reboot as there will not be enough usage data collected β€’ Make sure it is not a rarely used index that is not vital at quarter or year end 32
  • 33. Histograms - Like Indexes without the overhead (for some data) A histogram is an accurate representation of the distribution of numerical data. For databases, a histogram is an approximation of the data distribution within a specific column. 33
  • 34. Creating a histogram mysql> ANALYZE TABLE customer UPDATE HISTOGRAM ON c_mktsegment WITH 1024 BUCKETS; +---------------+-----------+----------+---------------------------------------------------------+ | Table | Op | Msg_type | Msg_text | +---------------+-----------+----------+---------------------------------------------------------+ | dbt3.customer | histogram | status | Histogram statistics created for column 'c_mktsegment'. | +---------------+-----------+----------+---------------------------------------------------------+ 34
  • 35. Creating a histogram mysql> ANALYZE TABLE customer UPDATE HISTOGRAM ON c_mktsegment WITH 1024 BUCKETS; +---------------+-----------+----------+---------------------------------------------------------+ | Table | Op | Msg_type | Msg_text | +---------------+-----------+----------+---------------------------------------------------------+ | dbt3.customer | histogram | status | Histogram statistics created for column 'c_mktsegment'. | +---------------+-----------+----------+---------------------------------------------------------+ 35 Do you know the cardinality of your indexes? Hit rate? Active data set size?
  • 36. Invisible indexes Invisible indexes make it possible to test the effect of removing an index on query performance, without making a destructive change that must be undone should the index turn out to be required. Dropping and re-adding an index can be expensive for a large table, whereas making it invisible and visible are fast, in-place operations. ALTER TABLE t1 ALTER INDEX i_idx INVISIBLE; ALTER TABLE t1 ALTER INDEX i_idx VISIBLE; 36
  • 37. 6. Learn to Optimize Queries 37
  • 38. Query optimization is a skill, not a black art. Even if you use an ORM (lazy), you need to learn how to use EXPLAIN and read a query plan. 38
  • 39. 1. Buy a copy of High Performance MySQL and/or the MySQL 5.0 Certification Guide and read sections on query optimizations a. Both are dated but valuable 2. Learn to use VISUAL EXPLAIN from MySQL Workbench 3. Read DAILY your slow query log a. Not all slow queries are bad (monthly report) b. A query that was running well that now shows up on the slow query log is BAD 4. Optimize the most frequently run queries FIRST 39 Best ways to learn how to optimize queries
  • 40. 7. Use JSON columns 40
  • 41. Oracle, SQL Server, MySQL, PostgreSQL* have added JSON data types ● PG actually added two JSON data types Vendors have added Native JSON data type
  • 42. Mutability JSON data type let you have a place store rapidly evolving or undetermined data. JSON Data Types are extremely useful! Replace many-to-many joins If you have to do repeated index- lookup/data-dives for β€˜stub’ data consider refactoring that data into a JSON column 42
  • 43. MySQl Document Store β€’ New API – The X Devapi β€’ New Protocol β€’ Allows you to use MySQL as a NoSQL JSON Document Store β€’ No need to set up relational tables, indexes, or normalize data β€’ Emphasis on CRUD – can use database before having β€˜perfect’ knowledge of the data β€’ Easily mutable β€’ Works with relational tables too (mix & match) 43
  • 44. JSON_Table THE JSON_TABLE is a function that lets you temporarily turn unstructured JSON data into a relational table for processing with SQL commands select country_name, IndyYear from countryinfo, json_table(doc,"$" columns (country_name char(20) path "$.Name", IndyYear int path "$.IndepYear")) as stuff where IndyYear > 1992; +----------------+----------+ | country_name | IndyYear | +----------------+----------+ | Czech Republic | 1993 | | Eritrea | 1993 | | Palau | 1994 | | Slovakia | 1993 | +----------------+----------+ 44
  • 45. Generated Columns from JSON data If you find some of that JSON data is needed to be used in SQL searches then extract that information into its own column using a Generated Column CREATE TABLE stuff (c JSON, g INT GENERATED ALWAYS AS (c->"$.id")), INDEX i (g)) ; 45
  • 46. Please buy my book If you are interested in using the JSON data type with MySQL but find the documentation hard to understand or are just looking for a compact reference guide, then you need my book! 46

Editor's Notes

  • #2: Started with a list of forty things that you could be doing wrong and cull it down to seven
  • #3: I started programming in FORTRAN on punch cards on a Dec KL-1091 running the Tops-10 Operating system
  • #4: Just performing backups is not enough