Questions tagged [query-optimization]

For questions about making database queries run faster. PLEASE INCLUDE QUERY PLANS, AND TABLE AND INDEX DEFINITIONS in your question. Please use additional tags to specify your query language and server maker. Some of these questions may belong on https://DBA.stackexchange.com, especially if they involve reindexing, query plans, etc.

Please include table and index definitions for all relevant tables in your question. Please also include the query plan: the EXPLAIN output. Read on for more explanation.

If you use this is a good guide to pulling together the right information for your question.

SQL is a declarative language that can have many ways to find the same data. As a SQL programmer, you declare what you want from it, and it figures out how to get your result. Because of how databases are structured, and how they retrieve data, the same results can be retrieved in various ways that the query interpreter needs to choose between.

Using sargable predicates, using indexes, and optimizing how the query works can make it run more quickly. Restructuring the query, removing unnecessary portions of the query, or using different features of different Database Engines can all be helpful.

If the question is not (just) about how to write the query, but involves how to create new indexes, or how to optimize the server, you may get good answers on https://DBA.stackexchange.com.

Your database's query planner, also known as its query optimizer module attempts to determine the most efficient way to execute a given query by considering possible query plans.

You'll hear a lot about query planners in this tag. Each DBMS software product has its own query planner. Here's a description of one query planner.

How to ask a good query-optimization question

If you're asking "Why is my SQL query so slow?" or something like that, you need to do a little extra homework to get good answers from the Stack Overflow community. Please include this information.

What database and version?

Put this tag on your question along with the tag for your database: , . , or whatever database you use. Don't use more than one database tag, please. Optimization works somewhat differently from database to database. And, please mention the version of your database server.

How many rows and how fast?

Please figure out approximately how many rows you expect in your result set, and approximately how many rows are in each table involved with the query. Mention those numbers in your question. Please mention how long you hope your query needs to take, and how long it's taking. (That is, tell us what you mean by slow).

Don't oversimplify

Don't try to simplify your question by replacing your actual SELECT column lists with *. Using * in SELECT clauses can be harmful to query performance compared to specifying the list of columns you need.

Show your table definitions, with indexes

In your question include your table definitions and index definitions for each table and view involved in your query. Make sure your question shows all indexes in all relevant tables. When you're asking a performance question, please don't oversimplify your question by omitting indexes or columns you think might be irrelevant. The strangest things can be relevant to performance!

Very important: the names and data types of columns included in each index, with their order.

Show your database's explanation of how it satisfied your query

In your question please show the EXPLAIN PLAN output (it's just EXPLAIN or SHOWPLAN in some table server makes and models) for your query.

Things to know

  • Creating an appropriate index for a slow query is often, not always, a good solution to a problem. Answers on will often contain a suggestion for an index.

  • Avoid creating indexes on lots of single columns. Single-column indexes are often not very useful to speed up complex queries.

  • As your database grows you will, most likely, discover new slow queries. Operations can be trivial on hundreds of rows, but much more time-consuming on tens of thousands of rows. In a growing database, plan to review performance issues routinely.

  • If you use SSMS -- Microsoft's SQL Server Management Studio -- this tip is for you: Right-click in a query window, then select Show Actual Execution Plan, then run the query. The execution plan display sometimes recommends a new index to create.

Things you might do when optimizing queries

  1. Join ordering
  2. Query planning for nested SQL queries
  3. Cost estimation
  4. Parametric Query Optimization
  5. Multi-Objective Query Optimization
  6. Multi-Objective Parametric Query Optimization

Reference

Please be aware that query performance optimization is an inexact science. This is a good reference. http://use-the-index-luke.com/

7749 questions
9
votes
3 answers

Can MySQL use index in a RANGE QUERY with ORDER BY?

I have a MySQL table: CREATE TABLE mytable ( id INT NOT NULL AUTO_INCREMENT, other_id INT NOT NULL, expiration_datetime DATETIME, score INT, PRIMARY KEY (id) ) I need to run query in the form of: SELECT * FROM…
Continuation
  • 12,722
  • 20
  • 82
  • 106
9
votes
3 answers

Multithreading in MySQL?

Are MySQL operations multithreaded? Specifically, on running a select, does the select (or join) algorithm spawn multiple threads to run together? Would being multi-threaded prevent being able to support a lot of concurrent users?
chustar
  • 12,225
  • 24
  • 81
  • 119
9
votes
1 answer

Postgresql compare 2 querys for optimization

I just created a couple of queries that bring the same data but in a different. the first one uses a sub query and the second one uses a self join strategy. checking the documentation, i found the ANALYZE and EXPLAIN commands, Now i'm trying to…
OJVM
  • 1,403
  • 1
  • 25
  • 37
9
votes
2 answers

PostgreSQL getting daily, weekly, and monthly averages of the occurrences of an event in one query

Currently I have this rather large query that works by Aggregating the daily, weekly, monthly counts into intermediate tables by taking the count() of an event grouped by the event name and the date. Selecting the avg count over each intermediate…
m0meni
  • 16,006
  • 16
  • 82
  • 141
9
votes
2 answers

MySQL index for MIN and MAX

Could anyone clarify this point from the official MySQL documentation Indexes are used ... To find the MIN() or MAX() value for a specific indexed column key_col. This is optimized by a preprocessor that checks whether you are using WHERE…
super.t
  • 2,526
  • 7
  • 32
  • 51
9
votes
8 answers

ORA-03113 while executing a sql query

I have a 400 line sql query which is throwing exception withing 30 seconds ORA-03113: end-of-file on communication channel Below are things to note: I have set the timeout as 10 mins There is one last condition when removed resolves this…
Ravi Gupta
  • 4,468
  • 12
  • 54
  • 85
9
votes
4 answers

MySQL indexes - what are the best practices according to this table and queries

i have this table (500,000 row) CREATE TABLE IF NOT EXISTS `listings` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `type` tinyint(1) NOT NULL DEFAULT '1', `hash` char(32) NOT NULL, `source_id` int(10) unsigned NOT NULL, `link`…
Mostafa Elkady
  • 5,645
  • 10
  • 45
  • 69
9
votes
2 answers

How to throttle or prioritize a query in MySql

Is there anyway to prioritize or throttle a query at all in MySQL? I'm a DBA on a server that sees a lot of unoptimized queries come into the server and they just destroy the CPU. I'm looking at throttling certain users that hit on the database in…
Tim Ferrell
  • 1,348
  • 3
  • 17
  • 40
9
votes
6 answers

SQL Server: IF EXISTS massively slowing down a query

(SQL Server 2012 being used) I found some topics on query optimization, and comparing EXISTS to COUNT, but I couldn't find this exact problem. I have a query that looks something like this: select * from tblAccount as acc join tblUser as user on…
John Darvill
  • 1,274
  • 11
  • 17
9
votes
4 answers

What's the difference between !col and col=false in MySQL?

The two statements have totally different performance: mysql> explain select * from jobs where createIndexed=false; +----+-------------+-------+------+----------------------+----------------------+---------+-------+------+-------+ | id |…
Mask
  • 33,129
  • 48
  • 101
  • 125
9
votes
1 answer

Under what circumstances would likelihood() be useful?

Reading through the sqlite documentation I found the following function: http://www.sqlite.org/lang_corefunc.html#likelihood The likelihood(X,Y) function returns argument X unchanged. The value Y in likelihood(X,Y) must be a floating point…
metrix
  • 1,486
  • 2
  • 11
  • 19
9
votes
1 answer

Avoid Naming User Stored Procedures SP% or SP_%

Does a user stored procedure with the prefix of SP (not SP_) have poorer performance by looking in the Master DB (similar to user SP named SP_) or does it look in the DB where the stored procedure exists because it lack an underscore?
KOddo
  • 107
  • 1
  • 8
9
votes
4 answers

Does size of a VARCHAR column matter when used in queries

Possible Duplicate: is there an advantage to varchar(500) over varchar(8000)? I understand that a VARCHAR(200) column containing 10 characters takes same amount of space as a VARCHAR(20) column containing same data. I want to know if changing a…
Salman A
  • 262,204
  • 82
  • 430
  • 521
9
votes
1 answer

Optimize query for table with hundreds of millions of rows

this feels like a "do my homework for me" kind of question but I'm really stuck here trying to make this query run quickly against a table with many many rows. Here's a SQLFiddle that shows the schema (more or less). I've played with the indexes,…
Nick Spacek
  • 4,717
  • 4
  • 39
  • 42
9
votes
1 answer

Why is MAX() 100 times slower than ORDER BY ... LIMIT 1?

I have a table foo with (among 20 others) columns bar, baz and quux with indexes on baz and quux. The table has ~500k rows. Why do the following to queries differ so much in speed? Query A takes 0.3s, while query B takes 28s. Query A select baz from…
Viktor Dahl
  • 1,942
  • 3
  • 25
  • 36