I found EXPLAIN SELECT query very useful in MySQL because it gives information on how SQL will be executed and gives the opportunity to analyze, for e.g., missing indexes you should add in order to improve response BEFORE doing the query itself and analyzing stats.
My question is: In databases like MS Sql, Firebird, Ingres, is there a similar command available?
In Firebird we have PLAN, but is very weak because many times one has to run very long queries in order to view a simple mistake.
Best regards,
Mauro H. Leggieri
In Oracle:
EXPLAIN PLAN FOR SELECT …
In PostgreSQL:
EXPLAIN SELECT …
In SQL Server:
SET SHOWPLAN_XML ON
GO
SELECT …
GO
For mssql server you can use
SET SHOWPLAN_TEXT ON and SET SHOWPLAN_TEXT OFF
this will prevent queries from actually being exectued but it will return they query plan.
For oracle you can use
SET AUTOTRACE ON or EXPLAIN PLAN
(I don't know about firebird or ingres)
In Oracle we have
EXPLAIN PLAN for sql
http://www.adp-gmbh.ch/ora/explainplan.html
In MS SQL Server you can get an text or XML version of the execution plan.
SET SHOWPLAN_XML ON|OFF
SET SHOWPLAN_TEXT ON|OFF
However these are best viewed using the visual tool in Sql Server Management Studio/TOAD.
http://msdn.microsoft.com/en-us/library/ms176058.aspx
Something else that is quite handy is
SET STATISTICS IO ON|OFF
For Ingres, the following will give you the final plan chosen with estimates as to the number of rows, disk IOs and CPU cycles:
set qep
To get the plan but not execute the SELECT also add
set optimizeonly
re-enable query execution:
set nooptimizeonly
to get the the actual statistics for the executed query, to compare with the output from "set qep":
set trace point qe90
See http://docs.ingres.com/Ingres/9.2/SQL%20Reference%20Guide/set.htm for more information on the above.
MS SQL has a utility in Management Studio called Display Execution Plan (Estimated and Exact) when executing a query. it can also display statistics for the query (run time, number of rows, traffic etc )
For Ingres, see also these resources:
Example of Reading and Interpreting a Query Execution Plan (QEP) [pdf]
A brief case study that demonstrates analysis and interpretation of a QEP
Getting Ingres Qep LockTrace Using JDBC
The Query Execution Plan (QEP)
Related
I am trying to calculate some performance metrics for a number of SQL queries. I have found the benchmarking queries for MS SQL Server and I would like the same queries for MySQL Workbench (Windows environment). The queries I am using are the following:
--Elapsed time and CPU time
SET STATISTICS TIME ON;
Query to measure
SET STATISTICS TIME OFF;
-- RAM
select
(physical_memory_in_use_kb)Phy_Memory_usedby_Sqlserver_KB,
(virtual_address_space_committed_kb/1024 )Total_Memory_UsedBySQLServer_MB
from sys. dm_os_process_memory
--HD
sp_msforeachtable N'EXEC sp_spaceused [?]';
Could you please help me to convert the above queries to MySQL?
I would not like to use the reports provided by the Performance Schema of MySQL Workbench since I want to compare the results with the above queries of MS SQL Server.
Thank you in advance
As Jeff pointed out, you can't get exactly those features in MySQL. Nor can you even get close. But, here are some useful things:
Turn on the slowlog with long_quer_time set to 0. Then look in the slowlog for a variety of information about the query.
Use performance_schema (which needs "turning on")
For simple timing, either do it in the client, or do
SELECT SYSDATE(6), ..., SYSDATE(6) FROM ...;
Memory for a specific query is not available. (However, MariaDB has something like that.) Usually a query takes a relatively fixed, and small, amount of memory. Most of what it uses is shared -- various caches.
EXPLAIN SELECT ...
EXPLAIN FORMAT=JSON SELECT ...
Optimizer trace. This needs to be "turned on", and you need to fetch the results.
I was reading an SQL tutorial which used the keyword EXPLAIN to see how a query is executed. I tried it in SQL Server 2008 with no success.
How do I get the equivalent result?
I believe that the EXPLAIN keyword is an MySQL concept - the equivalent Microsoft SQL server concept is the execution plan.
The simplest way of getting an execution plan is to turn on the "Show actual execution plan" menu item (in the query menu) in SQL server management studio. Alternatively you can read a more in-depth guide on execution plans here:
http://www.simple-talk.com/sql/performance/execution-plan-basics/
This article goes into a lot more detail on what execution plans are, how to obtain an execution plan, and the different execution plan formats.
The MySql EXPLAIN statement can be used either as a synonym for DESCRIBE or as a way to obtain information about how MySQL executes a SELECT statement.
The closest equivalent statement for SQL Server is:
SET SHOWPLAN_ALL (Transact-SQL)
or
SET SHOWPLAN_XML (Transact-SQL)
From a SQL Server Management Studio query window, you could run SET SHOWPLAN_ALL ON or SET SHOWPLAN_XML ON and then your query. At that point It will not return the result set of the query, but the actual execution plan. When you then run SET SHOWPLAN_ALL OFF or SET SHOWPLAN_XML OFF and then run your query, you will then again get a result set.
In SSMS (I got 18.3.1) highlight the query in question and hit CTRL+L
(that does what Tobias mentioned - Query->Display Estimated Query Plan)
Be aware that Microsoft added an EXPLAIN command to TSQL syntax in SQL 2012, however it only applies to Azure SQL Data Warehouse and Parallel Data Warehouse - so not the regular RDBMS product.
It provides an execution plan in XML format, and helpfully shows the parts of the plan that will be distributed across the warehouse nodes.
Source:
TSQL EXPLAIN
In oracle sql plus, while doing performance testing, I would do
set autotrace traceonly:
which would display the query plan and statistics without printing the actual results. Is there anything equivalent in mysql?
No, there's no equivalent available in MySQL, at least not in the community edition.
MySQL does not implement the kind of "instrumentation" that Oracle has in its code; so there's no equivalent to an event 10046 trace.
You can preface your SELECT statement with the EXPLAIN keyword, and that will produce output with information about the execution plan that MySQL would use to run the statement, but that's just an estimate, and not a monitoring of the actual execution.
You can also enable the slow query log on the server, to capture SQL statements that take longer than long_query_time seconds to execute, but that really only identifies the long running queries. That would give you the SQL text, along with elapsed time and a count of rows examined.
To get the query plan, just add EXPLAIN to the beginning of a SELECT query.
EXPLAIN SELECT * FROM table
It also estimates the number of rows to be read if that's what statistics you're talking about.
i have mysql that is used on production server for php webshop application.
sometimes it works very slow. so, i will change indexes for several tables.
but before that, i have to make some kind of "snapshot" of current performances (several times per day). after that, i will change indexes, and create new "performance snapshot". then i will made some more changes in database, and made another "performance snapshot".
how can i make that "performance snapshot"? is it possible to use some kind of tool, or to ckeck some logs, or...?
if you can help me how to do that.
thank you in advance!
If you want to buy a commercial product, there is the MySQL Query Analyzer
Otherwise, you could use the SQL Profiler which is already included with MySQL.
The SQL Profiler is built into the database server and can be dynamically enabled/disabled via the MySQL client utility. To begin profiling one or more SQL queries, simply issue the following command:
mysql> set profiling=1;
Thereafter, you will see the duration of each of your queries as you run them.
Slow query log and queries not using indexes
query cache hit rate
innodb monitor
and of course your database hard-disk I/O, memory usage ...
I'm an mssql veteran who's received a job that involves tuning a mysql db. with mssql it was simply a matter of firing up the db profiler and then crunching up the data it collects. I can't seem to find anything similar for mysql.
thanks in advance
You can enable the PROFILER in the mysql query tool and profile individual statements. Also see Using the New MySQL Query Profiler or How to profile a query in MySQL.
You can also use EXPLAIN to get the query optimization plan, but it only works for SELECT queries.