indexes and statistics in MySql

Hello,

I would like to know what it is needed to have the tables of MySql up to date and how to do it to be able to have fast queries when the tables are big?
Rebuild indexes? recreate indexes? update statistics?

Thanks in advance.
lulonAsked:
Who is Participating?
 
Ioannis AnifantakisSoftware EngineerCommented:
you can auto repair - check - optimize your databases with the code bellow:

However I have given this together with more answers regarding performance and all in another question asked by an expert which you can find here

http://www.experts-exchange.com/Database/MySQL/Q_26029393.html
mysqlcheck -u root -p --auto-repair --check --optimize --all-databases

Open in new window

0
 
lulonAuthor Commented:
what about ANALYZE and this kind of command that helps the statistics to be up to date?
0
 
lulonAuthor Commented:
do you know what exactly does this do?

mysqlcheck -u root -p --auto-repair --check --optimize --all-databases
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Ioannis AnifantakisSoftware EngineerCommented:
mysqlcheck uses the SQL statements CHECK TABLE, REPAIR TABLE, ANALYZE TABLE, and OPTIMIZE TABLE.

It will flush and lock the tables before it do any operation.  Will check the validity of your table files and check/rebuild (when necessary) the indeces.
0
 
Ioannis AnifantakisSoftware EngineerCommented:
About "analyze"

ANALYZE TABLE analyzes and stores the key distribution for a table. The MySQL query optimizer is the magic inside MySQL that decides which keys, if any, to use to in the query. ANALYZE helps query optimizer to make accurate decisions by detailed analysis of the data, unlike query optimizer which makes quick analysis.

The command to analyze all tables in a running database is:
mysqlcheck -Aa -uroot -p

More on that here
http://blog.taragana.com/index.php/archive/how-to-quickly-analyze-all-tables-in-mysql-database/
0
 
lulonAuthor Commented:
When you run this...

mysqlcheck -u root -p --auto-repair --check --optimize --all-databases


Can't there be any transaction affecting the tables?
0
 
Ioannis AnifantakisSoftware EngineerCommented:
The good with mysqlcheck is that it can run durring a server working.  You don't have to bring the server down.  As I mentioned in an earlier post of mine in this question thread, "It will flush and lock the tables before it do any operation".

So being in a transaction theoretically won't cause any trouble.

However its always adviced to do maintenance during "dead times" with low trafic, or remove all running users to do maintenance.  I always do it, however its NOT necessary.
0
 
lulonAuthor Commented:
thanks ioannisa,

there is something I don't understand. If it locks the table, won't a transaction take longer if it needs that table?
0
 
Ioannis AnifantakisSoftware EngineerCommented:
As I said, whenever I perform some maintenance, I am always with zero clients connected.
However, since we talk about a lock,

yes, will have to wait for the table to become available and a transaction during the time of the repair will take longer.  However trust me, repair takes few seconds usually to complete for a typical database with few dozens of tables and few thousands of data in some of these tables.
0
 
lulonAuthor Commented:
Is it needed to run it for the mysql.* tables?
0
 
Ioannis AnifantakisSoftware EngineerCommented:
>>Is it needed to run it for the mysql.* tables?
Every once in a while you should run it.

Its like when you "Defrag" your HDD.  You can avoid doing it, but its advised to do it once in a while.
Same way your index tree might need some optimization.  It changes on every insert/edit/delete.  Doing a repair once per month will keep it always optimized and in minimal length (after optimization the index length usually falls).

Now every MyISAM and InnoDB table is followed by an index file.  

So infact if I undestand your question "Is it needed to run it for the mysql.* tables?" yes, for every table you run it, it will test this table's integrity and fix/rebuild its index file.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.