mysql索引 两个大于,为什么MySQL不使用索引进行大于比较?

I am trying to optimize a bigger query and ran into this wall when I realized this part of the query was doing a full table scan, which in my mind does not make sense considering the field in question is a primary key. I would assume that the MySQL Optimizer would use the index.

Here is the table:

CREATE TABLE userapplication (

application_id int(11) NOT NULL auto_increment,

userid int(11) NOT NULL default '0',

accountid int(11) NOT NULL default '0',

resume_id int(11) NOT NULL default '0',

coverletter_id int(11) NOT NULL default '0',

user_email varchar(100) NOT NULL default '',

account_name varchar(200) NOT NULL default '',

resume_name varchar(255) NOT NULL default '',

resume_modified datetime NOT NULL default '0000-00-00 00:00:00',

cover_name varchar(255) NOT NULL default '',

cover_modified datetime NOT NULL default '0000-00-00 00:00:00',

application_status tinyint(4) NOT NULL default '0',

application_created datetime NOT NULL default '0000-00-00 00:00:00',

application_modified timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,

publishid int(11) NOT NULL default '0',

application_visible int(11) default '1',

PRIMARY KEY (application_id),

KEY publishid (publishid),

KEY application_status (application_status),

KEY userid (userid),

KEY accountid (accountid),

KEY application_created (application_created),

KEY resume_id (resume_id),

KEY coverletter_id (coverletter_id),

) ENGINE=MyISAM ;

This simple query seems to do a full table scan:

SELECT * FROM userapplication WHERE application_id > 1025;

This is the output of the EXPLAIN:

+----+-------------+-------------------+------+---------------+------+---------+------+--------+-------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+-------------------+------+---------------+------+---------+------+--------+-------------+

| 1 | SIMPLE | userapplication | ALL | PRIMARY | NULL | NULL | NULL | 784422 | Using where |

+----+-------------+-------------------+------+---------------+------+---------+------+--------+-------------+`

Any ideas how to prevent this simple query from doing a full table scan? Or am I out of luck?

解决方案

MyISAM tables are not clustered, a PRIMARY KEY index is a secondary index and requires an additional table lookup to get the other values.

It is several times more expensive to traverse the index and do the lookups. If you condition is not very selective (yields a large share of total records), MySQL will consider table scan cheaper.

To prevent it from doing a table scan, you could add a hint:

SELECT *

FROM userapplication FORCE INDEX (PRIMARY)

WHERE application_id > 1025

, though it would not necessarily be more efficient.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值