Better sq lqueries
Upcoming SlideShare
Loading in...5
×
 

Better sq lqueries

on

  • 391 views

 

Statistics

Views

Total Views
391
Views on SlideShare
391
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Better sq lqueries Better sq lqueries Presentation Transcript

  • 1 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • Writing BetterMySQL Queries forBeginnersDave StokesMySQL Community ManagerDavid.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. View slide
  • 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/A4 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. View slide
  • 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. Date6 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 awake8 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 UTF89 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 Codds 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 guideline13 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.
  • Sheeris Session after this session Are You Getting the Best Out of Your MySQL Indexes – Same room – 11:30 AM15 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 EXPLAIN16 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 Group18 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=launch19 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 instances20 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
  • Q&A Slides at http://slideshare.net/davestokes/presentations21 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.