SlideShare a Scribd company logo
1 of 23
Download to read offline
1 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Writing Better
MySQL Queries for
Beginners
Dave Stokes
MySQL Community Manager

David.Stokes@Oracle.com @stoker
The following is intended to outline our general product direction. It is
     intended for information purposes only, and may not be incorporated into
     any contract. It is not a commitment to deliver any material, code, or
     functionality, and should not be relied upon in making purchasing
     decisions. The development, release, and timing of any features or
     functionality described for Oracle’s products remains at the sole
     discretion of Oracle.




3 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Agenda


        SQL history and offities
        Data Storage – why all INTs should not be BIGINT, etc.
        Table Design
        Indexes – why to index columns on the right side of a
         WHERE
        QUERY monitoring and optimization
        Q/A

4 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
SQL


     Structured Query Language
     Based on relational algebra and tuple relational calculus
     Two parts
                                  –                Data definition language
                                  –                Data manipulation language
     SELECT name, address
     FROM customers
     WHERE age > 21 AND state = 'CA';

5 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Cod and Date, er, E.F. Codd C.J. Date




6 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
NULL, three-valued logic, and
                                                                         NULLs are used to show
                                                                           no value but have the
                                                                           side effect of making
                                                                           just about everything
                                                                           from enums to
                                                                           indexes much more
                                                                           difficult to process
                                                                           and optimize. Avoid
                                                                           as much as you can!




7 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Slide to check if audience is awake




8 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Data Types


     MySQL has data types for integers,
       decimals, dates, strings, BLOBS
       (not binary large object but like
       'The Thing that Ate Cincinatti').


     Note that character sets can affect
       size of character data – Latin1
       'A' is one char, thee in UTF8


9 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Size matters!



      Many programmers use the biggest
      Possible data type 'just in case' but it
      Wastes space, bandwidth, and time.
      Is a Wordpress blog really going to have
      9,223,372,936,054,775,807
      Entries?!?!?


10 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Select just what you need for SPEED!


      Bad:                                                                Better:
                     SELECT *                                                 SELECT fname, lname, email
                     FROM customers                                           FROM customers


      Let say you need to spam your customers, query on the left will use the *
         wild card to return all columns from the customers table. If this table
         has many columns, there is a lot of extra data movement (disk,
         memory, network, buffers, etc.) when all you need is three columns.


11 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Third normal form


                  A memorable statement of Codd's definition of 3NF, paralleling the
                  traditional pledge to give true evidence in a court of law, was given by
                    Bill Kent: "[Every] non-key [attribute] must provide a fact about the
                   key, the whole key, and nothing but the key."[6] A common variation
                       supplements this definition with the oath: "so help me Codd".

        Kent, William. "A Simple Guide to Five Normal Forms in Relational Database Theory", Communications
                                        of the ACM 26 (2), Feb. 1983, pp. 120–125
                                              Diehr, George. Database Management (Scott, Foresman, 1989), p. 331.


12 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Third normal – simplified (probably too much)


      Each row has a key and everything else on
       the row is unique to that key
                     – very simplified but a decent guideline




13 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
INDEXes


      MySQL Manual 8.3.1. How MySQL Uses Indexes
      Indexes are used to find rows with specific column values quickly.
         Without an index, MySQL must begin with the first row and then read
         through the entire table to find the relevant rows. The larger the table,
         the more this costs. If the table has an index for the columns in
         question, MySQL can quickly determine the position to seek to in the
         middle of the data file without having to look at all the data. If a table
         has 1,000 rows, this is at least 100 times faster than reading
         sequentially.

14 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Sheeri's Session after this session


      Are You Getting the Best Out of Your MySQL Indexes
      – Same room
      – 11:30 AM




15 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
INDEX rough rules


      1. Non null, unique, short as possible
      2. INDEX columns on right side of WHERE in a query (very, very crude)
      3. After #2, go out and really learn indexes!
      4. Composite index are your friend
      5. Indexes will not help when you have to perform a full table scan
      6. Learn how to use EXPLAIN




16 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Monitoring


      There are tools to help you gauge efficiency – MySQL Workbench,
        MySQL Enterprise Monitor, PHPmyAdmin, etc that can give you a
        clue. But you need a good understanding of query optimization first.
        But be aware they are there!




17 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Resources
                                                                           'Ping' Lenka
                                                                             or Dave
                                                                                For
                                                                             Anything
         Find Info on MySQL.Com                                           MySQL User
                                                                          Group Related
         EMEA contact – lenka.kasparova@oracle.com

         North America contact – david.stokes@oracle.com

         Planet.MySQL.Com – Best MySQL Blogs

         Los Angeles MySQL User Group



18 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Virtual Developer Day March 12
         Virtual Developer Day: MySQL is a one-stop shop for you to learn all the
         essential MySQL skills. With a combination of presentations and hands-on
         lab experience, you’ll have the opportunity to practice in your own
         environment and sharpen your skills to:
            • Develop your new applications cost-effectively using MySQL
            • Improve performance of your existing MySQL databases
            • Manage your MySQL environment more efficiently

         https://oracle.6connex.com/portal/mysql/login/?
         langR=en_US&mcc=launch


19 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Review



      1. SQL is odd, get used to it.
      2. Use the right size for your data
      3. Select what you need for speed
      4. Use 3NF
      5. Learn indexes, learn to monitor instances



20 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Q&A
             Slides at http://slideshare.net/davestokes/presentations



21 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
22 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
23 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

More Related Content

Viewers also liked

Scaling MySQl 1 to N Servers -- Los Angelese MySQL User Group Feb 2014
Scaling MySQl 1 to N Servers -- Los Angelese MySQL User Group Feb 2014Scaling MySQl 1 to N Servers -- Los Angelese MySQL User Group Feb 2014
Scaling MySQl 1 to N Servers -- Los Angelese MySQL User Group Feb 2014Dave Stokes
 
Tx lf propercareandfeedmysql
Tx lf propercareandfeedmysqlTx lf propercareandfeedmysql
Tx lf propercareandfeedmysqlDave Stokes
 
MySQL 5.7 -- SCaLE Feb 2014
MySQL 5.7 -- SCaLE Feb 2014MySQL 5.7 -- SCaLE Feb 2014
MySQL 5.7 -- SCaLE Feb 2014Dave Stokes
 
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014Dave Stokes
 
MySQL 5.7 New Features to Exploit -- PHPTek/Chicago MySQL User Group May 2014
MySQL 5.7 New Features to Exploit -- PHPTek/Chicago MySQL User Group May 2014MySQL 5.7 New Features to Exploit -- PHPTek/Chicago MySQL User Group May 2014
MySQL 5.7 New Features to Exploit -- PHPTek/Chicago MySQL User Group May 2014Dave Stokes
 
MySQL 5.6 Updates
MySQL 5.6 UpdatesMySQL 5.6 Updates
MySQL 5.6 UpdatesDave Stokes
 
Southeast Linuxfest -- MySQL User Admin Tips & Tricks
Southeast Linuxfest -- MySQL User Admin Tips & TricksSoutheast Linuxfest -- MySQL User Admin Tips & Tricks
Southeast Linuxfest -- MySQL User Admin Tips & TricksDave Stokes
 
MySql's NoSQL -- best of both worlds on the same disks
MySql's NoSQL -- best of both worlds on the same disksMySql's NoSQL -- best of both worlds on the same disks
MySql's NoSQL -- best of both worlds on the same disksDave Stokes
 

Viewers also liked (9)

Scaling MySQl 1 to N Servers -- Los Angelese MySQL User Group Feb 2014
Scaling MySQl 1 to N Servers -- Los Angelese MySQL User Group Feb 2014Scaling MySQl 1 to N Servers -- Los Angelese MySQL User Group Feb 2014
Scaling MySQl 1 to N Servers -- Los Angelese MySQL User Group Feb 2014
 
Tx lf propercareandfeedmysql
Tx lf propercareandfeedmysqlTx lf propercareandfeedmysql
Tx lf propercareandfeedmysql
 
My sql 5.6&MySQL Cluster 7.3
My sql 5.6&MySQL Cluster 7.3My sql 5.6&MySQL Cluster 7.3
My sql 5.6&MySQL Cluster 7.3
 
MySQL 5.7 -- SCaLE Feb 2014
MySQL 5.7 -- SCaLE Feb 2014MySQL 5.7 -- SCaLE Feb 2014
MySQL 5.7 -- SCaLE Feb 2014
 
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
 
MySQL 5.7 New Features to Exploit -- PHPTek/Chicago MySQL User Group May 2014
MySQL 5.7 New Features to Exploit -- PHPTek/Chicago MySQL User Group May 2014MySQL 5.7 New Features to Exploit -- PHPTek/Chicago MySQL User Group May 2014
MySQL 5.7 New Features to Exploit -- PHPTek/Chicago MySQL User Group May 2014
 
MySQL 5.6 Updates
MySQL 5.6 UpdatesMySQL 5.6 Updates
MySQL 5.6 Updates
 
Southeast Linuxfest -- MySQL User Admin Tips & Tricks
Southeast Linuxfest -- MySQL User Admin Tips & TricksSoutheast Linuxfest -- MySQL User Admin Tips & Tricks
Southeast Linuxfest -- MySQL User Admin Tips & Tricks
 
MySql's NoSQL -- best of both worlds on the same disks
MySql's NoSQL -- best of both worlds on the same disksMySql's NoSQL -- best of both worlds on the same disks
MySql's NoSQL -- best of both worlds on the same disks
 

Similar to Better sq lqueries

OUG Scotland 2014 - NoSQL and MySQL - The best of both worlds
OUG Scotland 2014 - NoSQL and MySQL - The best of both worldsOUG Scotland 2014 - NoSQL and MySQL - The best of both worlds
OUG Scotland 2014 - NoSQL and MySQL - The best of both worldsAndrew Morgan
 
Oracle mysql comparison
Oracle mysql comparisonOracle mysql comparison
Oracle mysql comparisonArun Sharma
 
Embracing Database Diversity: The New Oracle / MySQL DBA - UKOUG
Embracing Database Diversity: The New Oracle / MySQL DBA -   UKOUGEmbracing Database Diversity: The New Oracle / MySQL DBA -   UKOUG
Embracing Database Diversity: The New Oracle / MySQL DBA - UKOUGKeith Hollman
 
White Paper for OMG! Identifying and Refactoring Common SQL...
White Paper for OMG! Identifying and Refactoring Common SQL...White Paper for OMG! Identifying and Refactoring Common SQL...
White Paper for OMG! Identifying and Refactoring Common SQL...Jeff Jacobs
 
Sql server common interview questions and answers
Sql server   common interview questions and answersSql server   common interview questions and answers
Sql server common interview questions and answersKaing Menglieng
 
Oracle to MySQL 2012
Oracle to MySQL  2012 Oracle to MySQL  2012
Oracle to MySQL 2012 Marco Tusa
 
Ohio Linux Fest -- MySQL's NoSQL
Ohio Linux Fest -- MySQL's NoSQLOhio Linux Fest -- MySQL's NoSQL
Ohio Linux Fest -- MySQL's NoSQLDave Stokes
 
Santo Leto - MySQL Connect 2012 - Getting Started with Mysql Cluster
Santo Leto - MySQL Connect 2012 - Getting Started with Mysql ClusterSanto Leto - MySQL Connect 2012 - Getting Started with Mysql Cluster
Santo Leto - MySQL Connect 2012 - Getting Started with Mysql ClusterSanto Leto
 
What Your Database Query is Really Doing
What Your Database Query is Really DoingWhat Your Database Query is Really Doing
What Your Database Query is Really DoingDave Stokes
 
PhpTek Ten Things to do to make your MySQL servers Happier and Healthier
PhpTek Ten Things to do to make your MySQL servers Happier and HealthierPhpTek Ten Things to do to make your MySQL servers Happier and Healthier
PhpTek Ten Things to do to make your MySQL servers Happier and HealthierDave Stokes
 
Breakthrough performance with MySQL Cluster (2012)
Breakthrough performance with MySQL Cluster (2012)Breakthrough performance with MySQL Cluster (2012)
Breakthrough performance with MySQL Cluster (2012)Frazer Clement
 
10 sql tips to speed up your database cats whocode.com
10 sql tips to speed up your database   cats whocode.com10 sql tips to speed up your database   cats whocode.com
10 sql tips to speed up your database cats whocode.comKaing Menglieng
 
Government and Education Webinar: Simplify Your Database Performance Manageme...
Government and Education Webinar: Simplify Your Database Performance Manageme...Government and Education Webinar: Simplify Your Database Performance Manageme...
Government and Education Webinar: Simplify Your Database Performance Manageme...SolarWinds
 
MySQL's NoSQL -- Texas Linuxfest August 22nd 2015
MySQL's NoSQL  -- Texas Linuxfest August 22nd 2015MySQL's NoSQL  -- Texas Linuxfest August 22nd 2015
MySQL's NoSQL -- Texas Linuxfest August 22nd 2015Dave Stokes
 

Similar to Better sq lqueries (20)

NoSQL and MySQL
NoSQL and MySQLNoSQL and MySQL
NoSQL and MySQL
 
OUG Scotland 2014 - NoSQL and MySQL - The best of both worlds
OUG Scotland 2014 - NoSQL and MySQL - The best of both worldsOUG Scotland 2014 - NoSQL and MySQL - The best of both worlds
OUG Scotland 2014 - NoSQL and MySQL - The best of both worlds
 
No sql3 rmoug
No sql3 rmougNo sql3 rmoug
No sql3 rmoug
 
Oracle mysql comparison
Oracle mysql comparisonOracle mysql comparison
Oracle mysql comparison
 
Speed up sql
Speed up sqlSpeed up sql
Speed up sql
 
Embracing Database Diversity: The New Oracle / MySQL DBA - UKOUG
Embracing Database Diversity: The New Oracle / MySQL DBA -   UKOUGEmbracing Database Diversity: The New Oracle / MySQL DBA -   UKOUG
Embracing Database Diversity: The New Oracle / MySQL DBA - UKOUG
 
White Paper for OMG! Identifying and Refactoring Common SQL...
White Paper for OMG! Identifying and Refactoring Common SQL...White Paper for OMG! Identifying and Refactoring Common SQL...
White Paper for OMG! Identifying and Refactoring Common SQL...
 
Sql server common interview questions and answers
Sql server   common interview questions and answersSql server   common interview questions and answers
Sql server common interview questions and answers
 
Oracle to MySQL 2012
Oracle to MySQL  2012 Oracle to MySQL  2012
Oracle to MySQL 2012
 
Ohio Linux Fest -- MySQL's NoSQL
Ohio Linux Fest -- MySQL's NoSQLOhio Linux Fest -- MySQL's NoSQL
Ohio Linux Fest -- MySQL's NoSQL
 
Sql no sql
Sql no sqlSql no sql
Sql no sql
 
Santo Leto - MySQL Connect 2012 - Getting Started with Mysql Cluster
Santo Leto - MySQL Connect 2012 - Getting Started with Mysql ClusterSanto Leto - MySQL Connect 2012 - Getting Started with Mysql Cluster
Santo Leto - MySQL Connect 2012 - Getting Started with Mysql Cluster
 
Simple Way for MySQL to NoSQL
Simple Way for MySQL to NoSQLSimple Way for MySQL to NoSQL
Simple Way for MySQL to NoSQL
 
What Your Database Query is Really Doing
What Your Database Query is Really DoingWhat Your Database Query is Really Doing
What Your Database Query is Really Doing
 
PhpTek Ten Things to do to make your MySQL servers Happier and Healthier
PhpTek Ten Things to do to make your MySQL servers Happier and HealthierPhpTek Ten Things to do to make your MySQL servers Happier and Healthier
PhpTek Ten Things to do to make your MySQL servers Happier and Healthier
 
Breakthrough performance with MySQL Cluster (2012)
Breakthrough performance with MySQL Cluster (2012)Breakthrough performance with MySQL Cluster (2012)
Breakthrough performance with MySQL Cluster (2012)
 
10 sql tips to speed up your database cats whocode.com
10 sql tips to speed up your database   cats whocode.com10 sql tips to speed up your database   cats whocode.com
10 sql tips to speed up your database cats whocode.com
 
Government and Education Webinar: Simplify Your Database Performance Manageme...
Government and Education Webinar: Simplify Your Database Performance Manageme...Government and Education Webinar: Simplify Your Database Performance Manageme...
Government and Education Webinar: Simplify Your Database Performance Manageme...
 
Backpack Tools4 Sql Dev
Backpack Tools4 Sql DevBackpack Tools4 Sql Dev
Backpack Tools4 Sql Dev
 
MySQL's NoSQL -- Texas Linuxfest August 22nd 2015
MySQL's NoSQL  -- Texas Linuxfest August 22nd 2015MySQL's NoSQL  -- Texas Linuxfest August 22nd 2015
MySQL's NoSQL -- Texas Linuxfest August 22nd 2015
 

More from Dave Stokes

Json within a relational database
Json within a relational databaseJson within a relational database
Json within a relational databaseDave Stokes
 
Database basics for new-ish developers -- All Things Open October 18th 2021
Database basics for new-ish developers  -- All Things Open October 18th 2021Database basics for new-ish developers  -- All Things Open October 18th 2021
Database basics for new-ish developers -- All Things Open October 18th 2021Dave Stokes
 
Php & my sql - how do pdo, mysq-li, and x devapi do what they do
Php & my sql  - how do pdo, mysq-li, and x devapi do what they doPhp & my sql  - how do pdo, mysq-li, and x devapi do what they do
Php & my sql - how do pdo, mysq-li, and x devapi do what they doDave Stokes
 
Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...
Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...
Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...Dave Stokes
 
MySQL 8.0 New Features -- September 27th presentation for Open Source Summit
MySQL 8.0 New Features -- September 27th presentation for Open Source SummitMySQL 8.0 New Features -- September 27th presentation for Open Source Summit
MySQL 8.0 New Features -- September 27th presentation for Open Source SummitDave Stokes
 
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScriptJavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScriptDave Stokes
 
Open Source World June '21 -- JSON Within a Relational Database
Open Source World June '21 -- JSON Within a Relational DatabaseOpen Source World June '21 -- JSON Within a Relational Database
Open Source World June '21 -- JSON Within a Relational DatabaseDave Stokes
 
Dutch PHP Conference 2021 - MySQL Indexes and Histograms
Dutch PHP Conference 2021 - MySQL Indexes and HistogramsDutch PHP Conference 2021 - MySQL Indexes and Histograms
Dutch PHP Conference 2021 - MySQL Indexes and HistogramsDave Stokes
 
Validating JSON -- Percona Live 2021 presentation
Validating JSON -- Percona Live 2021 presentationValidating JSON -- Percona Live 2021 presentation
Validating JSON -- Percona Live 2021 presentationDave Stokes
 
Midwest PHP Presentation - New MSQL Features
Midwest PHP Presentation - New MSQL FeaturesMidwest PHP Presentation - New MSQL Features
Midwest PHP Presentation - New MSQL FeaturesDave Stokes
 
Data Love Conference - Window Functions for Database Analytics
Data Love Conference - Window Functions for Database AnalyticsData Love Conference - Window Functions for Database Analytics
Data Love Conference - Window Functions for Database AnalyticsDave Stokes
 
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...Dave Stokes
 
Confoo 2021 -- MySQL New Features
Confoo 2021 -- MySQL New FeaturesConfoo 2021 -- MySQL New Features
Confoo 2021 -- MySQL New FeaturesDave Stokes
 
Confoo 2021 - MySQL Indexes & Histograms
Confoo 2021 - MySQL Indexes & HistogramsConfoo 2021 - MySQL Indexes & Histograms
Confoo 2021 - MySQL Indexes & HistogramsDave Stokes
 
Datacon LA - MySQL without the SQL - Oh my!
Datacon LA - MySQL without the SQL - Oh my! Datacon LA - MySQL without the SQL - Oh my!
Datacon LA - MySQL without the SQL - Oh my! Dave Stokes
 
MySQL Replication Update - DEbconf 2020 presentation
MySQL Replication Update - DEbconf 2020 presentationMySQL Replication Update - DEbconf 2020 presentation
MySQL Replication Update - DEbconf 2020 presentationDave Stokes
 
MySQL 8.0 Operational Changes
MySQL 8.0 Operational ChangesMySQL 8.0 Operational Changes
MySQL 8.0 Operational ChangesDave Stokes
 
cPanel now supports MySQL 8.0 - My Top Seven Features
cPanel now supports MySQL 8.0 - My Top Seven FeaturescPanel now supports MySQL 8.0 - My Top Seven Features
cPanel now supports MySQL 8.0 - My Top Seven FeaturesDave Stokes
 
A Step by Step Introduction to the MySQL Document Store
A Step by Step Introduction to the MySQL Document StoreA Step by Step Introduction to the MySQL Document Store
A Step by Step Introduction to the MySQL Document StoreDave Stokes
 
Discover The Power of NoSQL + MySQL with MySQL
Discover The Power of NoSQL + MySQL with MySQLDiscover The Power of NoSQL + MySQL with MySQL
Discover The Power of NoSQL + MySQL with MySQLDave Stokes
 

More from Dave Stokes (20)

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

Better sq lqueries

  • 1. 1 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 2. Writing Better MySQL Queries for Beginners Dave Stokes MySQL Community Manager David.Stokes@Oracle.com @stoker
  • 3. The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 3 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 4. Agenda  SQL history and offities  Data Storage – why all INTs should not be BIGINT, etc.  Table Design  Indexes – why to index columns on the right side of a WHERE  QUERY monitoring and optimization  Q/A 4 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 5. SQL Structured Query Language Based on relational algebra and tuple relational calculus Two parts – Data definition language – Data manipulation language SELECT name, address FROM customers WHERE age > 21 AND state = 'CA'; 5 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 6. Cod and Date, er, E.F. Codd C.J. Date 6 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 7. NULL, three-valued logic, and NULLs are used to show no value but have the side effect of making just about everything from enums to indexes much more difficult to process and optimize. Avoid as much as you can! 7 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 8. Slide to check if audience is awake 8 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 9. Data Types MySQL has data types for integers, decimals, dates, strings, BLOBS (not binary large object but like 'The Thing that Ate Cincinatti'). Note that character sets can affect size of character data – Latin1 'A' is one char, thee in UTF8 9 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 10. Size matters! Many programmers use the biggest Possible data type 'just in case' but it Wastes space, bandwidth, and time. Is a Wordpress blog really going to have 9,223,372,936,054,775,807 Entries?!?!? 10 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 11. Select just what you need for SPEED! Bad: Better: SELECT * SELECT fname, lname, email FROM customers FROM customers Let say you need to spam your customers, query on the left will use the * wild card to return all columns from the customers table. If this table has many columns, there is a lot of extra data movement (disk, memory, network, buffers, etc.) when all you need is three columns. 11 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 12. Third normal form A memorable statement of Codd's definition of 3NF, paralleling the traditional pledge to give true evidence in a court of law, was given by Bill Kent: "[Every] non-key [attribute] must provide a fact about the key, the whole key, and nothing but the key."[6] A common variation supplements this definition with the oath: "so help me Codd". Kent, William. "A Simple Guide to Five Normal Forms in Relational Database Theory", Communications of the ACM 26 (2), Feb. 1983, pp. 120–125 Diehr, George. Database Management (Scott, Foresman, 1989), p. 331. 12 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 13. Third normal – simplified (probably too much) Each row has a key and everything else on the row is unique to that key – very simplified but a decent guideline 13 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 14. INDEXes MySQL Manual 8.3.1. How MySQL Uses Indexes Indexes are used to find rows with specific column values quickly. Without an index, MySQL must begin with the first row and then read through the entire table to find the relevant rows. The larger the table, the more this costs. If the table has an index for the columns in question, MySQL can quickly determine the position to seek to in the middle of the data file without having to look at all the data. If a table has 1,000 rows, this is at least 100 times faster than reading sequentially. 14 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 15. Sheeri's Session after this session Are You Getting the Best Out of Your MySQL Indexes – Same room – 11:30 AM 15 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 16. INDEX rough rules 1. Non null, unique, short as possible 2. INDEX columns on right side of WHERE in a query (very, very crude) 3. After #2, go out and really learn indexes! 4. Composite index are your friend 5. Indexes will not help when you have to perform a full table scan 6. Learn how to use EXPLAIN 16 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 17. Monitoring There are tools to help you gauge efficiency – MySQL Workbench, MySQL Enterprise Monitor, PHPmyAdmin, etc that can give you a clue. But you need a good understanding of query optimization first. But be aware they are there! 17 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 18. Resources 'Ping' Lenka or Dave For Anything  Find Info on MySQL.Com MySQL User Group Related  EMEA contact – lenka.kasparova@oracle.com  North America contact – david.stokes@oracle.com  Planet.MySQL.Com – Best MySQL Blogs  Los Angeles MySQL User Group 18 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 19. Virtual Developer Day March 12 Virtual Developer Day: MySQL is a one-stop shop for you to learn all the essential MySQL skills. With a combination of presentations and hands-on lab experience, you’ll have the opportunity to practice in your own environment and sharpen your skills to: • Develop your new applications cost-effectively using MySQL • Improve performance of your existing MySQL databases • Manage your MySQL environment more efficiently https://oracle.6connex.com/portal/mysql/login/? langR=en_US&mcc=launch 19 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 20. Review 1. SQL is odd, get used to it. 2. Use the right size for your data 3. Select what you need for speed 4. Use 3NF 5. Learn indexes, learn to monitor instances 20 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 21. Q&A Slides at http://slideshare.net/davestokes/presentations 21 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 22. 22 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • 23. 23 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.