Sunday, April 3, 2016

Tips for (My)SQL tables, attributes, indexes

First of all if you have hard time to get what is better to do - read official docs about attribute sizes, indexes and so on.
Second if you still can't make decision install wordpress or other popular cms and check how they do some stuff like indexes.

As I have installed wordpress and checked all the stuff there were I come to conclusion:
Collation:
utf8mb4_unicode_ci
index tip1: make indexes on every attribute you are going to make where statements, max or group by. And please read it on indexes official  documentation
index tip2: if you think you need uniq index and it is only one - make it primary.


Simple many to many relations
article (id, date, text); id primary. Anything else you need to use where, max, group by etc - simple index
tags (id, name, slug), id - primary, name and slug simple index (as we need them a lot we will use them from indexes data and will not need to get data from the disc
article_tags (article_id, tag_id). Primary index on 2 columns (article_id, tag_id) and simple index on tag_id.

Remember when using index on more than 1 column the order is very important. Read more in official docs. But basically if you have index on columns a, b, c you can benefit from index only when searching a, or a + b or a+b+c. If you search on b or c or b+c value it will not use index.
Learn more about this as usually at official docs http://dev.mysql.com/doc/refman/5.7/en/multiple-column-indexes.html


If you don't need -number (for example -12345 with minus sign), on number types make them unsigned

No comments:

Post a Comment