Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do indexes help a mysql MEMORY table?

I was optimizing a 3 GB table as a MEMORY table in order to do some analysis on it, and I was curious if adding indexes even help a MEMORY table. Since the data is all in memory anyway, is this just redundant?

like image 546
Talik Avatar asked Feb 07 '13 17:02

Talik


2 Answers

No, they're not redundant.

Yes, continue to use indexes.

The speed of access to a memory table on smaller tables with a non-indexed column may seem almost identical to the indexed ones due to how fast full table scans can be in memory, but as the table grows or as you join them together to make larger result sets there will be a difference.

Regardless of the storage method the engine uses (disk/memory), proper indexes will improve performance as long the storage engine supports them. How the indexes are implemented may vary, but I know they are implemented in the table types MEMORY, INNODB, and MyISAM. BTW: The default method for indexes in MEMORY tables is with a hash instead of a B-Tree.

Also, I generally don't recommend coding to your storage engine. What's a memory table today may need to changed to innodb tomorrow--the SQL and schema should stand on it's own.

like image 100
Ray Avatar answered Oct 01 '22 20:10

Ray


No, indexing has little to do with data access speed. An index reorganizes data in order to optimize specific queries.

For example if you add a balanced binary tree index to a one-million-row column, you will be able to find the item you want in about 20 read operations, instead of a average half million.

So placing that million rows in memory, which is 100x faster than the disk, will speed a brute force search by 100x. Adding the index will further improve the speed by a factor of twenty-five thousand by allowing the DB to perform a smarter search instead of a merely faster search.

Things are more complicated than this, because other factors get into play, and you rarely get such large a benefit from an index. Smarter searches are also slower on a one-by-one basis: those 20 index seeks cost much more than 20 brute force seeks. Then there's index maintenance, etc.

But my suggestion is to keep the data in memory if you can -- and index them.

like image 26
LSerni Avatar answered Oct 01 '22 20:10

LSerni