mysql tinyint create,MySQL:如何在boolean / tinyint列上创建部分索引?

CREATE TABLE participations (

id int(10) unsigned NOT NULL AUTO_INCREMENT,

participant_id int(10) unsigned NOT NULL,

prize_id int(10) unsigned NOT NULL,

win tinyint(1) NOT NULL DEFAULT '0',

-- ...

PRIMARY KEY (id),

INDEX participant_id (participant_id),

INDEX prize_id (prize_id)

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

I've these queries:

SELECT prize_id, participant_id FROM participations WHERE win=1;

SELECT participant_id FROM participations WHERE prize_id=1 AND win=1;

Can one create a partial index like (PostgreSQL-style) ?

CREATE UNIQUE INDEX prize_win ON participations (prize_id) WHERE win=1;

__

Plan B: Having a table "winners" (prize_id, participant_id).

解决方案

No, MySQL does not support partial indexes of this nature.

If people refer to "partial indexes" in MySQL, they're referring to text "prefix indexes" where only a certain number of leading characters are indexed rather than the whole value. For example, an index could be made by only indexing the first 5 characters.

If you're looking for a performance boost because it's not using both win and prize_id columns (though I see you don't currently have win indexed), you could create a composite index over (win, prize_id) (in that order) so that it would filter values by win first, then look at the remaining prize_id value.

If you're instead looking for a partial index simply for the purposes of decreasing size/memory usage of that index, this would unfortunately have the opposite effect.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值