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
22
votes
6 answers

How to make JOIN query use index?

I have two tables: CREATE TABLE `articles` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(1000) DEFAULT NULL, `last_updated` datetime DEFAULT NULL, PRIMARY KEY (`id`), KEY `last_updated` (`last_updated`), ) ENGINE=InnoDB…
Silver Light
  • 44,202
  • 36
  • 123
  • 164
22
votes
2 answers

MySQL EXPLAIN UPDATE

I am trying to answer the following question as part of my college revision: Create an index on at least one attribute of a table in the ‘employees’ database, where you use the MySQL ‘EXPLAIN’ tool to clearly show the benefit (in terms or…
jim
  • 8,670
  • 15
  • 78
  • 149
22
votes
1 answer

How to optimise MySQL queries based on EXPLAIN plan

Looking at a query's EXPLAIN plan, how does one determine where optimisations can best be made? I appreciate that one of the first things to check is whether good indexes are being used, but beyond that I'm a little stumped. Through trial and error…
anon
21
votes
3 answers

Optimizing Execution Plans for Parameterized T-SQL Queries Containing Window Functions

EDIT: I've updated the example code and provided complete table and view implementations for reference, but the essential question remains unchanged. I have a fairly complex view in a database that I am attempting to query. When I attempt to…
John Bledsoe
  • 17,142
  • 5
  • 42
  • 59
21
votes
1 answer

How to estimate SQL query timing?

I'm trying to get an rough (order-of-magnitude) estimate of how long time the following query could take: mysql> EXPLAIN SELECT t1.col1, t1_col4 FROM t1 LEFT JOIN t2 ON t1.col1=t2.col1 WHERE col2=0 AND col3 IS…
qazwsx
  • 25,536
  • 30
  • 72
  • 106
21
votes
4 answers

How can I further optimize a derived table query which performs better than the JOINed equivalent?

UPDATE: I found a solution. See my Answer below. My Question How can I optimize this query to minimize my downtime? I need to update over 50 schemas with the number of tickets ranging from 100,000 to 2 million. Is it advisable to attempt to set all…
hobodave
  • 28,925
  • 4
  • 72
  • 77
20
votes
2 answers

Multiple Column Index vs Multiple Indexes

I have the following table in a MySQL database: CREATE TABLE `secondary_images` ( `imgId` int(10) unsigned NOT NULL AUTO_INCREMENT, `primaryId` int(10) unsigned DEFAULT NULL, `view` varchar(255) DEFAULT NULL, `imgURL` varchar(255) DEFAULT…
stefmikhail
  • 6,877
  • 13
  • 47
  • 61
20
votes
2 answers

Rails 3 Database Indexes and other Optimization

I have been building rails apps for a while now, but unfortunately for me, none of my apps have had a large amount of data or traffic. But now I have one that is gaining steam. So I am diving in head first into scaling and optimizing my app. It…
20
votes
8 answers

Why is doing a top(1) on an indexed column in SQL Server slow?

I'm puzzled by the following. I have a DB with around 10 million rows, and (among other indices) on 1 column (campaignid_int) is an index. Now I have 700k rows where the campaignid is indeed 3835 For all these rows, the connectionid is the same. I…
Toad
  • 15,593
  • 16
  • 82
  • 128
20
votes
5 answers

How to make ORDER BY in JOIN query faster? Nothing I have tried has worked

I have the following JOIN query: SELECT table1.*, table2.* FROM Table1 AS table1 LEFT JOIN Table2 AS table2 USING (col1) LEFT JOIN Table3 as table3 USING (col1) WHERE 3963.191 * ACOS( (SIN(PI() *…
ProgrammerGirl
  • 3,157
  • 7
  • 45
  • 82
19
votes
4 answers

Calculate number of concurrent events in SQL

I have a table that holds phone calls, with the following fields: ID STARTTIME ENDTIME STATUS CALL_FROM CALL_TO There are 2,9 million records loaded into a local PostgreSQL database. I added indexes on ID (unique index), starttime and…
Sologoub
  • 5,312
  • 6
  • 37
  • 65
19
votes
4 answers

What do you do to make sure a new index does not slow down queries?

When we add or remove a new index to speed up something, we may end up slowing down something else. To protect against such cases, after creating a new index I am doing the following steps: start the Profiler, run a SQL script which contains…
A-K
  • 16,804
  • 8
  • 54
  • 74
19
votes
3 answers

Need help to optimize MySQL query

I have 6 tables: CREATE TABLE IF NOT EXISTS `sbpr_groups` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `active` tinyint(1) DEFAULT '0', `dnd` tinyint(1) DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT…
Arthur Halma
  • 3,943
  • 3
  • 23
  • 44
19
votes
2 answers

mysql, ifnull vs coalesce, which is faster?

if it's known that there are only two values to candidate for the result of a column, ifnull(a, b) as a_or_b_1 and coalesce(a, b) as a_or_b_2 will give the same result. but which is faster? when searching i found this article, which says ifnull is…
davogotland
  • 2,718
  • 1
  • 15
  • 19
19
votes
2 answers

CPU Time or Elapsed Time - Which actually means SQL Query's Performance?

I have a SQL server 2012 table with 2697 Records and the table is not indexed. The data will get increased in future up to 100k records. I am not joining any other table with this one to retrieve records. Initially I created a user defined function…
Karthik Venkatraman
  • 1,619
  • 4
  • 25
  • 55