Top Banner
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 1
23
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Better sq lqueries

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

Page 2: Better sq lqueries

Writing Better MySQL Queries for BeginnersDave StokesMySQL Community Manager

[email protected] @stoker

Page 3: Better sq lqueries

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.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.

Page 4: Better sq lqueries

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

Agenda

SQL history and offitiesData Storage – why all INTs should not be BIGINT, etc.Table Design Indexes – why to index columns on the right side of a

WHEREQUERY monitoring and optimizationQ/A

Page 5: Better sq lqueries

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';

Page 6: Better sq lqueries

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

Cod and Date, er, E.F. Codd C.J. Date

Page 7: Better sq lqueries

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!

Page 8: Better sq lqueries

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

Slide to check if audience is awake

Page 9: Better sq lqueries

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

Page 10: Better sq lqueries

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?!?!?

Page 11: Better sq lqueries

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

Select just what you need for SPEED!

Bad:

SELECT *

FROM customers

Better:

SELECT fname, lname, email

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.

Page 12: Better sq lqueries

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.

Page 13: Better sq lqueries

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 Each row has a key and everything else on the row is unique to that keythe row is unique to that key– very simplified but a decent guideline

Page 14: Better sq lqueries

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.

Page 15: Better sq lqueries

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

Page 16: Better sq lqueries

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

Page 17: Better sq lqueries

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!

Page 18: Better sq lqueries

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

Resources

Find Info on MySQL.Com

EMEA contact – [email protected]

North America contact – [email protected]

Planet.MySQL.Com – Best MySQL Blogs

Los Angeles MySQL User Group

'Ping' Lenka or Dave

ForAnything

MySQL UserGroup Related

Page 19: Better sq lqueries

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

Virtual Developer Day March 12Virtual 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

Page 20: Better sq lqueries

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

Page 21: Better sq lqueries

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

Q&A

Slides at http://slideshare.net/davestokes/presentations

Page 22: Better sq lqueries

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

Page 23: Better sq lqueries

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