31
loading...
This website collects cookies to deliver better user experience
PRIMARY KEY
s or, if they do not exist, UNIQUE
indexes.=
, >
, >=
, <
, <=
or BETWEEN
keywords, also LIKE
queries. One of the primary reasons such an index may be added to a column is to speed up search queries - when a b-tree index is in use, the storage engine does not scan through the whole table to find relevant rows.SELECT * FROM demo_table WHERE column [ = | > | >= | < | <= ] 'value';
SELECT * FROM demo_table WHERE column_a BETWEEN 100 AND 200;
SELECT * FROM demo_table WHERE column LIKE 'value%';
MBRContains
, MBRCovers
, or MBREquals
. Such functions can indicate whether the minimum bounding rectangles of one parameter contain, cover, or equals the minimum bounding rectangles of another parameter.SPATIAL
keyword like so:CREATE SPATIAL INDEX spatial_idx ON demo_table (demo_column);
orALTER TABLE demo_table ADD SPATIAL INDEX(spatial_idx);
SELECT MBRContains(@variable_1, @variable_2);
SELECT MBRWithin(@variable_2, @variable_1);
variable_1
or variable_2
in any capacity, you should define them first (the WKT value
parameter represents a well-known text-formatted value that represents geometry objects):SET @variable_1 = ST_GeomFromText('WKT value');
USING HASH
option at the end of your query:CREATE INDEX idx_name ON demo_table (demo_column) USING HASH;
SELECT column_1, column_2 FROM demo_table WHERE column_3 = 'value';
PRIMARY KEY
, the PRIMARY KEY
is the clustered index. If your table does not have a PRIMARY KEY
, the clustered index is the first UNIQUE INDEX
with all of its key columns defined as NOT NULL
.AUTO_INCREMENT
to your column and define it as the PRIMARY KEY
. To define a balanced tree index when creating a table, add an INDEX
to your column and specify the column you want to index (you can also specify the name of the index if you so desire):CREATE TABLE arctype ( clustered_index INT(255) NOT NULL
AUTO_INCREMENT PRIMARY KEY, demo_index VARCHAR(255) NOT
NULL, INDEX idx_name(demo_index));
UNIQUE INDEX
with all of its key columns defined as NOT NULL
can also be a clustered index - as the name suggests, in that case, all values in a column with a UNIQUE INDEX
will be unique (i.e. there will be no duplicates).c1
, c2
, and c3
are column names):CREATE INDEX composite_idx ON demo_table(c1,c2,c3);
c1
and c2
might be useful if you want to satisfy a query like so:SELECT * FROM demo_table WHERE c1 = 5 AND c2 = 10;
CREATE INDEX prefix_idx ON demo_table(column_name(length));
prefix_idx
is the name of the index, demo_table
is the name of the table, column_name
is the name of the column and length
is the length of the index.SELECT COUNT(DISTINCT column) / COUNT(*) FROM demo_table;
SELECT COUNT(DISTINCT LEFT(column, 5)) / COUNT(*) AS selectivity_5;