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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ioannis AnifantakisProgramming InstructorCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Ioannis AnifantakisProgramming InstructorCommented:
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 AnifantakisProgramming InstructorCommented:
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 AnifantakisProgramming InstructorCommented:
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 AnifantakisProgramming InstructorCommented:
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 AnifantakisProgramming InstructorCommented:
>>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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.