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

What's the fastest way to see if a table has no rows in postgreSQL?

I have a bunch of tables that I'm iterating through, and some of them have no rows (i.e. just a table of headers with no data). ex: SELECT my_column FROM my_schema.my_table LIMIT 1 returns an empty result set. What is the absolute fastest way to…
singmotor
  • 3,930
  • 12
  • 45
  • 79
2
votes
2 answers

Mysql with big tables: how to optmize this query?

I have a table using InnoDB that stores all messages sent by my system. Currently the table have 40 million rows and grows 3/4 million per month. My query is basically to select messages sent from an user and within a data range. Here is a…
Fernando
  • 4,459
  • 4
  • 26
  • 39
2
votes
1 answer

Optimize sql except query

I have a query that needs to be optimized and im not sure how. This is the sql script: declare @tempTable table(color1 int, color2 int, color3 int, color4 int, newToken uniqueidentifier default newid(), ordinal int identifier(1,1)) insert into…
Ikraam MX
  • 140
  • 12
2
votes
3 answers

Resusing select subquery/result

I am trying to optimize the speed of a query which uses a redundant query block. I am trying to do a row-wise join in sql server 2008 using the query below. Select * from () cq join table1 t1 on (cq.id=t1.id) union Select * from …
Whimsical
  • 5,985
  • 1
  • 31
  • 39
2
votes
1 answer

mysql difference in number of rows scanned

I have a query : mysql> explain SELECT * FROM OTHERS_TINY_URL_TBL WHERE LINK_TYPE = 'BITLY' AND URL_SHORTNER_ID = '5434e3b9e4b03aa06f25da11' AND MODIFIED_TM >= '2016-03-13 21:09:43' AND MODIFIED_TM <= '2017-03-13…
sudeepdino008
  • 3,194
  • 5
  • 39
  • 73
2
votes
8 answers

Tune up my sql query for better perfomance

select top 1 col1 from table1 order by CreatedOn desc This query takes 6 minutes on SQL Server 2005 to execute. The table contains about 25,00,000 rows and number of total columns is 36, but as shown above I am just retrieving a single column…
Pratik
  • 11,534
  • 22
  • 69
  • 99
2
votes
2 answers

How to remove redundant conditions in **IN** query Sql

I have this kind of query. But i need to optimize this query so how can omit redundant conditions with same split function. DECLARE @Filter nvarchar(20) SELECT @Filter ='5,22,3' SELECT * FROM Employee e WHERE e.code IN …
shaair
  • 945
  • 12
  • 24
2
votes
1 answer

MySql Delete Taking A Long Time

I have a query that is deleting a set of 300 records based on the primary key. The table is about 250,000 records and four columns (int PK, varchar, date, tinyint), so it should be a pretty manageable size, however, it takes around 2 minutes to…
maembe
  • 1,270
  • 1
  • 13
  • 25
2
votes
1 answer

Log join and where clause execution time in Oracle

I would like to ask for some help with a query in Oracle Database. I have a massive select with multiple tables joined together (over 10) and multiple where clauses applied (10-20). Some tables have 10 columns, some has 300+. Most tables have 10+…
2
votes
0 answers

PostgreSQL With vs SubQuery

I have these two queries with similar Query Plan outputs: update cartfund A set "CartId" = (select id from cart where "UserId" = 6) from cart B where A."CartId" = B.id and B."GuestUserId" = 1139; Query Plan for above: Update on cartfund a …
myusuf
  • 11,810
  • 11
  • 35
  • 50
2
votes
2 answers

Optimize an ORDER BY query

I'm at a loss. I have a table with about 100K rows. When querying this table results are usually snappy, about 2ms or so. But whenever I use an ORDER BY performance drops like a rock to about 120ms. I read the MySQL ORDER BY Optimization page but I…
harm
  • 10,045
  • 10
  • 36
  • 41
2
votes
3 answers

optimizing SQL query with conditional join

I need to run a query to compare two tables and return the records that are not in the second table. The tricky part is that the link between the two table is conditional. I have multiple sources feeding into Table2 and that is then feeding into…
Sandy DeLeon
  • 622
  • 6
  • 17
2
votes
1 answer

How to optimize huge query with repeated subqueries

I have the following huge query that contains repeated subqueries , It looks really inefficient to me. How can i optimize it ? SELECT T2.date1, T2.date2, T2.period, T1.market, T1.ticker, 0 AS scenario FROM (SELECT DISTINCT Q.market AS…
3ashmawy
  • 469
  • 1
  • 7
  • 16
2
votes
1 answer

Oracle index containing all columns of another index, right or wrong

I am working on an Oracle 12c database. I have a table being queried a lot. One query uses columns A and B in where clause, another query uses A,B,C,D & E columns in the where clause. I want to speed up queries. I have added 2 non-unique indexes,…
2
votes
1 answer

Slow Join on Composite Primary Key

I have the following query, its runtime is ~2 seconds until I join ProductStores on StoreID which increases it to ~3 minutes, joining only on ProductID keeps it at ~2 seconds. SELECT Enabled = pp.PspEnabled , StockStatusID = ss.ID ,…
Nick Spicer
  • 2,279
  • 3
  • 21
  • 26