MySQL - why not index every field?

前端 未结 6 1961
后悔当初
后悔当初 2020-12-02 07:12

Recently I\'ve learned the wonder of indexes, and performance has improved dramatically. However, with all I\'ve learned, I can\'t seem to find the answer to this question.<

相关标签:
6条回答
  • 2020-12-02 07:50

    this answer is my personal opinion based I m using my mathematical logic to answer

    the second question was about the border where to stop, First let do some mathematical calculation, suppose we have N rows with L fields in a table if we index all the fields we will get a L new index tables where every table will sort in a meaningfull way the data of the index field, in first glance if your table is a W weight it will become W*2 (1 tera will become 2 tera) if you have 100 big table (I already worked in project where the table number was arround 1800 table ) you will waste 100 times this space (100 tera), this is way far from wise.

    If we will apply indexes in all tables we will have to think about index updates were one update trigger all indexes update this is a select all unordered equivalent in time

    from this I conclude that you have in this scenario that if you will loose this time is preferable to lose it in a select nor an update because if you will select a field that is not indexed you will not trigger another select on all fields that are not indexed

    what to index ?

    foreign-keys : is a must based on

    primary-key : I m not yet sure about it may be if someone read this could help on this case

    other fields : the first natural answer is the half of the remaining filds why : if you should index more you r not far from the best answer if you should index less you are not also far because we know that no index is bad and all indexed is also bad.

    from this 3 points I can conclude that if we have L fields composed of K keys the limit should be somewhere near ((L-K)/2)+K more or less by L/10

    this answer is based on my logic and personal prictices

    0 讨论(0)
  • 2020-12-02 07:52

    You have to balance CRUD needs. Writing to tables becomes slow. As for where to draw the line, that depends on how the data is being acessed (sorting filtering, etc.).

    0 讨论(0)
  • 2020-12-02 07:53

    Indexing will take up more allocated space both from drive and ram, but also improving the performance a lot. Unfortunately when it reaches memory limit, the system will surrender the drive space and risk the performance. Practically, you shouldn't index any field that you might think doesn't involve in any kind of data traversing algorithm, neither inserting nor searching (WHERE clause). But you should if otherwise. By default you have to index all fields. The fields which you should consider unindexing is if the queries are used only by moderator, unless if they need for speed too

    0 讨论(0)
  • 2020-12-02 07:55

    Indexes take up space in memory (RAM); Too many or too large of indexes and the DB is going to have to be swapping them to and from the disk. They also increase insert and delete time (each index must be updated for every piece of data inserted/deleted/updated).

    You don't have infinite memory. Making it so all indexes fit in RAM = good.

    You don't have infinite time. Indexing only the columns you need indexed minimizes the insert/delete/update performance hit.

    0 讨论(0)
  • 2020-12-02 08:12

    Keep in mind that every index must be updated any time a row is updated, inserted, or deleted. So the more indexes you have, the slower performance you'll have for write operations.

    Also, every index takes up further disk space and memory space (when called), so it could potentially slow read operations as well (for large tables). Check this out

    0 讨论(0)
  • 2020-12-02 08:14

    It is not a good idea to indexes all the columns in a table. While this will make the table very fast to read from, it also becomes much slower to write to. Writing to a table that has every column indexed would involve putting the new record in that table and then putting each column's information in the its own index table.

    0 讨论(0)
提交回复
热议问题