180 likes | 322 Views
Database Performance Tuning. Optimization Methods. DBMS Architecture. All data in a database are stored in data files. Data files are stored in file groups or table spaces. To work with the data, it is first moved to data/buffer cache (in RAM).
E N D
Database Performance Tuning Optimization Methods
DBMS Architecture • All data in a database are stored in data files. • Data files are stored in file groups or table spaces. • To work with the data, it is first moved to data/buffer cache (in RAM). • SQL cache stores the most recently executed statements and functions.
Database Architecture • Moving data from data files (permanent storage) to cache requires in I/O request. • The majority of performance-tuning activities focus on minimizing the number of I/O requests.
Design Choices • Make your data as small as possible • Declare columns not null (saves 1 bit per column) • Keep primary keys as short as possible • Create only the indexes that you need. Use indexes when you search using a combination of columns.
Database Statistics/Troubleshooting • Use the ANALYZE TABLE command to gather statistics on your database. • Use the CHECK TABLE command to check for corrupt tables resulting from not being closed properly. • Use the REPAIR TABLE to fix any corrupt tables found with the CHECK TABLE command
Database Statistics/Troubleshooting • Use the OPTIMIZE table command to recover unused space caused by fragmented files.
Optimizing Queries • Use the EXPLAIN command BEFORE your SELECT query to see exactly how the query is working • Create indexes to speed up data retrieval • MySQL keeps row data (tables) and index data in separate files • An index is an ordered set of values that contains the index key and pointers. • The pointers are the row IDs for the actual table rows
Indexes • If there is no index, the DBMS will perform a full table scan when executing a query.
Indexes and Query Optimization (continued) CREATE INDEX STATE_NDX ON CUSTOMER(CUS_STATE);
Example using Premiere Products CREATE INDEX CREDIT_LIIMIT ON CUSTOMER (CREDIT_LIMIT); CREATE INDEX REP_NUM ON CUSTOMER (REP_NUM);
When do I need to create an Index? • When an indexed column appears by itself in a search criteria of a WHERE or HAVING clause. • When an indexed column appears by itself in a GROUP BY or ORDER BY clause. • When the data sparsity on the indexed column is high (High number of different values that a column could have.)
Can I “SEE” an index file? • SHOW INDEX FROM tablename;
Deleting Indexes • DROP INDEX indexname FROM tablename;
Database Optimization Exercise • Use the car dealership project 2 database to complete the exercise. • Completed exercise due on Wednesday, November 19 (Same day as exam 2)