Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does MySQL not use an index for a greater than comparison?
    text
    copied!<p>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 <em>assume</em> that the MySQL Optimizer would use the index.</p> <p>Here is the table:</p> <pre><code> 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 ; </code></pre> <p>This simple query seems to do a full table scan:</p> <pre><code>SELECT * FROM userapplication WHERE application_id > 1025;</code></pre> <p>This is the output of the EXPLAIN:</p> <pre> +----+-------------+-------------------+------+---------------+------+---------+------+--------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------------------+------+---------------+------+---------+------+--------+-------------+ | 1 | SIMPLE | userapplication | ALL | PRIMARY | NULL | NULL | NULL | 784422 | Using where | +----+-------------+-------------------+------+---------------+------+---------+------+--------+-------------+` </pre> <p>Any ideas how to prevent this simple query from doing a full table scan? Or am I out of luck?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload