Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to figure out if mysql index fits entirely in memory

Is there a way to determine whether a mysql index fits entirely in available memory? If so, how would I:

  • Determine size of mysql indexes
  • Determine available memory for the application
  • Determine if the indexes fit entirely in memory
like image 482
David542 Avatar asked Jul 31 '12 20:07

David542


People also ask

Is MySQL index in memory?

An index does not need to be kept in memory. It acts just like a table -- it is composed of 16KB blocks that are cached into the buffer pool as needed, then bumped out when 'old' (think "least-recently-used" caching schemes).

Are indexes stored in memory?

An index is usually maintained as a B+ Tree on disk & in-memory, and any index is stored in blocks on disk. These blocks are called index blocks. The entries in the index block are always sorted on the index/search key.

How much space does an index take MySQL?

One Index size: 3 bytes for DATE. 7 bytes for DATETIME(3)

How does MySQL decide which index to use?

If there is a choice between multiple indexes, MySQL normally uses the index that finds the smallest number of rows (the most selective index). If the table has a multiple-column index, any leftmost prefix of the index can be used by the optimizer to look up rows.


1 Answers

Depends on Storage Engine

MyISAM (Caches Index Pages From .MYI files)

SELECT FLOOR(SUM(index_length)/POWER(1024,2)) IndexSizesMB FROM information_schema.tables WHERE engine='MyISAM' AND table_schema NOT IN ('information_schema','performance_schema','mysql'); 

Subtract that from key_buffer_size. If the answer > 0, then Yes

InnoDB (Caches Data and Index Pages)

SELECT FLOOR(SUM(data_length+index_length)/POWER(1024,2)) InnoDBSizeMB FROM information_schema.tables WHERE engine='InnoDB'; 

Subtract that from innodb_buffer_pool_size. If the answer > 0, then Yes

I wrote about this in the DBA StackExchange

On a dedicated DB Server, make sure InnoDBSizeMB+IndexSizesMB does not exceed 75% of RAM.

like image 88
RolandoMySQLDBA Avatar answered Oct 18 '22 03:10

RolandoMySQLDBA