Uphpu Mysql
-
Upload
jay-robert-del-rosario -
Category
Documents
-
view
214 -
download
0
Transcript of Uphpu Mysql
-
8/6/2019 Uphpu Mysql
1/28
MySQL DatabasesUPHPU Meeting
May 18, 2004Presented by Mac Newbold
-
8/6/2019 Uphpu Mysql
2/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 2
What is MySQL?
Free, Open Source, High PerformanceDatabase Engine
Its popular too, and works great with PHP LAMP: Linux, Apache, MySQL, PHP
Based on SQL Standard Probably at least as compliant as other DBs
Versions: 3.23.xx, 4.0.xx Stable 4.1.xx, 5.xx Development
-
8/6/2019 Uphpu Mysql
3/28
-
8/6/2019 Uphpu Mysql
4/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 4
Biases and Disclaimers
I like MySQL a lot For me: reliable, capable, featureful
I havent used PostgreSQL Probably should try it sometime
So far have never needed to
Most familiar with 3.23.xx Havent tried some new features from 4.x, 5.x
Others here likely know more than I
-
8/6/2019 Uphpu Mysql
5/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 5
Why MySQL?
Most popular open-source DB
Fast, reliable, scalable, etc.
Integrated with common applications Why not?
Some advanced features are new in MySQL
Some think PostgreSQL or Oracle is better Some people think free == cheap
-
8/6/2019 Uphpu Mysql
6/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 6
The Plan
Introductions
Where to learn the basics
MySQL Table Types: MyISAM, InnoDB Database Design Tips and Tricks
Advanced SQL Joins, Subqueries, Triggers, Transactions,
Foreign Keys, Stored Procedures
Optimization Techniques
-
8/6/2019 Uphpu Mysql
7/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 7
Where to Learn the Basics
#1 most important thing: MySQL Manual www.mysql.com or dev.mysql.com
For basic SQL, ask Google: sql tutorial Im not planning on covering:
Basics of installing, administering, using it
DB Security Caching, replication, clustering, etc.
(We can talk after or on the list)
-
8/6/2019 Uphpu Mysql
8/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 8
MySQL Table Types
Choose one to match your needs
MyISAM
Default most common, most familiar InnoDB
Supports some newer features: Transactions, row-level locking, foreign keys
High volume, high performance
BDB tables support transactions too
-
8/6/2019 Uphpu Mysql
9/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 9
Database Design - Relationships
Entity-Relationship (ER) Model Relationships mapped to DB tables:
1-to-1: 1 SSN/Person, 1 Person/SSN
Usually a column SSN column in person table 1-to-N/N-to-1: One category, many items
Usually a column in separate table category table,plus item table with category column
N-to-N: bank accounts and account owners Usually three tables accts, people, plus ownership
table with (acctnum, personID)
-
8/6/2019 Uphpu Mysql
10/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 10
Database Design - Keys
Choosing Primary and Unique keys
Primary key is usually the minimum set of
uniquely identifying attributes Include too much unintended duplicates
Include too little false rejection
For performance, smaller is better Sometimes worthwhile to make unique IDs
-
8/6/2019 Uphpu Mysql
11/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 11
Database Design Keys 2
Table person == (first, last, dob, ssn,) Key (first, last) == ?
Key (first, last, dob) == ? Key (first, last, ssn) == ?
Table person == (id, first, last, dob, ssn,) Key (id) == ?
Pros and Cons? Size difference, speed difference
-
8/6/2019 Uphpu Mysql
12/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 12
Database Design - Efficiency
Two primary aspects Space disk space, memory footprint
Tables and indices
Time lookup speed, insert/delete speed Know your read vs. write mix
Redundancy in your data Pros: easier recovery, maybe faster Cons: harder to keep in sync, larger size
-
8/6/2019 Uphpu Mysql
13/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 13
Database Design Integrity
Referential Integrity Consistency Foreign Keys identifier in one table used to
find rows in another table (e.g. with JOIN)
Constraints are all application specific
Ex.: Bank loans and loan payments What if Loan ID in payment table doesnt match
any rows in Loans table? Foreign key constraints can be enforced
Then insert/delete order matters a lot
-
8/6/2019 Uphpu Mysql
14/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 14
Database Design - Advanced
Triggers and Stored Procedures Let database handle some of the logic/code
Nice if you have to use multiple interfaces I.e. PHP web site, Java GUI app, Perl billing system New versions of MySQL support them
Views - virtual table from query result Useful for simplification, security, privacy
Usually cant do inserts
-
8/6/2019 Uphpu Mysql
15/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 15
Joins
Relate one table to another
Select * from items as i left join category
as c on i.catid=c.catid where item=foo; New: can use for delete/update too
Many types of joins
Some more useful than others Used in almost every database application
-
8/6/2019 Uphpu Mysql
16/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 16
Join Types
Inner Join rows that have a match Omit row if either table doesnt have a match
Specify field(s) to match on Outer Join fill in non-matches with null
Left Join, Right Join, Full [Outer] Join
Specify field(s) to match on
Cross Join every possible pair of rows Huge result, not used often
-
8/6/2019 Uphpu Mysql
17/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 17
Join Types 2
Natural Join join fields with same name In MySQL, same as inner join, or use Natural
Left Join to get a left join
Left/Right/Full Joins Select * from A left join B on A.id=B.id
Left gets every row from A
Right gets every row from B
Full gets every row from both
-
8/6/2019 Uphpu Mysql
18/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 18
MySQL Join Syntax
Basic: from A [join-cmd] B on [join-cond]
Handy: from A join B using (c1,c2)
Same as on a.c1=b.c1 and a.c2=b.c2) Cross: from A,B or A CROSS JOIN B
Inner: A [INNER] JOIN B
Outer: A [LEFT|RIGHT] JOIN B Full: Use UNION in 4.x with left+right joins
-
8/6/2019 Uphpu Mysql
19/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 19
Fancy-Pants Join Example
Join a table with itself Useful with tree-like structures, for example
Table cat = (id, name, parent) Parent holds id of parent category
Select c1.id, c1.name,c2.id, c2.name from
cat as c1 left join cat as c2 onc1.parent=c2.id;
Pulls id/name for category and its parent
-
8/6/2019 Uphpu Mysql
20/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 20
Subqueries
Select * from t1 where col1= (select col2from t2);
More structured, and readable too
Alternative to complex joins and unions
Available in MySQL
-
8/6/2019 Uphpu Mysql
21/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 21
Subqueries 2
Default: true if any subq row matches
Additional keywords for subqueries:
IN (alias for =) ANY / SOME (optional, same meaning)
ALL (match all rows instead of any row) Select c from t where c > ALL (select c2 from t2)
EXISTS / NOT EXISTS (subq has results)
-
8/6/2019 Uphpu Mysql
22/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 22
Subqueries 3
Row subqueries:
Select * from t1 where (1,2) = (select
c1,c2 from t2); Rows where c1=1 AND c2=2
Subqueries in the FROM clause:
Select from (subquery) as t1 Derived tables, a.k.a. unnamed views
-
8/6/2019 Uphpu Mysql
23/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 23
Rewriting Subqueries
When you cant or dont want to use one MySQL < 4.1, performance, portability, etc.
Select * from t1 where c in (select c from t2)
Select t1.* from t1,t2 where t1.c=t2.c
Select t1.* from t1 join t2 on t1.c=t2.c
Select * from t1 where c not in (select c from
t2) Select t1.* from t1 left join t2 on t1.c=t2.c
where t2.c is null;
-
8/6/2019 Uphpu Mysql
24/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 24
Other Advanced SQL
Stored Procedures (5.0) & Triggers (5.1) Put some app logic into db
Transactions Multiple queries as an atomic operation
Use commit; to save, or rollback; to cancel
Can affect performance significantly
Foreign Keys DB enforces constraints on joinable columns
-
8/6/2019 Uphpu Mysql
25/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 25
Optimizing MySQL Queries
Common bottlenecks any guesses? Disk seek time, disk bandwidth, CPU, memory
bandwidth, sometimes network bandwidth
Optimization tools Benchmarking Benchmark(1000000, 1+1)
EXPLAIN - optimizations, indices, etc. in use
Add indices to your tables appropriately Minimize locking
-
8/6/2019 Uphpu Mysql
26/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 26
EXPLAIN Explained
Example: EXPLAIN select * from user acct as ua leftjoin user_memb as um on ua.uid = um.uid left
join comp_acct as ca on um.cid = ca.cid; Result has (table, type, possible_keys,
key, key_len, ref, rows, extra)
Type is const, eq_ref, ref, , range, , all Keys and rows are useful too Extra indicates sorting, temp tables, etc.
-
8/6/2019 Uphpu Mysql
27/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 27
Easy Optimization Tips
Make sure your joinable columns havethe exact same type in both tables
Add appropriate indices if explain showsthat mysql isnt finding one it should beable to use
Enable the slowquery log, and use it
Table order in joins has big effects
Fixed size vs variable size fields
-
8/6/2019 Uphpu Mysql
28/28
May 18, 2004 MySQL- UPHPU - Mac Newbold 28
Question and Answer
If youve got questions, I have answers Answers: Free
Reasonable Answers: $5 Correct Answers: Priceless
Thanks for coming and participating!
Im on IRC in #uphpu often, or
Email me at [email protected]