MySQL indexes - how many are enough?

前端 未结 6 1909
悲&欢浪女
悲&欢浪女 2020-12-09 17:56

I\'m trying to fine-tune my MySQL server so I check my settings, analyzing slow-query log, and simplify my queries if possible.

Sometimes it is enough if I am indexi

6条回答
  •  囚心锁ツ
    2020-12-09 18:25

    How many indexes depends entirely on the queries your running, what kinds of joins are being done (if any), the kind of data stored in the table and how big the tables are (as well as many other factors). There's really no exact science to it. The greatest tool in your arsenal for figuring out how to optimize a query is explain. Using explain you can find out what kind of joins are being down, what possible keys could be used and which key (if any) was used as well as how many rows were examined for each table in the join.

    Using this information you can decide how to key your tables and/or modify your queries to make them more efficient. The syntax for explain is very simple.

    EXPLAIN SELECT * FROM `categories` ORDER BY `orderid` ASC;
    

    Note, explain does not actually run the query. So if you're using this to debug a query that takes 5 minutes to run, explain will still be very fast.

    You do need to be careful when adding indexes though as they do cause inserts and updates to go slower and on very large tables this performance hit can become noticeable. Especially if that same table is used for a lot of reads. While adding a lot of indexes generally won't kill the performance of a query, you should still only add them as yo

提交回复
热议问题