MySQL: should I make all columns in my table indexed?

五迷三道 提交于 2019-12-23 13:06:05

问题


I have a pivot table, well of course every row will be included in a query:

mysql> select * from blog_posts as bp 
       join blog_joins as bj 
       on bj.post_id=1 
       and bj.taxonomy_id=10
       and bj.type = 1;

Here's my table structure:

Is it recommended to make an index for each column? If not, why and what would you recommend?

mysql > alter table blog_joins add index pid (post_id);
mysql > alter table blog_joins add index tid (taxonomy_id);
mysql > alter table blog_joins add index tp (type);

回答1:


For that specific query you would probably benefit from a multi-column index:

alter table blog_joins add index pid_tid_tp (post_id,taxonomy_id,type);

I'd recommend profiling your code. Try adding realistic data to your test database and try out a few different indexes. Use EXPLAIN to see which indexes MySQL actually uses for each query.

Once you have finished testing remove any unused indexes because while indexes can speed up queries, they will also slow down modifications to the table.




回答2:


You need an index for all 3 columns

create index bjind on blog_joins (post_id, taxonomy_id, `type`);


来源:https://stackoverflow.com/questions/11168518/mysql-should-i-make-all-columns-in-my-table-indexed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!