简体   繁体   中英

What is the use of Mysql Index key?

Hi I am a newbie to mysql

Here are my questions:

  1. What is the use of Mysql Index key?

  2. Does it make a difference in mysql queries with defining an Index key and without it?

  3. Are all primary keys default Index key?

Thanks a million

1- Defining an index on a column (or set of columns) makes searching on that column (or set) much faster, at the expense of additional disk space.

2- Yes, the difference is that queries using that column will be much faster.

3- Yes, as it's usual to search by the primary key, it makes sense for that column to always be indexed.

Read more on MySQL indexing here .

An index is indeed an additional set of records. Nothing more.

Things that make indexes access faster are:

  • Internally there's more chance that the engine put in buffer the index than the whole table rows
  • The index is smaller so to parse it means reading less blocks of the hard drive
  • The index is sorted already, so finding a given value is easy
  • In case of being not null, it's even faster (for various reasons, but the most important thing to know is that the engine doesn't store null values in indexes )

To know whether or not an index is useful is not so easy to guess (obviously I'm not talking about the primary key) and should be investigated. Here are some counterparts when it might slow down your operations:

  • It will slow down inserts and updates on indexed fields
  • It requires more maintenance: statistics have to be built for each index so the computing could take a significantly longer time if you add many indexes
  • It might slow down the queries when the statistics are not up to date. This effect could be catastrophic because the engine would actually go "the wrong way"
  • It might slow down when the query are inadequate (anyway indexes should not be a rule but an exception: no index, except if there's an urge on certain queries. I know usually every table has at least one index, but it came after investigations) We could comment this last point a lot, but I think every case is special and many examples of this already exist in internet.

Now about your question 'Are all primary keys default Index key?', I must say that it is not like this that the optimizer works. When there are various indexes defined in a table, the more efficient index combination will be compiled with on the fly datas and some other static datas (indexes statistics), in order to reach the best performances. There's no default index per se, every situation leeds to a different result.

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM