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
2
votes
1 answer

How to optimize oracle query?

Following query is taking around 45 seconds in oracle 11g select count(cap.ISHIGH),ms.SID,ms.NUM from CDetail cap,MData ms where cap.MDataID_FK=ms.MDataID_PK and trunc(cap.CREATEDTIME) between trunc(sysdate-10) and trunc(sysdate) group by…
2
votes
5 answers

Optimize MySQL self-join query

I have c_regs table that contains duplicate rows. I've created index on form_number and property_name columns. Unfortunately this query still taking to-o-o-o long to complete, especially with addition of t10 and t11 joins. Is there a way to optimize…
demisx
  • 7,217
  • 4
  • 45
  • 43
2
votes
1 answer

SQL query optimization join union distinct

I have a query: Select A.col1 as col1, B.col5 as col2, C.col10 as col3 FROM Table A JOIN Table B on(A.col2 = B.col2) JOIN table C on(C.col1 = B.col3) UNION SELECT A.col1 as col1, B.col5 as col2, NULL as col3 FROM Table A JOIN…
2
votes
1 answer

Reusing query plan in SQL Server 2012

I'm using SQL Server and I want to benefit from reusing query plan. I found this document, but it remains unclear for me whether the plan for my query is being reused or not. declare @su dbo.IntCollection -- TABLE (Value int not null) insert…
2
votes
0 answers

Full Text Search using Indexing

Can i create a full-text Search index on Non unique column having duplicate values? We have sql query in which we need to get distinct records based on LIKE operator and we have approx 20 million data in a table and our requirement is get final…
2
votes
1 answer

10 Million rows taking 3.50 seconds for a simple type query

I'm new to database query optimization. Here is the create table query: CREATE TABLE mo ( id int UNSIGNED NOT NULL auto_increment, msisdn varchar(20) NOT NULL, operatorid int UNSIGNED NOT NULL, shortcodeid int UNSIGNED NOT NULL, text…
Code Diary
  • 417
  • 2
  • 7
  • 17
2
votes
2 answers

Which query is better

EXPLAIN EXTENDED SELECT id, name FROM member INNER JOIN group_assoc ON ( member.id = group_assoc.member_id AND group_assoc.group_id =2 ) ORDER BY registered DESC LIMIT 0 , 1 Outputs: id select_type table type possible_keys key key_len ref…
Ben
  • 60,438
  • 111
  • 314
  • 488
2
votes
2 answers

Optimal Way to Avoid Duplicate SubQuery in SQL

I have a simple two column table. For example's sake we can use the following to build the data: CREATE TABLE Duplicates (assignmentid varchar(5), questionid varchar(5)); INSERT INTO Duplicates (assignmentid, questionid) VALUES ('aaaaa',…
Kumar
  • 23
  • 1
  • 3
2
votes
1 answer

Optimizing Hive GROUP BY when rows are sorted

I have the following (very simple) Hive query: select user_id, event_id, min(time) as start, max(time) as end, count(*) as total, count(interaction == 1) as clicks from events_all group by user_id, event_id; The table has the following…
Alejandro Piad
  • 1,827
  • 1
  • 16
  • 23
2
votes
2 answers

Optimization of 2 unrelated table join

I have an interface in which I will show a list of items from a SQLite database that each item can be one of two types: SubItem1 and SubItem2. This database currently has three tables: Items, SubItem1 and SubItem2. The table Items contains the…
2
votes
2 answers

Query Speed Issue with NOT EXISTS condition

I have a query that works, but it is slow. Is there a way to speed this up? Basically I have a table with timecard entries, and then a second table with time breakdowns of that entry, related by the TimecardID. What I am looking for is timeblocks…
2
votes
3 answers

MySQL Removing duplicates based on condition and multiple columns combinations

I have a table in MySQL as below: ID, COL1, COL2 VALUE '1', 'OBJ1', 'OBJ2', '5' '2', 'OBJ1', 'OBJ2', '1' '3', 'OBJ2', 'OBJ1', '3' '4', 'OBJ3', 'OBJ1', '4' '5', 'OBJ3', 'OBJ4', '6' Relation between col1 and col2 is independent of position, ie OBJ1…
Aybid
  • 86
  • 1
  • 8
2
votes
0 answers

How to estimate the size and the hashkeys value of cluster

I would love to understand more how I should calculate or estimate the size and the haskeys value when creating a cluster ? Should it be based on table size? Column size? I'm aware is documented Here but I still don't understand it properly…
napi15
  • 2,354
  • 2
  • 31
  • 55
2
votes
1 answer

PostgreSQL query performance and possible optimisation

I managed to write my query to get me correct data but to me it looks pretty bad since i had to use query inside query 3 times and even doe query performance is fine now around 700msec i am afraid it will slow down in future when there will be more…
2
votes
3 answers

Fast complex query to select bookings

I'm trying to write a query to get a courses information and the number of bookings and attendees. Each course can have many bookings and each booking can have many attendees. We already have a working report, but it uses multiple queries to get the…
Petah
  • 45,477
  • 28
  • 157
  • 213