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
2 answers

My SQL query is so slow it times out. Another solution?

This is a rather complicated question, but I will try my best to describe it as well as possible. I currently have 2 tables that are joined on an insta_id. The first table is called Soc_stat and it contains a lot of entries of Instagram posts. The…
2
votes
5 answers

How can I Optimize this Query Taking 30 Seconds for 1746 Rows?

Query: SELECT A.USER_ID, A.ROLE_ID, C.SUBGROUP, MAX(A.STATUS_ID) FROM USER_ROLE A, USER B, ROLE C WHERE A.ROLE_ID = C.ROLE_ID AND C.GROUP_ID = 3 AND A.USER_ID = B.USER_ID …
learn_plsql
  • 1,681
  • 10
  • 28
  • 34
2
votes
2 answers

Building a daily view from a table with an "effective date"

I have a table that uses "start dates" or effective dates. The values in the table take effect from the start date onward, until it is overridden by another entry from the same table with a later start date. My schema and sample data: CREATE TABLE…
mkasberg
  • 16,022
  • 3
  • 42
  • 46
2
votes
4 answers

Getting rid of full index scan

The following query performs badly because of a full non-clustered index scan of 6.5 million records in P4FileReleases followed by a hash join. I'm looking for possible reasons the optimizer picks a scan over a seek. SELECT p4f.FileReleaseID …
Dmitry B.
  • 9,107
  • 3
  • 43
  • 64
2
votes
2 answers

Tuning SQL statement performance

I have two queries that return same results: 1. SELECT DISTINCT cvc.object_id , cvc.object_name FROM ems.ibo_sm_cvc_rfs cvc, ems.ibo_alcatel_mse_locale poi, ems.ibo_nbn_csa csa, …
Andrei Maieras
  • 696
  • 5
  • 15
  • 34
2
votes
1 answer

SQL Server : optimize query. Lots of data

At the beginning I apologize for not being word-perfect in English. I have two tables in my database, one contains questions, and second contains user answers for questions (for statistics). TableA - questions ___________ | ID | Name | TableB -…
mateusz-dot
  • 186
  • 2
  • 9
2
votes
2 answers

Trial column in client statictics SQL

I start attach Client Statistics to examinate my queries. There are "Trial x" columns, What do they mean? I use SQL Server 2014
Jacek
  • 11,661
  • 23
  • 69
  • 123
2
votes
1 answer

Why is subselect with CASE is faster than JOIN WITH OR in Oracle

I was optimizing one of horrible views we have and it came as surprise that one of subselects with CASE statements was running faster than LEFT JOIN with OR. Original view is substantially bigger but parts that I am interested in can be boiled down…
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
2
votes
4 answers

Need filtered result optimised query

I have one table called Device: Device Table id deviceName genId description overview 1 PV motor 5 motor capacity 5 motor load is good 2 puf pannel 6 puf capacity 2 …
2
votes
2 answers

Does query plan optimizer works well with joined/filtered table-valued functions?

In SQLSERVER 2005, I'm using table-valued function as a convenient way to perform arbitrary aggregation on subset data from large table (passing date range or such parameters). I'm using theses inside larger queries as joined computations and I'm…
2
votes
1 answer

Optimizing a table for the latest/last rows in Azure SQL Server

I have a table on a MS Azure SQL DB with 60,000 rows that is starting to take longer to execute with a SELECT statement. The first column is the "ID" column which is the primary key. As of right now, there is no other indexes. The thing about this…
2
votes
1 answer

Use a Select Statement within a wildcard where clause

Working in MS SQL 2005 and I want to use a select statement within a wildcard where clause like so: SELECT text FROM table_1 WHERE ID LIKE '%SELECT ID FROM table_2%' I'm looking for product ids within a large body of text that is held in a DB. The…
Spencer Carnage
  • 2,056
  • 2
  • 28
  • 41
2
votes
1 answer

Optimize MYSQL Query with Order by

I have seen mysql queries with order by runs slow. Is there any specific way to optimize queries which use order by ? Queries without order by run very fast but with order by its always runs slow. if any one suggest any thing on this as general…
Victor
  • 21
  • 1
2
votes
2 answers

Execution sequence of IN and JOIN in MySQL

I have a SQL query which looks like the following. SELECT A.a, count(B.id) FROM TableA A inner join TableA B on A.referId = B.id WHERE A.id in (123,2424,232...) GROUP BY A.id While executing this query The rows are filtered using where clause…
Ravi Chandra
  • 677
  • 12
  • 24
2
votes
1 answer

Testing my website for database performance & traffic handling

I am developing a website using Django1.7 on python3.4 along with MySql as database engine. For the next 15-20 days I am planning to test it. The site is something like linkedin in terms of functionality and complexity and I am expecting to get…
Rohit
  • 475
  • 1
  • 7
  • 16
1 2 3
99
100