简体   繁体   中英

Index part of the mysql/innodb table?

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM