Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you optimize tables for specific queries?

  1. What are the patterns you use to determine the frequent queries?
  2. How do you select the optimization factors?
  3. What are the types of changes one can make?
like image 838
akiva Avatar asked Sep 26 '08 01:09

akiva


People also ask

How do you optimize a table?

The MySQL OPTIMIZE table helps you to optimize the table storage space. It reorganizes the storage data in a way that increases the Input Output efficiency and reduces the storage space. To execute this statement, you need SELECT and INSERT privileges.

How do I optimize a selected query in Oracle?

One of the primary ways in which you can optimize your SELECT query is to ensure you only include the columns you really need. Avoid using SELECT * where possible, as this will pull out a large amount of data you probably don't need to deal with. You should also avoid using SELECT DISTINCT where possible.


1 Answers

This is a nice question, if rather broad (and none the worse for that).
If I understand you, then you're asking how to attack the problem of optimisation starting from scratch.

The first question to ask is: "is there a performance problem?"
If there is no problem, then you're done. This is often the case. Nice.

On the other hand...

Determine Frequent Queries

Logging will get you your frequent queries.
If you're using some kind of data access layer, then it might be simple to add code to log all queries.
It is also a good idea to log when the query was executed and how long each query takes. This can give you an idea of where the problems are.
Also, ask the users which bits annoy them. If a slow response doesn't annoy the user, then it doesn't matter.

Select the optimization factors?

(I may be misunderstanding this part of the question) You're looking for any patterns in the queries / response times.
These will typically be queries over large tables or queries which join many tables in a single query. ... but if you log response times, you can be guided by those.

Types of changes one can make?

You're specifically asking about optimising tables.
Here are some of the things you can look for:

  • Denormalisation. This brings several tables together into one wider table, so in stead of your query joining several tables together, you can just read one table. This is a very common and powerful technique. NB. I advise keeping the original normalised tables and building the denormalised table in addition - this way, you're not throwing anything away. How you keep it up to date is another question. You might use triggers on the underlying tables, or run a refresh process periodically.
  • Normalisation. This is not often considered to be an optimisation process, but it is in 2 cases:
    • updates. Normalisation makes updates much faster because each update is the smallest it can be (you are updating the smallest - in terms of columns and rows - possible table. This is almost the very definition of normalisation.
    • Querying a denormalised table to get information which exists on a much smaller (fewer rows) table may be causing a problem. In this case, store the normalised table as well as the denormalised one (see above).
  • Horizontal partitionning. This means making tables smaller by putting some rows in another, identical table. A common use case is to have all of this month's rows in table ThisMonthSales, and all older rows in table OldSales, where both tables have an identical schema. If most queries are for recent data, this strategy can mean that 99% of all queries are only looking at 1% of the data - a huge performance win.
  • Vertical partitionning. This is Chopping fields off a table and putting them in a new table which is joinned back to the main table by the primary key. This can be useful for very wide tables (e.g. with dozens of fields), and may possibly help if tables are sparsely populated.
  • Indeces. I'm not sure if your quesion covers these, but there are plenty of other answers on SO concerning the use of indeces. A good way to find a case for an index is: find a slow query. look at the query plan and find a table scan. Index fields on that table so as to remove the table scan. I can write more on this if required - leave a comment.

You might also like my post on this.

like image 185
AJ. Avatar answered Oct 05 '22 11:10

AJ.