Index all columns

前端 未结 6 2094
梦毁少年i
梦毁少年i 2021-02-18 14:58

Knowing that an indexed column leads to a better performance, is it worthy to indexes all columns in all tables of the database? What are the advantages/disadvantages of such ap

相关标签:
6条回答
  • 2021-02-18 15:09

    Indexes take up space. And they take up time to create, rebuild, maintain, etc. So there's not a guaranteed return on performance for indexing just any old column. You should index the columns that give the performance for the operations you'll use. Indexes help reads, so if you're mostly reading, index columns that will be searched on, sorted by, or joined to other tables relationally. Otherwise, it's more expensive than what benefit you may see.

    0 讨论(0)
  • 2021-02-18 15:14

    It is difficult to imagine real-world scenarios where indexing every column would be useful, for the reasons mentioned above. The type of scenario would require a bunch of different queries, all accessing exactly one column of the table. Each query could be accessing a different column.

    The other answers don't address the issues during the select side of the query. Obviously, maintaining indexes is an issue, but if you are creating the table/s once and then reading many, many times, the overhead of updates/inserts/deletes is not a consideration.

    An index contains the original data along with points to records/pages where the data resides. The structure of an index makes it fast to do things like: find a single value, retrieve values in order, count the number of distinct values, and find the minimum and maximum values.

    An index does not only take space up on disk. More importantly, it occupies memory. And, memory contention is often the factor that determines query performance. In general, building an index on every column will occupy more space than then original data. (One exception would be a column that is relative wide and has relatively few values.)

    In addition, to satisfy many queries you may need one or more indexes plus the original data. Your page cache gets rather filled with data, which can increase the number of cache misses, which in turn incurs more overhead.

    I wonder if your question is really a sign that you have not modelled your data structures adequately. There are few cases where you want users to build ad hoc permanent tables. More typically, their data would be stored in a pre-defined format, which you can optimize for the access requirements.

    0 讨论(0)
  • 2021-02-18 15:19

    No, you should not index all of your columns, and there's several reasons for this:

    • There is a cost to maintain each index during an insert, update or delete statement, that will cause each of those transactions to take longer.
    • It will increase the storage required since each index takes up space on disk.
    • If the column values are not disperse, the index will not be used/ignored (ex: A gender flag).
    • Composite indexes (indexes with more than one column) can greatly benefit performance for frequently run WHERE, GROUP BY, ORDER BY or JOIN clauses, and multiple single indexes cannot be combined.

    You are much better off using Explain plans and data access and adding indexes when necessary (and only when necessary, IMHO), rather than creating them all up front.

    0 讨论(0)
  • 2021-02-18 15:24

    No because you have to take in consideration that every time you add or update a record, you have to recalculate your indexes and having indexes on all columns would take a lot of time and lead to bad performance.

    So databases like data warehouses where there use only select queries is a good idea but on normal database it's a bad idea.

    Also, it's not because you are using a column in a where clause that you have to add an index on it. Try to find a column where the record will be almost all unique like a primary key and that you don't edit often. A bad idea would be to index the sex of a person cause there are only 2 possible values and the result of the index would only split the data then it will search in almost every records.

    0 讨论(0)
  • 2021-02-18 15:27

    No, there is overhead in maintaining the indexes, so indexing all columns would slow down all of your insert, update and delete operations. You should index the columns that you are frequently referencing in WHERE clauses, and you will see a benefit.

    0 讨论(0)
  • 2021-02-18 15:31
    • Every index requires additional CPU time and disk I/O overhead during inserts and deletions.
    • Indies on non-primary keys might have to be hanged on updates, although an index on the primary key might not (this is beause updates typially do not modify the primary-key attributes).
    • Each extra index requires additional storage spae.
    • For queries whih involve onditions on several searh keys, e ieny might not be bad even if only some of the keys have indies on them. Therefore, database performane is improved less by adding indies when many indies already exist.
    0 讨论(0)
提交回复
热议问题