MYSQL Indexes for performance and scalalbility - mysql

Ive been using PHP and MySQL for a while now and I still don't understand when I should add Indexes to colums in my MySQL tables to increase performance. Are there any simple resources to help explain this to me?

In short, anything that will appear in a WHERE or ON clause should probably have an index on it. As well, you can use MySQL's EXPLAIN command to see how it actually executes the query which can point out where indexes are needed.

Related

MySQL one query executed on two identical servers uses different indexes

I have two identical databases on separate server machines, and if I execute one query on both machines, on one server it would go smoothly while on the other it would cause slow log. Explain shows me that they are not using same indexes. Any suggestion or advice, it would be helpful.
The index statistics which MySQL keeps, sometimes become inaccurate (I don't know why/when).
Running ANALYZE TABLE <table> on both servers should correct the statistics.
If the problem appears again, you can use index hints and/or IF's to force MySQL to use the correct index.

SQL - Query Performance

How to find the inefficient queries in mysql database ? I want to do performance tuning on my queries , but i coudn't find where my queries are located ? Please suggest me where can i find mysql queries for my tables .
Thanks
Prabhakaran.R
You can enable the general log and slow query logs.
Enabling general query log will log all the queries and might be heavy if you have many reads/writes. In slow query log, you can mention a threshold and only queries taking time beyond some time will be logged. Post that, you can manually analyze it or you can use tools provided( Percona has great tools)
Have you analyzed your queries with explain plans? You may be able to find a query that will return the result set you wish that isn't as heavy a load on the query engine. Remember to only select the columns you actually need (try to avoid SELECT *), well planned indexing and to use inner/outer joins in favour of a huge list of WHERE clause filters.
Good luck.
RP
In addition to what the others said, use pt-query-digest (from percona.com) to summarize the slowlog. That will tell you the worst queries.
Performance tuning often involves knowing what indexes to put on tables. My index cookbook is one place to learn how to build an INDEX for a given SELECT.

MySQL EXPLAIN - it keeps giving me different explanations each time

I have a really large, complex query I'm trying to optimise using MySQL EXPLAIN SELECT or EXPLAIN EXTENDED SELECT.
If I run it against the query, I'll see every table in the query is using Using where in the Extra column, which is great.
No data will be changed at all, I'll go off and make a cup of tea or something, come back and re-run EXPLAIN.
This time, just a few minutes later, only 20% of the tables are Using where, the primary table is now Using index; Using temporary; Using filesort, and my day becomes a nightmare trying to debug this.
I am aware that sometimes things like temporary tables and filesorts are more efficient than using where clauses and indexes. But not in the case of this database, which is 10GB in size, and creating temporary tables and filesorts kills the server completely.
Any ideas why this would be happening? Is there logic or reason behind such a thing?!
You are using InnoDB, correct? You are using a version older than 5.6.6, correct?
You have encountered an interesting variant on InnoDB's lack of "persistent statistics". Several things used to trigger re-computing the statistics for InnoDB tables. And those statistics are used for deciding how to execute the query.
Probably your particular query was "on the fence" -- a slight change in some statistic would lead to a different query plan.
If you would like, we could dig deeper. But we need to see
SHOW CREATE TABLE
SHOW TABLE STATUS (for clues of table size)
EXPLAIN EXTENDED SELECT...
EXPLAIN FORMAT=JSON SELECT... (5.6.5 or later)
And we may be able to suggest ways to speed up the query.

mysql - how to run query without index

I am looking to compare indexes and further optimize my code. What I would like to do is force the query to run without an index so I can see what difference it has made. Is it possible to do this?
Yes, you can ask the query to not use indexes by adding IGNORE INDEX (index1, index2, ...) to your query. More details can be found here.
Note: I know this is an old question and you would already have found the answer, but I hope this will help others looking for the same answer, and they wouldn't have to spend their valuable time digging through the MySQL docs.

InnoDB to MyISAM conversion strange performance behaviour

this may be a very trivial question but i dont understand it, because afaik MyISAM should be faster
i have a database containing all MyISAM tables except one - it's simple N:M joining table with ~130k records. i dont know why only this table is InnoDB, but it wasnt intentional :) It has indexes on both foreign keys pointing to its associated tables.
I tried to change the table to MyISAM, cause i tought it would boost the performance, but instead, queries involved this table was like 50x slower (i even tried to recreate the indexes, but it didnt help). Why is that?
I suspect your replacement indexes aren't getting used. Have you tried analysing the query plan with EXPLAIN? This should show you whether your indexes are being used, and how.
Just enter "EXPLAIN [yourquery];" into the MySQL console.