Questions tagged [optimization]

In the context of a database, optimisation refers to the process of the query optimiser selecting an efficient physical execution plan.

SQL is a declarative language, so the actual physical operations selected to fulfil a query are not under the direct control of the person writing the query. The database management system will have the option of a number of semantically equivalent strategies to execute the query. Most database management systems have a query optimiser that generates the set (or a subset) of possible execution plans and selects the best one.

A database query plan consists of a tree structure of operations that process data to produce the final result. For any given query, there may be multiple query plans that are semantically equivalent (i.e. will return the same final result) but vary in their structure and performance. A query plan can also be transformed algorithmically into other semantically equivalent query plans.

For example, a predicate (filter) could be applied at various points in a query. It may be quicker to apply it early, reducing the amount of data to be processed by the rest of a complex query. The query optimiser can apply transformations to push the query predicate down toward the leaves of the plan so it is evaluated earlier, which avoids subsequent operations having to process unnecessary data.

Other optimisations possible include generating intermediate results and post-processing them (sometimes known as spool operations), altering the order that tables are joined, and selecting the algorithm used for the join. For example, a hash join is efficient for matching a larger table against a smaller one but has a startup overhead in generating the hash table. A nested loos join is efficient for a query that processes a small amount of data as it has little startup overhead. A merge join processes data sequentially, so it is good for processing multiple large data sets if they can be sorted on the key used for the join.

Most DBMS platforms use a technique called 'cost based query optimisation' that works by joining a large number of query plans and using statistics about data volumes and distributions of key values to estimate a cost metric for the candidate plans. The cheapest plan is then selected and executed. Cost based optimisation is heuristic, and for various reasons a cost based optimiser can produce suboptimal plans. This can necessitate tuning work on the database if the suboptimal plans cause performance issues.

Other optimisation strategies include 'rule based', where a set of transformation rules are applied to query plans where specific patterns are found in the query plan. PostgreSQL has an unusual optimiser based on a genetic algorithm that evolves optimal plans by mutating query plans over time and retaining successful mutations.

1987 questions
3
votes
1 answer

What is search space when it comes to query optimization?

I am quite new to learning about databases and I have come across the term "search space" in the context of query optimization. I am a little unsure of what the term means and why it is important? I have tried to search the web but haven't found any…
br.nz
  • 41
  • 1
2
votes
1 answer

How can performance of queries containing keywords like IN, EXISTS, etc. be optimized?

I was interested in some of the general ways one can use to optimize the performance of queries that use keywords like IN and EXISTS (relying on the state of the entire database). For example, something like SELECT id FROM table1 WHERE A IN…
paulinho
  • 133
  • 5
1
vote
1 answer

If a database is in memory does it still need indexing?

INFO I'm following lessons on databases. We're learning about indices now. Question If big parts of a table are in the memory, would using a index still have benefits? My side I would assume indexing makes a search always faster, so yes it would…
JochemQuery
  • 113
  • 4
1
vote
3 answers

Query optimization

In another question they told me that the speed of a DBMS largely depends on the optimizations of your queries. Does anybody know good documents about this? At school I only learned the basics. For example, a query to find a user with a password. I…
Simon Verbeke
  • 203
  • 2
  • 6
1
vote
1 answer

Message system - delete message, or flag for deletion?

I'm wondering, what is the best approach about message deletion in a mysql based message system. Plan A: When the sender sends a message, it will be saved in 2 versions. One for the sender and other for the recipient. This way everyone can…
heal
1
vote
1 answer

How can I select data with one query?

I have the following tables: Table xwrh9_betty_dictionary_ids +----+----------+-------+-------------+---------------------+-------+-------+ | id | ordering | state | checked_out | checked_out_time | lang1 | lang2…
Mokus
  • 1,017
  • 4
  • 15
  • 17
1
vote
0 answers

Link-table(s) vs Redundant Columns?

Possible Duplicate: Would it be considered as a bad practice to have multiple nullable FKs on a table in SQL Server I have two tables, Users and People, both of which share a common attribute, email address, of which they should be allowed to…
user7442
1
vote
3 answers

Improving query time for auto complete box with millions of record

I have a very basic requirement. I have a table that store city id (primary key) and city name (nvarchar value). The remaining information is stored in a separate table. In my UI, I have an auto complete box, that lists all cities when user start…
jitendragarg
  • 161
  • 1
  • 6
0
votes
1 answer

What is different between fixed-size disk pages and disk-based?

As per my knowledge, i understand fixed page size as size of page which used for read and write data but i am not able to understand what does mean of disk based ? I read these words from this paper which compared about sql and…
user35662
  • 71
  • 1
  • 7
0
votes
1 answer

How to Search a Database of Package Deals for Specific Items with the Fewest Unwanted Extra Items

Short Description I have a database of special offers that bundle several items together. I want to be able to search for a list of specific items and get a combination of bundles that includes all those items with the fewest unwanted…
Qanthelas
  • 101
0
votes
1 answer

How can I apply heuristics to optimise an expression tree?

I've converted the following SQL query: SELECT name FROM dog d, works_at w WHERE (d.owner_name = w.person_name) AND (w.kennal_name = "Breeders"); into the following expression tree: My main question is how does one apply heuristics to optimise an…
JmJ
  • 111
  • 2
0
votes
0 answers

optimize this query

Possible Duplicate: Slow query performance My database contains 20,000 rows and the following query is slow. Please help me to speed this up. SELECT r.report_id, r.user_id,u.user_name,u.user_mail, d.department_name,r.report_comment,…
0
votes
1 answer

Xamarin.Forms Working with Data

I'm looking for the best option to work with a database for a mobile app. The data that I'm looking to store is articles, posts , sounds (podcasts) and pictures. I have 0 knowledge (as to 2023) about the right way to do it. Also, is user…
0
votes
1 answer

Optimal Table Structure

We have a parent table, Journal and a child table, Stops. A Journal can have X amount of Stops. For a good 80% of our SQL queries, our customers are just interested in the first and last Stop for each Journal. We often have queries that do…
FLDelphi
  • 101
0
votes
1 answer

Comparing Query Accomplishment Speed By Using JOIN or not

I have been searching about this, and still didn't get satisfying answer. My problem is, I have 2 tables, for example table A and table B. Table A consists of: id (pk), name, address Table B consists of: b_id (pk), id(fk - from table A), tools The…
1
2