SELECT
works
DISTINCT
optimization
Type | Comment |
ISAM | The old table format |
MyISAM | The new table format that is based on ISAM but optimized for SQL |
MERGE | A UNION view over many identical MyISAM tables |
HEAP | Tables in memory |
BDB | Berkeley DB transactional table format |
myisamchk
.
CHECK
, REPAIR
,
OPTIMIZE
and ANALYZE
on MyISAM tables.
ANALYZE
updates the cardinality for each index which helps
the optimizer to decide which index to use.
ALTER TABLE
.
GROUP BY
either sort the table according
to GROUP BY
or create a temporary table with the
GROUP BY
as a unique key.
GROUP BY
or ORDER BY
on
expressions that are not part of the first table then create a
temporary table for the result.
SELECT * FROM a WHERE k1=1 | k1=1 |
SELECT * FROM a WHERE k1>=1 AND k1 <= 10 AND OS='Linux' | 1<= k1 <= 10 |
SELECT * FROM a WHERE k1>=1 AND k1 <= 10 OR k1 > 5 and k1 <20 | 1<= k1 < 20 |
SELECT * FROM a WHERE k1 IN (1,2) AND k2 = 10 OR k1 > 10 | (k1,k2) = (1,10) OR (k1,k2) = (2,10) OR k1 > 10 |
SELECT * FROM a WHERE k1>=1 and k1 <= 10 and k2>=1 and k2<=10 | (1,1) <= (k1,k2) <= (10,10) |
ANALYZE TABLE
) to
estimate how many rows will be matched.
EXPLAIN
shows the result from the join optimizer.
system | Table has only one row |
const | At most one row to be read |
eq_ref | Read by unique key |
ref | Read by not unique key or by key part |
range | Read rows within a key range |
index | Scan the index tree |
ALL | Scan the data file |
EXPLAIN
on every query that you think is too slow!
mysql> explain select t3.DateOfAction, t1.TransactionID -> from t1 join t2 join t3 -> where t2.ID = t1.TransactionID and t3.ID = t2.GroupID -> order by t3.DateOfAction, t1.TransactionID; +-------+--------+---------------+---------+---------+------------------+------+---------------------------------+ | table | type | possible_keys | key | key_len | ref | rows | Extra | +-------+--------+---------------+---------+---------+------------------+------+---------------------------------+ | t1 | ALL | NULL | NULL | NULL | NULL | 11 | Using temporary; Using filesort | | t2 | ref | ID | ID | 4 | t1.TransactionID | 13 | | | t3 | eq_ref | PRIMARY | PRIMARY | 4 | t2.GroupID | 1 | | +-------+--------+---------------+---------+---------+------------------+------+---------------------------------+
SELECT distinct resume_main.server_id, resume_main.resume_id, date_format(resume_main.incoming,"%d.%m.%Y") as incoming, WEEKDAY(resume_main.incoming) as weekday, resume_main.title FROM resume_main, resume_region, resume_language_skills, resume_profession, resume_industry WHERE resume_main.appkind = 1 AND resume_main.language_id = 2 AND resume_region.region_id = 220 AND resume_main.resume_id = resume_region.resume_id AND resume_main.server_id = resume_region.server_id AND resume_main.experience_profession = 3 AND resume_language_skills.language_id = 2 AND resume_main.resume_id = resume_language_skills.resume_id AND resume_main.server_id = resume_language_skills.server_id AND resume_profession.profession_id in (2,59) AND resume_main.resume_id = resume_profession.resume_id AND resume_main.server_id = resume_profession.server_id AND resume_industry.industry in (30,29) AND resume_main.resume_id = resume_industry.resume_id AND resume_main.server_id = resume_industry.server_id AND resume_main.incoming >= FROM_UNIXTIME(967759200) ORDER BY resume_main.incoming LIMIT 75,25;