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
19
votes
4 answers

SQL performance MAX()

Just got a small question. When trying to get a single max-Value of a table. Which one is better? SELECT MAX(id) FROM myTable WHERE (whatever) or SELECT TOP 1 id FROM myTable WHERE (whatever) ORDER BY id DESC I'm using Microsoft SQL Server 2012
Ole Albers
  • 8,715
  • 10
  • 73
  • 166
18
votes
5 answers

PL/SQL Performance Tuning for LIKE '%...%' Wildcard Queries

We're using Oracle 11g database. As you may or may not know, if you use wildcard query with "%" in front of the string, the column index is not being used and a full table scan is happening. It looks like there isn't a definitive suggestion on how…
Ruslan
  • 9,927
  • 15
  • 55
  • 89
18
votes
2 answers

Force MySQL to use two indexes on a Join

I am trying to force MySQL to use two indexes. I am joining a table and I want to utilize the cross between the two indexes. The specific term is Using intersect and here is a link to MySQL documentation:…
Spencer
  • 21,348
  • 34
  • 85
  • 121
18
votes
2 answers

Increment counter or insert row in one statement, in SQLite

In SQLite, given this database schema CREATE TABLE observations ( src TEXT, dest TEXT, verb TEXT, occurrences INTEGER ); CREATE UNIQUE INDEX observations_index ON observations (src, dest, verb); whenever a new observation tuple…
zwol
  • 135,547
  • 38
  • 252
  • 361
18
votes
2 answers

Mysql Explain Query with type "ALL" when an index is used

I ran a query in Mysql like below: EXPLAIN SELECT * FROM( SELECT * # Select Number 2 FROM post WHERE parentid = 13 ORDER BY time, id LIMIT 1, 10 ) post13_childs JOIN post post13_childs_childs ON…
ahoo
  • 1,321
  • 2
  • 17
  • 37
18
votes
5 answers

Questionable SQL practice - Order By id rather than creation time

So I have an interesting question that I am not sure is considered a 'hack' or not. I looked through some questions but did not find a duplicate so here it is. Basically, I need to know if this is unreliable or considered bad practice. I have a very…
Alex Naspo
  • 2,052
  • 1
  • 20
  • 37
18
votes
4 answers

How can i force sql server to execute subquery first and filter the 'where' statement

i have a query like this: select * from ( select * from TableX where col1 % 2 = 0 ) subquery where col1 % 4 = 0 The actual subquery is more complicated. when i execute the subquery alone it returns maybe 200rows quickly, but when i execute…
Narutokk
  • 964
  • 1
  • 8
  • 20
17
votes
3 answers

SQL speed up performance of insert?

I am performing some test on sql server and I want to get the best insert speed possible. The statement I use is something like this: INSERT INTO db_Test_databse..tbl_test with(rowlock) ( joinid, date_key, col1, col2,col3,col4,…
Corovei Andrei
  • 1,646
  • 6
  • 28
  • 42
17
votes
3 answers

Selecting COUNT from different criteria on a table

I have a table named 'jobs'. For a particular user a job can be active, archived, overdue, pending, or closed. Right now every page request is generating 5 COUNT queries and in an attempt at optimization I'm trying to reduce this to a single…
nzifnab
  • 15,876
  • 3
  • 50
  • 65
17
votes
7 answers

For autoincrement fields: MAX(ID) vs TOP 1 ID ORDER BY ID DESC

I want to find the highest AutoIncremented value from a field. (its not being fetched after an insert where I can use @@SCOPE_IDENTITY etc) Which of these two queries would run faster or gives better performance. Id is the primary key and…
Binoj Antony
  • 15,886
  • 25
  • 88
  • 96
17
votes
3 answers

MySql.Data.MySqlClient.MySqlException: Timeout expired

In recent times, a particular page in my web app throws the Exception Details: MySql.Data.MySqlClient.MySqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. Though…
Gopi
  • 5,656
  • 22
  • 80
  • 146
17
votes
6 answers

Mysql query with Left Join is too very slow

Query: select `r`.`id` as `id` from `tbl_rls` as `r` left join `tblc_comment_manager` as `cm` on `cm`.`rlsc_id` != `r`.`id` Both tables have 8k records but why is it very slow, taking 2-3 minutes and more sometimes? OMG , this query makes…
Arshdeep
  • 4,281
  • 7
  • 31
  • 46
17
votes
2 answers

Does SQL Server short-circuit IF statements?

I am working on optimizing some heavily used stored procedures and ran across a scenario that raised a question that I couldn't find any answers for: when evaluating TSQL in a stored procedure, does SQL Server short-circuit the IF statement? For…
competent_tech
  • 44,465
  • 11
  • 90
  • 113
17
votes
3 answers

MySQL WHERE NOT IN extremely slow

Below is a SQL statement inside a stored procedure (truncated for brevity): SELECT * FROM item a WHERE a.orderId NOT IN (SELECT orderId FROM table_excluded_item); This statement takes 30 seconds or so! But if I remove the inner SELECT query, it…
pixelfreak
  • 17,714
  • 12
  • 90
  • 109
17
votes
1 answer

Optimize Postgres query on timestamp range

I have the following table and indices defined: CREATE TABLE ticket ( wid bigint NOT NULL DEFAULT nextval('tickets_id_seq'::regclass), eid bigint, created timestamp with time zone NOT NULL DEFAULT now(), status integer NOT NULL DEFAULT 0, …