views:

21

answers:

2

I am sorry if this is a dumb question (cause it sounds unlikely).

I have a table that is 20 Million rows. However, only about 300K of these rows get accessed regularly, and they can be identified in a column condition called "app_user=1"

Is there anyway i can just index those rows, and when I call a select, i will be sure to pass in the condition as well?

+1  A: 

Add a column called app_user and index on that, then pass in "WHERE app_user = 1" in your query.

You could go further to partition your table based on that column.

Marcus Adams
What a cool new feature - thanks!
ming yeow
+1  A: 

I would recommend splitting the table into two separate tables. But in case you don't want to do that, the highest performance way to do this if you're always going to include "where app_user=1" in your queries is to create a primary key on the table that includes the app_user column as the first part of the key. InnoDB will use this as a clustered index which saves you a few extra disk accesses. You can create the table like this:

create table testTable ( app_user tinyint UNSIGNED default 0, id int UNSIGNED NOT NULL, name varchar(255) default '', PRIMARY KEY k1(app_user, id) ) ENGINE=InnoDB;

A friend wrote this article on clustered indexes in InnoDB a while back: http://www.joehruska.com/?p=6

Mark Maunder