索引应该建在那个表mysql,MySQL:我应该使表中的所有列都建立索引吗?

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:

9dd59406712cd532c8d01f285fe8db45.png

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);

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值