MySQL
Dept. of Computing Science, University of Aberdeen 2In this lecture you will learn• The main subsystems in MySQLarchitectu...
Dept. of Computing Science, University of Aberdeen 3Introduction• MySQL is an open source RDBMS• Main Features– Server-cli...
Dept. of Computing Science, University of Aberdeen 4MySQL Architecture• Primary subsystems– The Query Engine– The Storage ...
Dept. of Computing Science, University of Aberdeen 5MySQL ArchitectureOperating SystemStorage ManagerInnoDB(transactional)...
Dept. of Computing Science, University of Aberdeen 6Query Engine• Three components– Syntax parser• Translates SQL query to...
Dept. of Computing Science, University of Aberdeen 7The Storage Manager• Responsible for file organization and indexing• I...
Dept. of Computing Science, University of Aberdeen 8Storage Engines• MyISAM (default)– extensions to the traditional ISAM–...
Dept. of Computing Science, University of Aberdeen 9Buffer Manager• Manages memory management issues– Between query engine...
Dept. of Computing Science, University of Aberdeen 10Recovery Manager• Performs data recovery in the event ofloss of data•...
Dept. of Computing Science, University of Aberdeen 11Transaction Management• Performs locking and transaction management• ...
Dept. of Computing Science, University of Aberdeen 12Specifying the table type• When you create a table you can specify th...
Dept. of Computing Science, University of Aberdeen 13Performance related issues• MySQL allows users to ask for information...
Dept. of Computing Science, University of Aberdeen 14Example continued• Now add the index to the client tablemysql> alter ...
Dept. of Computing Science, University of Aberdeen 15Further performanceenhancements• You can improve the performance of y...
Dept. of Computing Science, University of Aberdeen 16Example showing the use ofshorter indexesmysql> explain select * from...
Upcoming SlideShare
Loading in …5
×

My sql

159 views
139 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
159
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

My sql

  1. 1. MySQL
  2. 2. Dept. of Computing Science, University of Aberdeen 2In this lecture you will learn• The main subsystems in MySQLarchitecture• The different storage engines or tabletypes in MySQL• How you can define the storage engineor table type to be used for your tables• How you can improve the performanceof your queries
  3. 3. Dept. of Computing Science, University of Aberdeen 3Introduction• MySQL is an open source RDBMS• Main Features– Server-client architecture– Available for a variety of platforms including Windows andLinux– Multiple storage engines• Users/applications can select the storage engine for theirdatabase– Based on their requirements• Can even mix and match different engines within the samedatabase– Supports ANSI SQL– Version 5.0 is the current stable release– Version 5.0 supports certain spatial data types
  4. 4. Dept. of Computing Science, University of Aberdeen 4MySQL Architecture• Primary subsystems– The Query Engine– The Storage Manager– The Buffer Manager– The Transaction Manager– The Recovery Manager• Support components– The Process Manager• Connectivity functionality– Function Libraries• General functions used by all the subsystems
  5. 5. Dept. of Computing Science, University of Aberdeen 5MySQL ArchitectureOperating SystemStorage ManagerInnoDB(transactional)MyISAM(default)Memory(In-memory)NDB(Clustered)Buffer Manager Transaction ManagerRecovery ManagerQuery Engine
  6. 6. Dept. of Computing Science, University of Aberdeen 6Query Engine• Three components– Syntax parser• Translates SQL query to an internal form, RAT• Correctness of the syntax is established• User privileges are used to verify if the query makes legalreferences to tables (or fields)– Query optimizer• Selects the most efficient query execution plan– Execution component• Executes the query by making calls to other modules
  7. 7. Dept. of Computing Science, University of Aberdeen 7The Storage Manager• Responsible for file organization and indexing• Interfaces with the OS to write to disk all of the data, such as– User tables, indexes and logs• The component that makes MySQL special by– Offering different types of storage engines (or table types)• Advantages of multiple storage engines– When new engines are added to the server, the server architectureallows supporting older file formats– Changes to storage engines do not cause changes elsewhere inMySQL– Different users/applications have a choice in the storage engineused– Easier to introduce new storage media (compact flash) which mayrequire different approach
  8. 8. Dept. of Computing Science, University of Aberdeen 8Storage Engines• MyISAM (default)– extensions to the traditional ISAM– No support for transactions– No referential integrity• InnoDB (Transactional)– Supports transactions with ‘ACID’ properties– Supports referential integrity• Memory (In-memory)– Tables of this type are stored in RAM; therefore fast– But data will be lost in the event of a crash• NDB (Clustered) – advanced storage method - nothere• You will be using mostly MyISAM or InnoDB
  9. 9. Dept. of Computing Science, University of Aberdeen 9Buffer Manager• Manages memory management issues– Between query engine and memory manager• Maintains Query Cache which– Stores the result sets of queries– Repeated queries are answered directly from query cache• New records are cached in before being written todisk• Query cache is unique to MySQL• Is responsible for enhanced response times– Studies show that MySQL works as fast as Oracle and SQLserver (Microsoft’s RDBMS server)
  10. 10. Dept. of Computing Science, University of Aberdeen 10Recovery Manager• Performs data recovery in the event ofloss of data• Different for different storage engines(or table types)• InnoDB table provides recoverymanagement• MyISAM table provides fixes to datainconsistencies due to server outage
  11. 11. Dept. of Computing Science, University of Aberdeen 11Transaction Management• Performs locking and transaction management• Different for different storage engines (ortable types)• InnoDB table supports– transaction management where transactions obey‘ACID’ requirements– row and table level locking– All isolation levels• MyISAM table supports– Table level locking
  12. 12. Dept. of Computing Science, University of Aberdeen 12Specifying the table type• When you create a table you can specify the table type (storageengine) for that table• For exampleCREATE TABLE test (id int(5) Primary Key, name varchar(25)) TYPE =InnoDB;• You can alter the type of an existing table to a different type• For exampleALTER TABLE test TYPE = MyISAM;• A single MySQL database can have tables of different types• If you don’t require transactions, it is best to use MyISAM• You can use SHOW TABLE STATUS to display informationabout a table• For example to display information about table called testSHOW TABLE STATUS like ‘test’ G
  13. 13. Dept. of Computing Science, University of Aberdeen 13Performance related issues• MySQL allows users to ask for information about how theirselect queries are processed using EXPLAIN• EXPLAIN is the command to be added before your selectstatement• For examplemysql> explain select * from client where ClientNo=CR74;+--------+------+---------------+------+---------+------+------+-------------+| table | type | possible_keys | key | key_len | ref | rows | Extra |+--------+------+---------------+------+---------+------+------+-------------+| client | ALL | NULL | NULL | NULL | NULL | 4 | Using where |+--------+------+---------------+------+---------+------+------+-------------+1 row in set (0.00 sec)Please note that the ‘possible_keys’ field is NULL – which means no indexdefinedPlease note that the ‘rows’ field is 4 – which means 4 rows (which is all therows client table has) which requires examination
  14. 14. Dept. of Computing Science, University of Aberdeen 14Example continued• Now add the index to the client tablemysql> alter table client add index(ClientNo);Query OK, 4 rows affected (0.05 sec)Records: 4 Duplicates: 0 Warnings: 0• Now rerun the previous select query asking for explanationmysql> explain select * from client where ClientNo=CR74;+--------+------+---------------+----------+---------+-------+------+-------------+| table | type | possible_keys | key | key_len | ref | rows | Extra |+--------+------+---------------+----------+---------+-------+------+-------------+| client | ref | ClientNo | ClientNo | 6 | const | 1 | Using where |+--------+------+---------------+----------+---------+-------+------+-------------+1 row in set (0.00 sec)•Please note that now only one row needs to be examined andthe query therefore runs faster•Please also note that this table now has an index
  15. 15. Dept. of Computing Science, University of Aberdeen 15Further performanceenhancements• You can improve the performance of your query further bydefining smaller indexes• In the previous example ClientNo is used as the index– it is five characters long– therefore defining only part of the clientNo as the index may notmake much difference• When longer fields are used as indexes– It helps to define the index on part of the field (ref: slide 16)• Query Optimizer in MySQL inspects all the indexes beforeselecting one for use in query processing• You can help MySQL by running the following statement to helpin the process of selecting the correct indexANALYZE TABLE client;• You can also instruct MySQL to reclaim space lost due todeletion of records (holes)OPTIMIZE TABLE client;
  16. 16. Dept. of Computing Science, University of Aberdeen 16Example showing the use ofshorter indexesmysql> explain select * from client where Lname = Ritchie;+--------+------+---------------+------+---------+------+------+-------------+| table | type | possible_keys | key | key_len | ref | rows | Extra |+--------+------+---------------+------+---------+------+------+-------------+| client | ALL | NULL | NULL | NULL | NULL | 4 | Using where |+--------+------+---------------+------+---------+------+------+-------------+1 row in set (0.00 sec)mysql> alter table client add index(Lname(3));Query OK, 4 rows affected (0.03 sec)Records: 4 Duplicates: 0 Warnings: 0mysql> explain select * from client where Lname = Rit;+--------+------+---------------+-------+---------+-------+------+-------------+| table | type | possible_keys | key | key_len | ref | rows | Extra |+--------+------+---------------+-------+---------+-------+------+-------------+| client | ref | Lname | Lname | 4 | const | 1 | Using where |+--------+------+---------------+-------+---------+-------+------+-------------+1 row in set (0.00 sec)

×