I need to speed up a query. Is an index table what I'm looking for? If so, how do I make one? Do I have to update it each insert?
Here are the table schemas:
--table1-- | --tableA-- | --table2--
id | id | id
attrib1 | t1id | attrib1
attrib2 | t2id | attrib2
| attrib1 |
And the query:
SELECT
table1.attrib1,
table1.attrib2,
tableA.attrib1
FROM
table1,
tableA
WHERE
table1.id = tableA.t1id
AND (tableA.t2id = x or ... or tableA.t2id = z)
GROUP BY
table1.id
-
You need to create a composite index on
tableA:CREATE INDEX ix_tablea_t1id_t2id ON table_A (t1id, t2id)Indexes in
MySQLare considered a part of a table: they are updated automatically, and used automatically whenever the optimizer decides it's a good move to use them.MySQLdoes not use the termindex table.This term is used by
Oracleto refer to what other databases callCLUSTERED INDEX: a kind of table where the records themselves are arranged according to the value of a column (or a set of columns).In
MySQL:When you use
MyISAMstorage, an index is created as a separate file that has.MYIextension.The contents of this file represent a
B-Tree, each leaf containing the index key and a pointer to the offset in.MYDfile which contains the data.The size of the pointer is determined by the server setting called
myisam_data_pointer_size, which can vary from2to7bytes, and defaults to6sinceMySQL 5.0.6.This allows creating
MyISAMtables up to2 ^ (8 * 6) bytes=256 TBIn
InnoDB, all tables are inherently ordered by thePRIMARY KEY, it does not support heap-organized tables.Each index, therefore, in fact is just a plain
InnoDBtable consisting of a singlePRIMARY KEYofN+Mrecords:Nrecords being an indexed value, andMrecords being aPRIMARY KEYof the main table record which holds the indexed data.
0 comments:
Post a Comment