Decision when to create Index on table column in database?

后端 未结 3 766
臣服心动
臣服心动 2021-01-30 03:29

I am not db guy. But I need to create tables and do CRUD operations on them. I get confused should I create the index on all columns by default or not? Here is my understanding

3条回答
  •  情歌与酒
    2021-01-30 04:01

    Don't create Indexes in every column! It will slow things down on insert/delete/update operations.

    As a simple reminder, you can create an index in columns that are common in WHERE, ORDER BY and GROUP BY clauses. You may consider adding an index in colums that are used to relate other tables (through a JOIN, for example)

    Example:

    SELECT col1,col2,col3 FROM my_table WHERE col2=1
    

    Here, creating an index on col2 would help this query a lot.

    Also, consider index selectivity. Simply put, create index on values that has a "big domain", i.e. Ids, names, etc. Don't create them on Male/Female columns.

提交回复
热议问题