Home > MySQL, Snippets of Code > MySql add index alter table example

MySql add index alter table example

Syntax: ALTER TABLE mytable ADD INDEX idx_a_b (a, b);

This MySQL command will create a very useful combined index for this table:

CREATE TABLE IF NOT EXISTS mytable (
id INT(32) UNSIGNED NOT NULL AUTO_INCREMENT,
a INT(32) UNSIGNED NOT NULL,
b INT(32) UNSIGNED NOT NULL,
PRIMARY KEY (id)
);

Using that index, this kind of queries will run faster:

SELECT b FROM mytable WHERE a=’value’;

Because data scan is avoided, thanks to index usage.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: