What information should be provided when asking how to improve the performance of a query?
3 Answers
As many of the following as possible:
- Formatted text of the query/procedure/function.
- Indenting SQL by 4+ spaces on each line is usually best.
- Schema (CREATE TABLE); use text, not images
- Table, index, and constraint definitions (scripts).
- Even better: A link to a demo that reproduces the core of the problem.
- The performance or query-optimization tag.
- Query plans
- Ideally the post-execution variety, containing runtime information.
- Estimated execution plans (if obtaining a post-execution plan is not practical).
- Anonymized plans are acceptable but may limit answer quality.
- A picture of execution is occasionally enough (but discouraged).
- Environment
- Database product, precise version, and edition.
- Hardware (e.g. memory/CPU/storage) and OS information.
- About the issue
- What is the intent of the query
- Current and desired performance levels.
- Things you can and cannot change (e.g. can add indexes, cannot change the query).
- The tuning steps followed so far.
Further reading
-
3I'm hoping to keep this answer to a manageable size. Please contribute, but prioritize brevity over comprehensiveness. Feel free to add additional links to the Further reading section at the end.– Paul White ModCommented Aug 27, 2018 at 7:35
-
"Maneageable size" -- I added structure since there are only about 6 things. I can answer most query performance questions given only the first 2. (The 3rd, a tag, is to get my attention.) Commented Sep 10, 2021 at 15:57
Instructions for PostgreSQL Performance Questions
Copied from the postgresql-performance tag info.
About improving / explaining the performance of existing queries. May concern particular query techniques or the setup of your hard- and software. Tag with postgresql as well.
Questions about trivial queries should rather go to stackoverflow.com. See DBA Help about suitable questions.
Consider basic advice on Performance Optimization and Slow Query Questions in the PostgreSQL Wiki, including the section "Things to try before you post". EXPLAIN
is particularly important.
When posting questions, include:
Your Postgres version, at least the major version like "15" or "9.6". (Since Postgres 10, the major version is just the leading number.) Find out with:
SELECT version();
Your actual query / queries. In a readable format and as brief as possible, but don't remove anything that might be relevant. Describe the expected result. Include an example for simple cases.
Table definition(s): Preferably a
CREATE TABLE
script showing data types and constraints for relevant columns andCREATE INDEX
scripts for relevant indexes. (The output from\d+ tablename
inpsql
is a less useful fallback.)Cardinalities (rough number of rows) in involved tables. Cardinalities (rough number of distinct values) and typical distribution in crucial columns.
Query plan(s) obtained with
EXPLAIN (BUFFERS, ANALYZE, SETTINGS)
. (Before Postgres 12 justEXPLAIN (BUFFERS, ANALYZE)
.)
Consider pasting plans on explain.depesz.com, and include the resulting link.Link to an online demo at db<>fiddle or similar, populated with the schema, some sample data, and the query.
Only where relevant, a brief mention of your hardware and system, like: "CentOS 6.1, Xeon E5-2450 with 64GB RAM, 4-disk RAID 10 of Intel X-25E SSDs". Or more details if your question is about hardware.
Taken from a comment by Wilson Hauck on a MySQL performance issue question.
Instructions for MySQL Performance Questions
Computer specifications
- RAM size
- SSD/NVME versus HDD on MySQL Host server
Please post on pastebin.com and share the links. (Or similar product.)
Additionally from your SSH login root, text results of:
SHOW GLOBAL STATUS;
(after minimum 24 hours UPTIME)SHOW GLOBAL VARIABLES;
SHOW FULL PROCESSLIST;
a complete MySQLTuner report
- Windows version: github.com/pmachapman/mysqltuner
- Linux version: MySQLTuner-perl
SHOW ENGINE INNODB STATUS;
SELECT name, count FROM information_schema.innodb_metrics ORDER BY name;
required for server workload tuning analysis to provide suggestions.Enable the mysql slow query log (MySQL-5.7, MySQL-8.0, MariaDB). This can be done at runtime (in addition to the config file for next restart):
SELECT @@slow_query_log_file, @@log_output;
to see what the filename (if@@log_output
is FILE) will be. Change if required before enabling withslow_query_log
.SELECT @@long_query_time;
to see what the time in seconds for the slow queries are. The default of 10 seconds is rarely useful; change to1
.SET GLOBAL slow_query_log=1 /*M!100005 , GLOBAL log_slow_verbosity='query_plan,explain' */;
- use mysqldumpslow -s t or pt-query-digest to summarize the important ones.
- Post the most important queries along with
EXPLAIN {query}
andSHOW CREATE TABLE {tablename}
for each table in the query. (Please do not use the less-descriptiveDESCRIBE
.)
Please use text, not images.
-
1You need also to post an EXPLAIN SELECT .... to see if and which indexes where used. Further a cull CREATE TABLE with INDEXES is qalso helpful.– nbkCommented Sep 27, 2020 at 20:06
-
@nbk You can add the details to the answer yourself. It's a community wiki answer and as such can be edited by anyone/everyone. Commented Sep 28, 2020 at 8:04