Do mysql update queries benefit from an index?

前端 未结 3 2073
抹茶落季
抹茶落季 2020-12-24 03:13

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 ju

3条回答
  •  -上瘾入骨i
    2020-12-24 03:31

    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.

提交回复
热议问题