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.

Better sq lqueries

  • 1.
    1 Copyright ©2012, Oracle and/or its affiliates. All rights reserved.
  • 2.
    Writing Better MySQL Queriesfor Beginners Dave Stokes MySQL Community Manager David.Stokes@Oracle.com @stoker
  • 3.
    The following isintended 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 checkif 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 whatyou 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 afterthis 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 DayMarch 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.