I think the issue is due to the choice of the clustered index. From MySQL docs, Clustered and Secondary Indexes:
Every InnoDB table has a special index called the clustered index where the data for the rows is stored. Typically, the clustered index is synonymous with the primary key. To get the best performance from queries, inserts, and other database operations, you must understand how InnoDB uses the clustered index to optimize the most common lookup and DML operations for each table.
Also check the answer by @marc_s in this SO question: How to choose the clustered index in SQL Server?, where he mentions:
According to The Queen Of Indexing - Kimberly Tripp - what she looks for in a clustered index is primarily:
And if you can also guarantee:
then you're pretty close to having your ideal clustering key!
Now, your clustered index is the (Primary Key):
hash varchar(5) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,
which (lets go through the check-list) is:
- Unique (yes, OK)
- Narrow (yes, OK)
- Static (perhaps, you know that)
but is probably not:
- Ever-increasing pattern (No, it probably isn't)
So, what happens when you use a non-ever-increasing clustered index?
I can't answer better than Kimberly L. Trip: Ever-increasing clustering key - the Clustered Index Debate..........again!
If the clustering key is ever-increasing then new rows have a specific location where they can be placed. If that location is at the end of the table then the new row needs space allocated to it but it doesn't have to make space in the middle of the table. If a row is inserted to a location that doesn't have any room then room needs to be made (e.g. you insert based on last name then as rows come in space will need to be made where that name should be placed). If room needs to be made, it's made by SQL Server doing something called a split. Splits in SQL Server are 50/50 splits - simply put - 50% of the data stays and 50% of the data is moved. This keeps the index logically intact (the lowest level of an index - called the leaf level - is a douly-linked list) but not physically intact. When an index has a lot of splits then the index is said to be fragmented. Good examples of an index that is ever-increasing are IDENTITY columns (and they're also naturally unique, natural static and naturally narrow) or something that follows as many of these things as possible - like a datetime column (or since that's NOT very likely to be unique by itself datetime, identity).
Note that despite the mention of SQL-Server, the same concept applies to InnoDB clustered indexes as well. I suppose that the clustered index has 2 issues:
When you are inserting a new row (the "random" hash guarantees that) it gets inserted in a random location of the index. This means that it sometimes will find no space there available to be inserted (note that InnoDB always leaves some space free in the index but when that free-available space is filled) there has to be some rearrangement of the index - and that takes time.
What the rearrangement is also causing over time is fragmentation of the index. Which will eventually make other queries and statements slower.
I suspect your slow UPDATES occur due to your high innodb_max_dirty_pages_pct
. This is a very good article on how InnoDB handles checkpoints and dirty page flushing, but the gist of my recommendation is to lower innodb_max_dirty_pages_pct
to 60 or 70 and see if that helps.
Unfortunately, I suspect you are running native InnoDB in 5.1 and not the InnoDB plugin. This will limit your ability to tune your checkpoints.
Best Answer
SHOW GLOBAL STATUS
before and after a test run. Then look at the differences inCom_...
,Questions
, andQueries
.Things like
Com_insert
will tell you specifically how manyINSERTs
there were.If you have Stored routines,
Questions
andQueries
will not be the same, the difference being due to one counting statements in the routines; the other not.IOPs may have very little relation to qps. One
SELECT
could cause millions of IOPs if it is scanning a huge table that is not cached in RAM. OTOH, a thousand simpleSELECTs
where all the necessary data is cached might cause zero IOPs.QPS is not as important as the sum of the times for all the queries. In the previous paragraph, that one big query arguably might have more impact on the system than the thousand small ones.
To find which statements are being naughty, use the slowlog. It will catch DDLs and DMLs. See http://mysql.rjweb.org/doc.php/mysql_analysis#slow_queries_and_slowlog