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

SQL Server 2005: Delete Optimization

Is this the most efficient way to delete from DeletedProducts table where there are not references found in the ProductFileInfo table? Sample: DELETE FROM DeletedProducts WHERE ProductId NOT IN SELECT DISTINCT ProductID FROM ProductFileInfo Or is…
RPS
  • 1,401
  • 8
  • 19
  • 32
2
votes
2 answers

Which one is Better from in DATE_FORMATE() OR MONTH(),YEAR()

I have to get a records/count on monthly basis. There is no of ways to do --- 1 SELECT COUNT(1)AS approved FROM lu_registration WHERE MONTH(approved_date)=MONTH(NOW()) AND YEAR(approved_date)=YEAR(NOW()); 2 SELECT COUNT(1)AS approved FROM…
Zigri2612
  • 2,279
  • 21
  • 33
2
votes
1 answer

How to prevent dependant subqueries within CASE WHEN x THE (subquery)

I have a very complex query which uses some subqueries within a CASE statement. For this question the complete query isn't needed and would just prevent people from getting into the problem quick. So this post uses pseudocode to work with. If wanted…
user2429266
  • 390
  • 1
  • 3
  • 19
2
votes
0 answers

speed up WQL query "ASSOCIATOR OF ..."

I got the following code chunk which is used to get the hard disk serial number given a partition. And I need to speed it up a bit, here is the slow part of the code : //... var partition = "Disk #0, Partition #0";//previously computed, hard…
Wasabi
  • 456
  • 1
  • 4
  • 12
2
votes
1 answer

Should i split my table vertically?

I have this table of: ~200k rows (expected to grow to about a million) about 40 columns (might grow slightly) The first 20 columns are used by all rows, but the remaining 20 columns are just used by about 2% of the rows, and null for the rest.…
BobbyTables
  • 4,481
  • 1
  • 31
  • 39
2
votes
2 answers

How do I optimize this query with whereHas?

I was using this query to filter out stores with city and categories. It was working fine when I had around 1000 records in stores table. Now, when I have 5000 records it takes around 3-10 seconds to generate result. A store belongs to multiple…
Rajender Joshi
  • 4,155
  • 1
  • 23
  • 39
2
votes
3 answers

MySQL: Optimizing SELECT from 6 huge identical tables with different data split up by timestamp

please I have the same problem as I found here MySQL - Selecting data from multiple tables all with same structure but different data , I have to select data from many MySQL tables with identical structure, but different data (split up into table_0,…
Cogicero
  • 1,514
  • 2
  • 17
  • 36
2
votes
1 answer

MySQL query optimisation help

hoping you can help me on the right track to start optimising my queries. I've never thought too much about optimisation before, but I have a few queries similar to the one below and want to start concentrating on improving their efficiency. An…
ted776
  • 135
  • 2
  • 10
2
votes
1 answer

MySQL - creating a query optimizer add-on

My question quite a general one. I have to create a prototype of a query optimizer for mySQL, that works instead of standard one, or complements it. It doesn't have to be very complicated, at the beginning. But is there a way of turning off the…
Razzi
  • 21
  • 1
2
votes
2 answers

Optimizing Django get queries

I have Django code similar to this: for obj in some_list: m1obj = Model1.objects.get(a=obj.a, b=obj.b, c=obj.c) Model2(m1=m1obj, d=obj.d, e='foo').save() I did optimize the insert into Model2 using bulk_create, however, this is still…
mibm
  • 1,328
  • 2
  • 13
  • 23
2
votes
1 answer

SQL Server Execution Plans - Actual vs. Estimated

I am reading Grant Fritchey's excellent book, SQL Server Execution Plans. On pg. 21 he states, "When a query is submitted to the server, an estimated execution plan is created by the optimizer. Once that plan is created, and before it gets passed to…
Randy Minder
  • 47,200
  • 49
  • 204
  • 358
2
votes
1 answer

Optimize SQL query when GROUP BY and ORDER BY expressions are different?

From the Order By Optimization in Mysql documentation, I quote... In some cases, MySQL cannot use indexes to resolve the ORDER BY, although it still uses indexes to find the rows that match the WHERE clause. These cases include the…
vikmalhotra
  • 9,981
  • 20
  • 97
  • 137
2
votes
2 answers

How to query only the specific fields in MongoDB with Java?

I'm using MongoDB 3.2 and MongoDB Java Driver 3.2. In order to query document I use the following code: Document query = new Document("fetchStatus", new Document("$lte", fetchStatusParam)); ArrayList unfetchedEvents =…
Mike
  • 14,010
  • 29
  • 101
  • 161
2
votes
1 answer

How do indexes work behind the scenes

Im a begginer. I know indexes are necessary for performance boosts, but i want to know how they actually work behind the scenes. Beforehand, I used to think that we should make indexes on those columns which are included in where clause (which I…
Sadiq
  • 786
  • 1
  • 10
  • 35
2
votes
2 answers

Why doesn't MySql automatically optimises BETWEEN query?

I have two query for same output Slow Query: SELECT * FROM account_range WHERE is_active = 1 AND '8033576667466317' BETWEEN range_start AND range_end; Execution Time: ~800…
vaibhavatul47
  • 2,766
  • 4
  • 29
  • 42