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

Optimize PostgreSQL query with join, group by

Thanks in advance to those who will help me. I have to optimize this PostgreSQL query, creating I suppose some indexes. SELECT D.market, D.symbol, D.company_name, D.first_date, D.last_date, D.days FROM (SELECT MIN(B.DIFF) AS MIN_DIFF FROM…
Unknown_11
  • 45
  • 3
2
votes
3 answers

Measuring actual Execution time of php script

i want to test actual speed of script. I have run below query. $query="select * from test"; $msc = microtime(true); for ($i = 0; $i <= 99999; $i++) $rs = mysql_query($query); $msc = microtime(true) - $msc; echo…
Nitesh Pawar
  • 435
  • 2
  • 11
2
votes
2 answers

Sql Query Pervious Row Optimisation

Here is my table structure MyTable ----------- ObjectID int (Identity), -- Primary Key FileName varchar(10), CreatedDate datetime ........... ........... ........... I need to get the time taken to create record in a file... ie... Time…
The King
  • 4,600
  • 3
  • 39
  • 58
2
votes
1 answer

Finding the "next 25 rows" in Oracle SQL based on an indexed column

I have a large table (~200M rows) that is indexed on a numeric column, Z. There is also an index on the key column, K. K Z = ========================================== 1 0.6508784068583483336644518457703156855132 2 …
Eric
  • 520
  • 1
  • 4
  • 14
2
votes
0 answers

Why does my view's clustered index update when the underlying data it references doesn't change?

Refer to this SQL Fiddle and click the "Execution Plan" link at the bottom left for context. As you can see, the CountryGroup table has a single row with CountryId = 1. I then update the CountryGroup table with a statement that should effectively be…
Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
2
votes
1 answer

How to speedup MySQL query on 3 tables ( around 60M rows together )

right now I have application where I have 4 select from 3 mysql tables where always next one is base on data I get from previous. Selects: // Search by single input from user, the VIN SELECT * FROM axnmrs_cases WHERE vin = :vin ORDER BY…
Andurit
  • 5,612
  • 14
  • 69
  • 121
2
votes
1 answer

How to explicitly load mapping tables with Entity Framework?

I have to optimize my Entity Framework queries as the generated SQL is getting more and more complex because of multiple .Include(...) statements. Instead of using Includes, I started to use explicit loading which is nice, since I have some small…
Zsolt
  • 3,263
  • 3
  • 33
  • 48
2
votes
2 answers

A join here seems unnecessary

TableA ------ id Name other_fields TableB ------ A_id (foreign key to TableA.id) other_fields Select entries from TableB which reference entries in TableA with some specific property (e.g. Name = "Alice") This can be easily done with a…
bolov
  • 72,283
  • 15
  • 145
  • 224
2
votes
3 answers

Join performance

My situation is: Table member id firstname lastname company address data ( 5 fields ) contact data ( 2 fields ) etc Table member_profile member_id html ( something like

firstname lastname

Company

date_registration
FrankBr
  • 237
  • 1
  • 9
2
votes
1 answer

Oracle:query optimization

I have two tables tab1 and tab2.tab1 has 108000 rows and tab2 has 1200000 rows. Here is sample data tab1 +-----------------------------------------------------+ | Low | high | Region_id …
arminrock
  • 525
  • 1
  • 7
  • 23
2
votes
2 answers

Merge queries with difference in conditions

I have 2 SQL (Oracle 11g) queries : select x1,x2,x3 from X where x1 = a and x2 = b; select x1,x2,x3 from X where x1 = a and x2 = b and x3 = c; They select the same columns in table X but difference in conditions. I use UNION for merge…
Hana
  • 824
  • 4
  • 14
  • 30
2
votes
1 answer

Does Oracle Query Optimizer apply top level where clauses to sub queries or views?

Oracle documentation says Oracle query engine executes sub-queries and views first and then executes top-level query. As a natural result of this, Oracle does not allow you to reference field values of top-level query in sub-queries (MSSQL allows…
e-mre
  • 3,305
  • 3
  • 30
  • 46
2
votes
3 answers

Fetching RAND() rows without ORDER BY RAND() in just one query

Using RAND() in MySQL to get a single random row out of a huge table is very slow: SELECT quote FROM quotes ORDER BY RAND() LIMIT 1 Here is an article about this issue and why this is the case. Their solution is to use two queries: SELECT COUNT(*)…
JochenJung
  • 7,183
  • 12
  • 64
  • 113
2
votes
1 answer

What is the fastest way to do a REGEXP find and Replace in MySQL database?

I have a table with 700,000 entries and I need to check each entry for 1,000,000 words and then replace the word if found from hello to #~hello~#. Words can occur multiple times in an entry and need to all be replaced. I tried this in PHP and the…
alutz
  • 192
  • 1
  • 1
  • 16
2
votes
1 answer

Guidance in reading SQL Server Explain plan

We have a SQL Server 2008 Express server that we are currently attempting to troubleshoot performance issues. The explain plan shows where the query is hitting indexes, however I would like to improve more if I could. I attached the hover box from a…
Toolman21
  • 47
  • 2
  • 7
1 2 3
99
100