Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do mysql update queries benefit from an index?

Tags:

indexing

mysql

I have a table which I do mainly updates and I'm wondering if update queries would benefit from having an index on the where column and the updated column or an index on just where column?

like image 587
incognito2 Avatar asked Jul 21 '11 00:07

incognito2


3 Answers

Just on the where column. An index on the update column will actually slow down your query because the index has to be updated along with the data. An index on the where column will speed up updates, and selects, but slow down some insertions.

Indices also cause overhead when you delete rows. In general they are a good thing though on columns you are using WHERE on a lot, and they are basically necessary on columns you do joins on, or ORDER BY

like image 91
Paul Avatar answered Nov 03 '22 01:11

Paul


Not a straight forward answer for this one. So here goes.

UPDATE table SET ColumnA = 'something' 

if an index exists on ColumnA then you will have a slight performance hit as there will be two write operations for each row. First the data in the table and then the write for the index update.
You can even have several indexes that each have ColumnA as part of the index which mean you will have several writes in addition to the table row. You can see how having more than a few indexes can start to really slow your updates down.
But if ColumnA is not indexed at all then it will be a single write for each row only.

UPDATE table SET ColumnA = 'something' WHERE ColumnB = 'something else'

For this query if an index exists on ColumnB and not on ColumnA, it will be very fast to locate the record (called a seek) and a single write to update, and as the index doesn't care about columnA, it wont need updating.
But if you index ColumnA and not ColumnB, You will read every row in the table first (called a scan and normally a bad thing) which while a read is faster than a write it is still very slow, then it will write to the table and then another write for the index. Basically the slowest way of doing things.

DELETE table WHERE ColumnB = 'somethingelse'

Now if you have an index on any column in this table two writes, delete from table and a update/delete of the record in the index.
Again if ColumnB is not indexed, you will scan the table then delete the row(s) from the table and update indexes if any.

INSERT INTO table (ColumnA, ColumnB) VALUES ('something','something else')

If no indexes exist, a single write to the table and it's done.
Again, if indexes do exist, then an extra write for each one.

I haven't mentioned the primary key unique constraints, because you really cant get around them when you need a primary key, but every record must be checked to see if something already exists with that key before insert. Which will be a fast primary key index seek, but nevertheless, its another step in the process. The less steps the faster it will be.

Now back to yours, Basically, if you need to update a specific record, an index will help you locate that record faster than scanning the entire table. The the time saved to locate the record will be much more then the time lost updating the indexes. If you are only inserting and never reading, then indexes will slow you down. It becomes a balance thing. If you need to read specific records, then an index will help immensely. But the more indexes, the slower the writes get.

like image 22
John Petrak Avatar answered Nov 03 '22 00:11

John Petrak


Most people here don't know how indexes work in MySQL.

It depends on with storage engine you are using. InnoDB uses indexes completely different from MyISAM. This is because MySQL implements indexes on the storage engine level not the MySQL server level.

I'm afraid most people here are giving you answers based on other databases in which indexes work differently from MySQL.

InnoDB

In the case of InnoDB. This is because whenever a row is updated in InnoDB, the index has to be updated as well, as InnoDB's indexes have to be sequential, so it has to find out which page node of the index it is supposed to be in and inserted there. At times that particular page maybe full, so it has to split the page, wasting both space and increasing the time. This happens no matter which column you index because InnoDB uses clustered indexes, where the index stores the data of the entire row.

MyISAM

In the case of MyISAM, it does not have this problem. MyISAM actually uses only 1 column index, even though you can set multiple uniques on more than 1 column. Also MyISAM's index is not stored sequentially so updates are very quick. Likewise inserts are quick as well, as MyISAM just inserts it at the end of the row.

Conclusion

So in regard to your question, you should consider your schema design instead of worrying about whether the query would use the indexes. If you are updating mostly on a table, I suggest you not use InnoDB unless if you need row-level locking, high concurrency, and transactions. Otherwise MyISAM would be much better for update tasks. And no if you are using InnoDB indexes do not really help with updating, especially if the table is very large.

like image 22
bash- Avatar answered Nov 03 '22 00:11

bash-