This document provides an introduction to using EXPLAIN to analyze MySQL queries. It begins with background on MySQL and the objectives of learning to use EXPLAIN. It then covers the syntax of EXPLAIN, the output it provides, and what each column in the output means. The document concludes with brief discussions on indexing techniques and query optimization.
This document discusses several MySQL idiosyncrasies related to data integrity, transactions, and variable scope that can cause unexpected behavior for developers. Specifically, it notes that MySQL does not enforce data integrity by default, transactions are not supported with all storage engines, and variable settings apply at the session level rather than globally by default. The document provides examples of each issue and recommends configurations like stricter SQL modes and using InnoDB to avoid potential problems.
This document provides an introduction to basic MySQL statements and queries. It covers how to connect to MySQL, enter simple SELECT queries to retrieve data, create and manage databases and tables, load data, and do basic filtering, sorting and pattern matching. Examples show how to view server information, select specific columns or rows, sort results, and use wildcards and patterns to search table data.
Just about anyone can write a basic SQL query for a table. Not everyone can write a good query though - that takes practice and knowing how to understand what the optimizer is doing with the query. Learn the basics of query optimization so you keep your application engaging the user rather then showing the progress bar as they wait on the database.
MySQL Kitchen : spice up your everyday SQL queriesDamien Seguy
This document provides an agenda and summary for a MySQL conference session on clever SQL recipes and techniques for MySQL. The session will cover topics like unexpected sorting results, storing IP addresses efficiently, using auto-increment for multiple columns, generating random values, and transposing row data. The presenter is Damien Séguy, a MySQL expert consultant, who will demonstrate various techniques using a sample PHP statistics database schema. Attendees are encouraged to ask questions throughout the presentation.
The document provides a tutorial on basic MySQL commands for creating and manipulating databases, tables, and data. It covers how to create and delete databases and tables, insert, select, update, and delete rows of data, and perform joins across multiple tables. Examples are provided for each command to demonstrate its syntax and usage. The goal is to introduce developers to fundamental MySQL operations for structuring, managing, and querying relational data.
The document provides 15 pro-tips for MySQL users, beginning with basic tips like using the correct MySQL version and understanding EXPLAIN plans, and progressing to more advanced tips involving backups, queries, indexing, and database design. Key recommendations include knowing important configuration settings, backing up at the table level, avoiding SELECT * queries, using triggers for consistency, and separating volatile and non-volatile data.
Applied Partitioning And Scaling Your Database System PresentationRichard Crowley
This document discusses applying partitioning to MySQL OLTP applications. It begins by reviewing classic uses of partitioning in data warehousing applications. It then explains how partitioning can benefit OLTP workloads by reducing seek and scan set sizes and limiting transaction durations. The document provides an example of applying hash partitioning to tables in a retail store application based on store ID. It demonstrates how to expand and merge partitions as stores are added or closed. Performance tests on the partitioned tables show improvements in query times compared to non-partitioned tables. In summary, the document argues that partitioning provides a way to scale MySQL databases for OLTP workloads while reducing maintenance costs.
This document discusses several MySQL idiosyncrasies related to data integrity, transactions, and variable scope that can cause unexpected behavior for developers. Specifically, it notes that MySQL does not enforce data integrity by default, transactions are not supported with all storage engines, and variable settings apply at the session level rather than globally by default. The document provides examples of each issue and recommends configurations like stricter SQL modes and using InnoDB to avoid potential problems.
This document provides an introduction to basic MySQL statements and queries. It covers how to connect to MySQL, enter simple SELECT queries to retrieve data, create and manage databases and tables, load data, and do basic filtering, sorting and pattern matching. Examples show how to view server information, select specific columns or rows, sort results, and use wildcards and patterns to search table data.
Just about anyone can write a basic SQL query for a table. Not everyone can write a good query though - that takes practice and knowing how to understand what the optimizer is doing with the query. Learn the basics of query optimization so you keep your application engaging the user rather then showing the progress bar as they wait on the database.
MySQL Kitchen : spice up your everyday SQL queriesDamien Seguy
This document provides an agenda and summary for a MySQL conference session on clever SQL recipes and techniques for MySQL. The session will cover topics like unexpected sorting results, storing IP addresses efficiently, using auto-increment for multiple columns, generating random values, and transposing row data. The presenter is Damien Séguy, a MySQL expert consultant, who will demonstrate various techniques using a sample PHP statistics database schema. Attendees are encouraged to ask questions throughout the presentation.
The document provides a tutorial on basic MySQL commands for creating and manipulating databases, tables, and data. It covers how to create and delete databases and tables, insert, select, update, and delete rows of data, and perform joins across multiple tables. Examples are provided for each command to demonstrate its syntax and usage. The goal is to introduce developers to fundamental MySQL operations for structuring, managing, and querying relational data.
The document provides 15 pro-tips for MySQL users, beginning with basic tips like using the correct MySQL version and understanding EXPLAIN plans, and progressing to more advanced tips involving backups, queries, indexing, and database design. Key recommendations include knowing important configuration settings, backing up at the table level, avoiding SELECT * queries, using triggers for consistency, and separating volatile and non-volatile data.
Applied Partitioning And Scaling Your Database System PresentationRichard Crowley
This document discusses applying partitioning to MySQL OLTP applications. It begins by reviewing classic uses of partitioning in data warehousing applications. It then explains how partitioning can benefit OLTP workloads by reducing seek and scan set sizes and limiting transaction durations. The document provides an example of applying hash partitioning to tables in a retail store application based on store ID. It demonstrates how to expand and merge partitions as stores are added or closed. Performance tests on the partitioned tables show improvements in query times compared to non-partitioned tables. In summary, the document argues that partitioning provides a way to scale MySQL databases for OLTP workloads while reducing maintenance costs.
Beyond php - it's not (just) about the codeWim Godden
Most PHP developers focus on writing code. But creating Web applications is about much more than just wrting PHP. Take a step outside the PHP cocoon and into the big PHP ecosphere to find out how small code changes can make a world of difference on servers and network. This talk is an eye-opener for developers who spend over 80% of their time coding, debugging and testing.
This document outlines the organization of a course on MySQL/PHP database applications. It is divided into four parts:
Part I covers working with MySQL, including designing databases, creating tables, and querying data. Part II focuses on working with PHP scripts to access databases and present HTML. Part III presents simple database applications using lessons from Parts I and II. Part IV introduces more complex real-world applications commonly used on the web.
Constraints are rules used to define and restrict the type of data that can be placed within MySQL tables. The document discusses several constraint types including NOT NULL, DEFAULT, UNIQUE, PRIMARY KEY, and FOREIGN KEY. It provides examples of how to implement these constraints when creating or altering tables, and demonstrates the behavior that results from adhering to or violating the constraints, such as errors generated or default values used. Foreign keys link data between tables, helping to enforce relational integrity.
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013Sergey Petrunya
The document discusses techniques for identifying and addressing problems with a database query optimizer. It describes old and new tools for catching slow queries, such as the slow query log, SHOW PROCESSLIST, and the Performance Schema. It also provides examples of using these tools to analyze query plans, identify inefficient plans, and determine if optimizer settings or query structure need to be modified to address performance issues.
The document discusses MySQL indexing and optimization. It provides an overview of indexing in MySQL, including what indexes are, how they are created, and how MySQL uses indexes to optimize queries. The document also discusses best practices for indexing, including identifying missing indexes, avoiding unnecessary indexes, and ensuring indexes have good selectivity to maximize query performance.
1. The document discusses two methods for explaining SQL execution plans without executing the query: using the EXPLAIN PLAN statement and the GATHER_PLAN_STATISTICS hint.
2. It explains the components of an execution plan such as operation IDs, costs, and predicate information. Filter operations may validate logic before child operations execute.
3. Displaying execution plan statistics with DBMS_XPLAN after running a query with GATHER_PLAN_STATISTICS shows runtime metrics like number of rows and buffers accessed.
Lecture5 my sql statements by okello erickokelloerick
The document provides an overview of MySQL statements and how to interact with databases and tables. It discusses how to connect to MySQL, create and select databases, create and describe tables, enter and query data, filter queries using WHERE clauses, and update and delete records. Examples are provided for common statements like SELECT, INSERT, UPDATE, DELETE, SHOW, DESCRIBE, COUNT and more. The document also covers sorting, pattern matching, and running MySQL in batch mode to execute multiple commands from a file.
This document discusses the architecture of web applications using MySQL and PHP. It describes the main components including the relational database (MySQL), middleware (PHP), web server (Apache, IIS), and web browser. It also provides examples of SQL statements like SELECT, INSERT, WHERE to query and manipulate data in a MySQL database.
The document shows the steps taken to create and populate a MySQL database called pbd. The following tables were created: siswa, guru, mahasiswa, matakuliah, and krs. Data was inserted into the siswa and guru tables. Columns in the tables were updated, rows were deleted, and selections were made to view the data.
MySQL Server Performance Tuning 101 presented by Ligaya Turmelle discusses optimizing MySQL performance. It recommends first optimizing queries, then the underlying system. Various status variables provide insight into server operations like queries, caches, threads and storage engines. Global and session variables can be tuned including key buffer sizes, caches, threads and buffers. Understanding status variables and making incremental changes while benchmarking helps identify performance improvements.
This document discusses various techniques for optimizing MySQL queries and databases, including:
- Using the query cache can speed up repeating queries with the same syntax.
- Choosing optimal variable types like integers over strings for size and performance.
- Indexing the right columns and testing indexes regularly as tables change.
- Using EXPLAIN to check query performance and identify optimization opportunities.
- Profiling queries with other tools to see where time is spent.
- Testing changes thoroughly before deploying optimizations.
This document discusses strategies for maintaining very large MySQL tables that have grown too big. It recommends creating a new database server with different configuration settings like InnoDB file per table to reduce size, using tools like MySQLTuner and tuning-primer to analyze settings, archiving old historical data with ptArchiver to reduce table sizes, and considering partitioning or changing the MySQL version. Monitoring tools like InnoDB status, global status, cacti and innotop are recommended to analyze server performance.
This document provides an overview of MySQL server performance tuning. It discusses laying the foundation for performance tuning by examining the server, OS, network and filesystem. It also covers examining current server settings and status variables, and tuning various aspects of MySQL like InnoDB, MyISAM, queries and session settings. The document aims to provide guidance on areas to optimize to improve MySQL server performance.
The document discusses optimization of MySQL performance. It includes an agenda for the presentation which covers strategic considerations, schema optimization through normalization and indexing, and query optimization. Examples provided demonstrate summary and cache tables to improve query performance through de-normalization, and techniques for indexing and handling concurrency issues.
The document discusses MySQL data manipulation commands. It provides examples of using SELECT statements to retrieve data from tables based on specified criteria, INSERT statements to add new data to tables, UPDATE statements to modify existing data in tables, and the basic syntax for these commands. It also reviews naming conventions and some best practices for working with tables in MySQL.
The document discusses window functions in MariaDB. It begins with an overview and plan, then covers basic window functions like row_number(), rank(), dense_rank(), and ntile(). It discusses frames for window functions, including examples using RANGE frames. It provides examples of problems that can be solved using window functions, such as smoothing noisy data, generating account balance statements, and finding sequences with no missing numbers ("islands").
New optimizer features in MariaDB releases before 10.12Sergey Petrunya
The document discusses new optimizer features in recent and upcoming MariaDB releases. MariaDB 10.8 introduced JSON histograms and support for reverse-ordered indexes. JSON produced by the optimizer is now valid and processible. MariaDB 10.9 added SHOW EXPLAIN FORMAT=JSON and SHOW ANALYZE can return partial results. MariaDB 10.10 enabled table elimination for derived tables and improved optimization of many-table joins. Future releases will optimize queries on stored procedures and show optimizer timing in EXPLAIN FORMAT=JSON.
This document discusses MySQL indexing best practices. It explains that indexes can improve query performance but also waste space and time if unnecessary. The right balance must be found to achieve fast queries using an optimal index set. It provides examples of how to create indexes and how they can be used to optimize WHERE, JOIN, ORDER BY, and other clauses. It also discusses avoiding unnecessary or redundant indexes.
This document provides an overview of MySQL basics including what MySQL is, installing the MySQL server, using the mysql command line client, SQL basics like concepts and terminology for databases, tables and fields, and performing operations like insert, update, delete and select. It also covers retrieving records with select through ordering, limiting and arithmetic functions. The document concludes by explaining how to read and execute SQL statements from a script file.
Modern query optimisation features in MySQL 8.Mydbops
MySQL 8 (a huge leap forward), indexing capabilities, execution plan enhancements, optimizer improvements, and many other current query tweak features are covered in the slides.
This presentation focuses on optimization of queries in MySQL from developer’s perspective. Developers should care about the performance of the application, which includes optimizing SQL queries. It shows the execution plan in MySQL and explain its different formats - tabular, TREE and JSON/visual explain plans. Optimizer features like optimizer hints and histograms as well as newer features like HASH joins, TREE explain plan and EXPLAIN ANALYZE from latest releases are covered. Some real examples of slow queries are included and their optimization explained.
Beyond php - it's not (just) about the codeWim Godden
Most PHP developers focus on writing code. But creating Web applications is about much more than just wrting PHP. Take a step outside the PHP cocoon and into the big PHP ecosphere to find out how small code changes can make a world of difference on servers and network. This talk is an eye-opener for developers who spend over 80% of their time coding, debugging and testing.
This document outlines the organization of a course on MySQL/PHP database applications. It is divided into four parts:
Part I covers working with MySQL, including designing databases, creating tables, and querying data. Part II focuses on working with PHP scripts to access databases and present HTML. Part III presents simple database applications using lessons from Parts I and II. Part IV introduces more complex real-world applications commonly used on the web.
Constraints are rules used to define and restrict the type of data that can be placed within MySQL tables. The document discusses several constraint types including NOT NULL, DEFAULT, UNIQUE, PRIMARY KEY, and FOREIGN KEY. It provides examples of how to implement these constraints when creating or altering tables, and demonstrates the behavior that results from adhering to or violating the constraints, such as errors generated or default values used. Foreign keys link data between tables, helping to enforce relational integrity.
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013Sergey Petrunya
The document discusses techniques for identifying and addressing problems with a database query optimizer. It describes old and new tools for catching slow queries, such as the slow query log, SHOW PROCESSLIST, and the Performance Schema. It also provides examples of using these tools to analyze query plans, identify inefficient plans, and determine if optimizer settings or query structure need to be modified to address performance issues.
The document discusses MySQL indexing and optimization. It provides an overview of indexing in MySQL, including what indexes are, how they are created, and how MySQL uses indexes to optimize queries. The document also discusses best practices for indexing, including identifying missing indexes, avoiding unnecessary indexes, and ensuring indexes have good selectivity to maximize query performance.
1. The document discusses two methods for explaining SQL execution plans without executing the query: using the EXPLAIN PLAN statement and the GATHER_PLAN_STATISTICS hint.
2. It explains the components of an execution plan such as operation IDs, costs, and predicate information. Filter operations may validate logic before child operations execute.
3. Displaying execution plan statistics with DBMS_XPLAN after running a query with GATHER_PLAN_STATISTICS shows runtime metrics like number of rows and buffers accessed.
Lecture5 my sql statements by okello erickokelloerick
The document provides an overview of MySQL statements and how to interact with databases and tables. It discusses how to connect to MySQL, create and select databases, create and describe tables, enter and query data, filter queries using WHERE clauses, and update and delete records. Examples are provided for common statements like SELECT, INSERT, UPDATE, DELETE, SHOW, DESCRIBE, COUNT and more. The document also covers sorting, pattern matching, and running MySQL in batch mode to execute multiple commands from a file.
This document discusses the architecture of web applications using MySQL and PHP. It describes the main components including the relational database (MySQL), middleware (PHP), web server (Apache, IIS), and web browser. It also provides examples of SQL statements like SELECT, INSERT, WHERE to query and manipulate data in a MySQL database.
The document shows the steps taken to create and populate a MySQL database called pbd. The following tables were created: siswa, guru, mahasiswa, matakuliah, and krs. Data was inserted into the siswa and guru tables. Columns in the tables were updated, rows were deleted, and selections were made to view the data.
MySQL Server Performance Tuning 101 presented by Ligaya Turmelle discusses optimizing MySQL performance. It recommends first optimizing queries, then the underlying system. Various status variables provide insight into server operations like queries, caches, threads and storage engines. Global and session variables can be tuned including key buffer sizes, caches, threads and buffers. Understanding status variables and making incremental changes while benchmarking helps identify performance improvements.
This document discusses various techniques for optimizing MySQL queries and databases, including:
- Using the query cache can speed up repeating queries with the same syntax.
- Choosing optimal variable types like integers over strings for size and performance.
- Indexing the right columns and testing indexes regularly as tables change.
- Using EXPLAIN to check query performance and identify optimization opportunities.
- Profiling queries with other tools to see where time is spent.
- Testing changes thoroughly before deploying optimizations.
This document discusses strategies for maintaining very large MySQL tables that have grown too big. It recommends creating a new database server with different configuration settings like InnoDB file per table to reduce size, using tools like MySQLTuner and tuning-primer to analyze settings, archiving old historical data with ptArchiver to reduce table sizes, and considering partitioning or changing the MySQL version. Monitoring tools like InnoDB status, global status, cacti and innotop are recommended to analyze server performance.
This document provides an overview of MySQL server performance tuning. It discusses laying the foundation for performance tuning by examining the server, OS, network and filesystem. It also covers examining current server settings and status variables, and tuning various aspects of MySQL like InnoDB, MyISAM, queries and session settings. The document aims to provide guidance on areas to optimize to improve MySQL server performance.
The document discusses optimization of MySQL performance. It includes an agenda for the presentation which covers strategic considerations, schema optimization through normalization and indexing, and query optimization. Examples provided demonstrate summary and cache tables to improve query performance through de-normalization, and techniques for indexing and handling concurrency issues.
The document discusses MySQL data manipulation commands. It provides examples of using SELECT statements to retrieve data from tables based on specified criteria, INSERT statements to add new data to tables, UPDATE statements to modify existing data in tables, and the basic syntax for these commands. It also reviews naming conventions and some best practices for working with tables in MySQL.
The document discusses window functions in MariaDB. It begins with an overview and plan, then covers basic window functions like row_number(), rank(), dense_rank(), and ntile(). It discusses frames for window functions, including examples using RANGE frames. It provides examples of problems that can be solved using window functions, such as smoothing noisy data, generating account balance statements, and finding sequences with no missing numbers ("islands").
New optimizer features in MariaDB releases before 10.12Sergey Petrunya
The document discusses new optimizer features in recent and upcoming MariaDB releases. MariaDB 10.8 introduced JSON histograms and support for reverse-ordered indexes. JSON produced by the optimizer is now valid and processible. MariaDB 10.9 added SHOW EXPLAIN FORMAT=JSON and SHOW ANALYZE can return partial results. MariaDB 10.10 enabled table elimination for derived tables and improved optimization of many-table joins. Future releases will optimize queries on stored procedures and show optimizer timing in EXPLAIN FORMAT=JSON.
This document discusses MySQL indexing best practices. It explains that indexes can improve query performance but also waste space and time if unnecessary. The right balance must be found to achieve fast queries using an optimal index set. It provides examples of how to create indexes and how they can be used to optimize WHERE, JOIN, ORDER BY, and other clauses. It also discusses avoiding unnecessary or redundant indexes.
This document provides an overview of MySQL basics including what MySQL is, installing the MySQL server, using the mysql command line client, SQL basics like concepts and terminology for databases, tables and fields, and performing operations like insert, update, delete and select. It also covers retrieving records with select through ordering, limiting and arithmetic functions. The document concludes by explaining how to read and execute SQL statements from a script file.
Modern query optimisation features in MySQL 8.Mydbops
MySQL 8 (a huge leap forward), indexing capabilities, execution plan enhancements, optimizer improvements, and many other current query tweak features are covered in the slides.
This presentation focuses on optimization of queries in MySQL from developer’s perspective. Developers should care about the performance of the application, which includes optimizing SQL queries. It shows the execution plan in MySQL and explain its different formats - tabular, TREE and JSON/visual explain plans. Optimizer features like optimizer hints and histograms as well as newer features like HASH joins, TREE explain plan and EXPLAIN ANALYZE from latest releases are covered. Some real examples of slow queries are included and their optimization explained.
Using Optimizer Hints to Improve MySQL Query Performanceoysteing
The document discusses using optimizer hints in MySQL to improve query performance. It covers index hints to influence which indexes the optimizer uses, join order hints to control join order, and subquery hints. New optimizer hints introduced in MySQL 5.7 and 8.0 are also presented, including hints for join strategies, materialized intermediate results, and query block naming. Examples are provided to illustrate how hints can be used and their behavior.
Microsoft Access is a DBMS (also known as Database Management System) from Microsoft that combines the relational Microsoft Jet Database Engine with a graphical user interface and software-development tools.
1Hippeus - zerocopy messaging по законам Спарты, Леонид Юрьев (ПЕТЕР-СЕРВИС)Ontico
This document describes a high-performance distributed computing architecture using message queues and actors. It shows how producers push messages onto shared memory queues, which consumers then pull from. Actors process the messages in an asynchronous and concurrent way. The system uses allotment, depots, and baskets to manage memory allocation and work distribution across nodes.
This document discusses functional database strategies, specifically focusing on immutable rows, window functions, event-sourcing, and database interactions from a functional perspective. The key points covered include:
1. Making database rows immutable by storing them as a log of changes rather than allowing in-place updates. This allows reconstructing the full history.
2. Using window functions to partition and aggregate over row groups to enable querying the latest or historical state.
3. Storing a log of events rather than the current application state, in line with event-sourcing principles.
4. Techniques for interacting with databases in a functional way, including restricting mutations and optimizing data structures.
MariaDB: ANALYZE for statements (lightning talk)Sergey Petrunya
The document describes a new ANALYZE statement in MariaDB 10.1 that provides execution statistics for a SQL statement. ANALYZE runs the statement and collects statistics, similar to EXPLAIN ANALYZE in PostgreSQL. It produces an EXPLAIN plan with additional columns showing real rows, filtering percentages, and time spent. The FORMAT=JSON option outputs the results as a JSON document containing detailed timing and resource usage statistics for each step. This allows more complete analysis of how a query plan was executed versus global counters.
This document provides an overview of MySQL including its architecture, clients, connection layer, SQL layer, storage engines, and installation methods. Key points include:
- MySQL uses a connection thread pool, query cache, parser, optimizer, and various storage engines like InnoDB and MyISAM.
- Common MySQL clients allow executing queries, administering the server, checking tables, backing up data, and more.
- The connection layer handles authentication and the communication protocol.
- The SQL layer performs parsing, optimization, and determining the optimal execution plan.
- Storage engines like InnoDB and MyISAM support different features and have different performance characteristics for storage, indexing, and more.
Analytics functions in mysql, oracle and hiveAnkit Beohar
This document discusses analytics functions in MySQL, Oracle, and Hive for employee hierarchies, nth highest salaries, and pivot queries. It provides examples of implementing employee manager relationships, nth highest salary queries, and pivot queries in each database. It also explains how Oracle supports additional analytic functions like ROW_NUMBER, RANK, and DENSE_RANK using the OVER clause, and how the PARTITION BY clause partitions results. All queries shown for MySQL will also work in Oracle and Hive with minor syntax adjustments.
Big Data Analytics with MariaDB ColumnStoreMariaDB plc
Big Data Analytics with MariaDB ColumnStore provides an overview of MariaDB ColumnStore. Key points include:
- MariaDB ColumnStore is an open source columnar storage engine that provides high performance analytics on large datasets in a scalable distributed environment using standard SQL.
- Columnar storage organizes data by columns rather than rows, improving query performance by only accessing relevant columns. It supports workloads from terabytes to petabytes of data.
- Common use cases include data warehousing, financial services, healthcare, telecom, and any workload requiring analysis of millions to billions of rows.
- The architecture employs a distributed query processing model with horizontal partitioning and parallel query execution across nodes for high scalability
Window functions enable calculations across partitions of rows in a result set. This document discusses window function syntax, types of window functions available in MySQL 8.0 like RANK(), DENSE_RANK(), ROW_NUMBER(), and provides examples of queries using window functions to analyze and summarize data in partitions.
The document discusses SQL, MySQL, and PHP for building web applications. It covers topics like database management with MySQL, the SQL language for querying and manipulating data, using PHP to connect to MySQL databases and perform operations, and best practices for designing web applications that utilize a database backend.
This document provides an overview of advanced date and time handling in PHP, including:
- Setting default timezones and parsing date strings
- Modifying dates and times using objects and intervals
- Working with timezones and the timezone database
- Iterating over periods defined by start date, interval, and end date
APC & Memcache the High Performance DuoAnis Berejeb
The document discusses Apc & Memcached as a high-performance caching duo for PHP applications. It provides an overview of APC for opcode caching and user data caching. It then discusses Memcached which allows distributed caching across multiple servers and provides more advanced features than APC. Examples are given for basic usage of storing, retrieving, and deleting data from the caches with both extensions.
MySQL client side caching allows caching of query results on the client side using the mysqlnd driver. It is transparent to applications using MySQL extensions like mysqli or PDO. Cached results are stored using pluggable storage handlers like APC, memcache, or local memory. Queries can be cached based on SQL hints or custom logic in a user-defined storage handler. Statistics are collected on cache usage and query performance to analyze effectiveness. This provides an alternative to server-side query caching with potential benefits like reducing network traffic and database load.
MySQL client side caching allows caching of query results on the client side using the mysqlnd driver. It is transparent to applications using MySQL extensions like mysqli or PDO. Cached results are stored using pluggable storage handlers like APC, memcache, or local memory. Queries can be cached based on SQL hints or custom logic in a user-defined storage handler. Statistics are collected on cache usage and query performance to analyze effectiveness. This provides an alternative to server-side query caching with potential benefits like reducing network traffic and database load.
This document discusses various tools and techniques for profiling and optimizing MySQL query performance, including:
- Using tools like Apache ab, JMeter, and Siege to measure baseline performance and identify focus areas like latency and throughput.
- Profiling PHP applications with Xdebug to identify expensive queries and operations.
- Analyzing slow query logs, MySQL query profiling and the performance schema to find slow and expensive queries.
- Using EXPLAIN to analyze and optimize query execution plans and indexes.
- MySQL Enterprise Monitor and Query Analyzer for centralized monitoring of queries across servers.
This document discusses various tools and techniques for profiling and optimizing MySQL query performance, including:
- Using tools like Apache ab, JMeter, and Siege to measure baseline performance and identify focus areas like latency and throughput.
- Profiling PHP applications with Xdebug to identify expensive queries and operations.
- Analyzing slow query logs, MySQL query profiling and the performance schema to find slow and expensive queries.
- Using EXPLAIN to analyze and optimize query execution plans and indexes.
- MySQL Enterprise Monitor and Query Analyzer for centralized monitoring of queries across servers.
- Oracle's strategy is to provide a complete, open, and integrated stack including MySQL. They have made investments in open source projects like MySQL to expand the developer community and speed up innovation.
- MySQL 5.5 includes improvements like InnoDB as the default storage engine, semi-synchronous replication, and performance gains of over 300% on benchmarks compared to previous versions.
- The mysqli extension provides full support for modern MySQL features while ext/mysql is limited. mysqlnd provides a native driver for PHP that improves performance versus libmysql. Caching and other mysqlnd plugins provide opportunities to optimize database usage.
The document discusses hidden features of PHP, including the __DIR__ magic constant for retrieving the directory of a script, the ?? operator for retrieving a non-empty value from expressions, using GOTO for error handling, OpenSSL functions for hashing, encryption and decryption, preventing double encoding with HTML entities functions, date parsing, finding files with stream_resolve_include_path, improving session security, mail logging, directory traversal with SPL iterators, using igbinary for faster serialization, profiling with xhprof, identifying file types with fileinfo, and creating and reading Excel files with PHP-Excel.
2. <Insert Picture Here>
Intro to MySQL EXPLAIN
Ligaya Turmelle
Senior Technical Support Engineer – MySQL
http://joind.in/2257
Friday, October 29, 2010
3. MySQL Basics
• MySQL is the world's most popular open source database
software, with over 100 million copies of its software downloaded
or distributed throughout its history.
• a key part of LAMP (Linux, Apache, MySQL, PHP / Perl / Python),
the fast-growing open source enterprise software stack.
• Site: http://www.mysql.com/
• Download: mysql.org or http://dev.mysql.com/downloads/
• Online Manual: http://dev.mysql.com/doc/refman/5.1/en/index.html
Friday, October 29, 2010
4. Objectives
• So what are we hoping you will learn
– How to get EXPLAIN output
– How to read the output of EXPLAIN
– What does it mean? What can we do to speed things up?
– Briefly discuss indexing techniques
– Briefly discuss query optimization techniques
Friday, October 29, 2010
5. Start at the beginning
• Syntax: EXPLAIN [EXTENDED] SELECT select_options
– Basically you just put an EXPLAIN before a SELECT
statement.
• What does EXPLAIN do?
– displays information from the optimizer about the query
execution plan.
• EXTENDED with SHOW WARNINGS
• Works only with SELECT statements
– Some statements can be converted to SELECTS – but the
statement has to “touch” all the same columns
Friday, October 29, 2010
6. Output
mysql> EXPLAIN SELECT c.Name FROM City c WHERE
District='Florida'G
***************** 1. row ************************
id: 1
select_type: SIMPLE
table: c
type: ref
possible_keys: District
key: District
key_len: 20
ref: const
rows: 15
Extra: Using where
1 row in set (0.00 sec)
Friday, October 29, 2010
7. EXPLAIN Output
• Each row provides information about one table
• EXPLAIN output columns:
id key_len
select_type ref
table rows
type Filtered (new to 5.1)
possible_keys Extra
key
Friday, October 29, 2010
9. id
• The SELECT identifier.
• Only if there are subqueries, derived tables or unions
is this incremented
– Derived table is a subquery in the FROM clause that created
a temporary table
• Number reflects the order that the SELECT was done
in
mysql> EXPLAIN SELECT * FROM (SELECT * FROM COUNTRY) AS Country1 LIMIT 5;
+----+-------------+------------+------+---------------+------+---------
| id | select_type | table | type | possible_keys | key | key_len
+----+-------------+------------+------+---------------+------+---------
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL
| 2 | DERIVED | COUNTRY | ALL | NULL | NULL | NULL
+----+-------------+------------+------+---------------+------+---------
Friday, October 29, 2010
10. select_type
• The type of SELECT
• Can have lots of values and each means something
different
– SIMPLE – normal SELECT
– PRIMARY – outermost SELECT
– DERIVED - subquery in the FROM clause that is placed in a
temporary table (table is derived from the subquery).
– UNION – second or more SELECT statement in a UNION
– SUBQUERY – a SELECT that is not in the FROM clause
• UNION and SUBQUERY can have DEPENDANT if they
use the outer SELECT.
Friday, October 29, 2010
11. table
• Displays the name or alias of the table used
• Read it down and you see the order of the tables in a
JOIN which may or may not be the same as the order
given in the query.
• Derived tables will be noted and numbered in the
listing
mysql> EXPLAIN SELECT * FROM (SELECT * FROM COUNTRY) AS Country1 LIMIT 5;
+----+-------------+------------+------+---------------+------+---------
| id | select_type | table | type | possible_keys | key | key_len
+----+-------------+------------+------+---------------+------+---------
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL
| 2 | DERIVED | COUNTRY | ALL | NULL | NULL | NULL
+----+-------------+------------+------+---------------+------+---------
2 rows in set (0.02 sec)
Friday, October 29, 2010
12. type
• The access type for the SELECT query
• Various methods:
PREFERENCES SELECT TYPE
BEST system/const
⇩ eq_ref
⇩ ref
⇩ ref_or_null
⇩ index_merge
⇩ range
⇩ index
WORST ALL
Friday, October 29, 2010
14. system/const
• Best types
• system
– From an in memory table
– Has only one row of data
• const
– optimizer knows that it can get at most one row from the
table.
– Primary key or unique key
mysql> EXPLAIN SELECT * FROM Country WHERE Code='USA';
+----+-------------+---------+-------+---------------+---------+---------
| id | select_type | table | type | possible_keys | key | key_len
+----+-------------+---------+-------+---------------+---------+---------
| 1 | SIMPLE | Country | const | PRIMARY | PRIMARY | 3
+----+-------------+---------+-------+---------------+---------+---------
Friday, October 29, 2010
15. eq_ref
• An index lookup that will only return one row
• Only used if both conditions are met
– All parts of a key are used by the JOIN
– Table contains unique, non-nullable key to JOIN on
mysql> EXPLAIN SELECT Country.name FROM Country
-> JOIN City ON City.CountryCode = Country.Code;
+----+-------------+---------+--------+---------------+---------+---------
| id | select_type | table | type | possible_keys | key | key_len |
+----+-------------+---------+--------+---------------+---------+---------
| 1 | SIMPLE | City | ALL | NULL | NULL | NULL
| 1 | SIMPLE | Country | eq_ref | PRIMARY | PRIMARY | 3 |
+----+-------------+---------+--------+---------------+---------+---------
Friday, October 29, 2010
16. ref
• Similar to eq_ref but it can return more then one row
• Selected when
– Either leftmost part of a unique key is used
OR
– A non-unique or non-null key is used
mysql> ALTER TABLE City ADD INDEX (district);
Query OK, 4079 rows affected (0.09 sec)Records: 4079 Duplicates: 0
mysql> EXPLAIN SELECT * FROM City WHERE District = 'California';
+----+-------------+-------+------+---------------+----------+--------
| id | select_type | table | type | possible_keys | key | key_len
+----+-------------+-------+------+---------------+----------+--------
| 1 | SIMPLE | City | ref | District | District | 20
+----+-------------+-------+------+---------------+----------+--------
1 row in set (0.01 sec)
Friday, October 29, 2010
17. ref_or_null
• Again similar to eq_ref and ref but
– Allows null values
OR
– Allows null conditions
mysql> ALTER TABLE Country ADD INDEX (IndepYear);
Query OK, 239 rows affected (0.36 sec)Records: 239 Duplicates: 0
mysql> EXPLAIN SELECT IndepYear FROM Country
-> WHERE IndepYear = 1905 or IndepYear IS NULL;
+----+-------------+---------+-------------+---------------+-----------
| id | select_type | table | type | possible_keys | key |
+----+-------------+---------+-------------+---------------+-----------
| 1 | SIMPLE | Country | ref_or_null | IndepYear | IndepYear |
+----+-------------+---------+-------------+---------------+-----------
1 row in set (0.00 sec)
Friday, October 29, 2010
18. index_merge
• This is the only access type that allows you to use 2
separate indexes for a table with MySQL
• There are lots of rules of when it will be used and in
what conditions – intersection, union
– http://dev.mysql.com/doc/refman/5.0/en/index-merge-optimization.html
mysql> EXPLAIN SELECT * from City where id = 5 or district = 'Michigan'G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: City
type: index_merge
possible_keys: PRIMARY,District
key: PRIMARY,District
key_len: 4,20
ref: NULL
rows: 9
Extra: Using union(PRIMARY,District); Using where
1 row in set (0.00 sec)
Friday, October 29, 2010
19. range
• Access method for a range value in the where clause
(<, <=, >, >=, LIKE, IN or BETWEEN)
– With LIKE you can use the range access only if the first
character is not a wild card character
– Not possible with hash indexes (MEMORY or NDB tables)
mysql> ALTER TABLE City ADD INDEX (population);
Query OK, 4079 rows affected (0.10 sec) Records: 4079 Duplicates: 0
mysql> EXPLAIN SELECT * FROM City WHERE population>10000000;
+----+-------------+-------+-------+---------------+------------+---------
| id | select_type | table | type | possible_keys | key | key_len
+----+-------------+-------+-------+---------------+------------+---------
| 1 | SIMPLE | City | range | Population | Population | 4
+----+-------------+-------+-------+---------------+------------+---------
Friday, October 29, 2010
20. index
• Doing an index scan
• Seen when
– Getting data is slow...
– The data being sought is available through the index
• Different then “Using index” in Extra
mysql> EXPLAIN SELECT id FROM CityG
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: City
type: index
possible_keys: NULL
key: PRIMARY
key_len: 4
ref: NULL
rows: 4079
Extra: Using index
1 row in set (0.00 sec)
Friday, October 29, 2010
21. ALL
• Full table scan
• SLOW
mysql> ALTER TABLE City DROP INDEX Population;
Query OK, 4079 rows affected (0.06 sec)
Records: 4079 Duplicates: 0 Warnings: 0
mysql> EXPLAIN SELECT * FROM City WHERE Population > 10000000G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: City
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 4079
Extra: Using where
1 row in set (0.00 sec)
Friday, October 29, 2010
23. possible_keys
• Provides a list of the available indexes or NULL that
the optimizer considered for use with the query
• Gotcha
– If you have tables/column with different character sets, the
indexes on the tables/column may not not be available for
lookup since it has to convert from one character set to
another.
Friday, October 29, 2010
24. key
• The actual index that was used for the query or NULL
• key and possible_keys are where you start looking at your
indexing strategy
– We will briefly discuss optimizing your indexes a bit later in the talk.
mysql> EXPLAIN SELECT id FROM CityG
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: City
type: index
possible_keys: NULL
key: PRIMARY
key_len: 4
ref: NULL
rows: 4079
Extra: Using index
1 row in set (0.00 sec)
Friday, October 29, 2010
25. key_len
• Shows the number of bytes MySQL will use from the index
• You can use this to see if the entire index or part of an index is
being used for the query.
• Keep in mind that some character sets can use more then one
byte per character.(Ex: UTF8 can use up to 3 bytes per character)
mysql> EXPLAIN SELECT id FROM CityG
*************************** 1. row *
id: 1
select_type: SIMPLE
table: City
type: index
possible_keys: NULL
key: PRIMARY
key_len: 4
ref: NULL
mysql> DESCRIBE CityG
*************************** 1. row *
Field: ID
Type: int(11) ← int is 4 bytes
Null: NO
Key: PRI
Default: NULL
Extra: auto_increment
*************************** 2. row *
...
Friday, October 29, 2010
26. ref
• Very different from the access type “ref”
• Show which columns or constants within the index
that will be used to access the data
mysql> DESCRIBE SELECT * FROM City WHERE ID=3820G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: City
type: const
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: const
rows: 1
Extra:
1 row in set (0.01 sec)
Friday, October 29, 2010
27. rows
• Number of rows MySQL expects to find based on
statistics
– Can be very different then what really shows up
• You can update these statistics with ANALYZE TABLE
but remember that each storage engine updates it's
statistics with varying levels of accuracy.
– Ex: InnoDB statistics are based on 10 random dives rather
then on all the actual data
mysql> EXPLAIN SELECT * FROM City WHERE CountryCode='USA';
+----+-------------+-------+------+---------------+------+---------+------+------+-------
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
+----+-------------+-------+------+---------------+------+---------+------+------+-------
| 1 | SIMPLE | City | ALL | NULL | NULL | NULL | NULL | 4079 | Using
+----+-------------+-------+------+---------------+------+---------+------+------+-------
1 row in set (0.00 sec)
Friday, October 29, 2010
28. filtered
• Since 5.1.12
• Indicates an estimated percentage of table rows that
will be filtered by the table condition.
• Rows shows the estimated number of rows examined
– rows × filtered / 100 shows the number of rows that will be
joined with previous tables.
• Displayed if you use EXPLAIN EXTENDED.
mysql> EXPLAIN extended SELECT * from City where district = 'Michigan';
-------+------+---------------+----------+---------+-------+------+----------+---------
table | type | possible_keys | key | key_len | ref | rows | filtered | Extra
-------+------+---------------+----------+---------+-------+------+----------+---------
City | ref | District | District | 20 | const | 8 | 100.00 | Using
-------+------+---------------+----------+---------+-------+------+----------+---------
Friday, October 29, 2010
29. Extra
• This is where all additional information is located
• Comments you need know about
– “Using index” - you are using a covering index (getting data
from the index rather then the table)
– “Using filesort” - manual sorting in a temporary table was
done rather then using an index for the sort
– “Using temporary” - at some point in the execution a
temporary table was made. If necessary you want to keep it
in RAM (tmp_table_size and max_heap_table_size)
– “Using where” - filtering outside the storage engine.
Friday, October 29, 2010
31. Indexing
• Know your data and queries
– You always have to keep the big picture in mind
– Data changes and the right index now – may not be the right
index later
• Understand how indexes work in MySQL
– Leftmost prefix
– More indexes is not necessarily better
– MyISAM vs. InnoDB
– ANALYZE TABLE
Friday, October 29, 2010
32. Indexing
(con't)
• Shoot for the best EXPLAIN plan
– Use indexes for sorting
– Covering indexes
• Index hinting is possible though highly discouraged
– If you know absolutely that you should be using a specific
index _and_ ANALYZE TABLE is not helping
– Requires that you maintain that query over time since data
changes (can't set it and forget it)
– http://dev.mysql.com/doc/refman/5.1/en/index-hints.html
– also look into STRAIGHT_JOIN
Friday, October 29, 2010
34. Query Tuning
• Use the slow query log
– Don't guess which queries are slow - know!
– You can set a maximum time for a query to run and if it goes
over, log it (long_query_time)
• In 5.1 micro time is available for under 1 sec
– You can tell it to log all queries that do not use indexes
(log_queries_not_using_indexes)
– http://dev.mysql.com/doc/refman/5.0/en/slow-query-log.html
• Don't be afraid of sub-selects, but use them wisely
– Can you write it as a JOIN and if so – does it run faster then the
subselect?
Friday, October 29, 2010
35. Query Tuning
(con't)
• Be willing to consider alternate methods of doing things
– Instead of one big query with lots of derived tables, use a stored
procedure that builds intermittent MEMORY tables that you
index and work with
• Use Query Profiling to get the nitty gritty
– Available from 5.0
– There is a lot of information available using profiling
– http://dev.mysql.com/tech-resources/articles/using-new-query-profiler.html
• Test, Test, Test
– Keep in mind load and concurrency will affect speed
Friday, October 29, 2010