mysql> CREATE TABLE `t` ( `d1` int(10) unsigned NOT NULL default '0', `d2` timestamp NOT NULL default CURRENT_TIMESTAMP, `d3` datetime NOT NULL, KEY `d2` (`d2`), KEY `d1` (`d1`), KEY `d3` (`d3`) );
mysql> DELIMITER // CREATE PROCEDURE INS_T() BEGIN SET @i=1; WHILE 0<1 DO SET @i=@i+1; INSERT INTO i VALUES (1199116800+@i, FROM_UNIXTIME(1199116800+@i), FROM_UNIXTIME(1199116800+@i)); END WHILE; END;// DELIMITER ;
mysql> select count(*) from t; +----------+ | count(*) | +----------+ | 924707 | +----------+ mysql> analyze table t; +--------+---------+----------+-----------------------------+ | Table | Op | Msg_type | Msg_text | +--------+---------+----------+-----------------------------+ | test.t | analyze | status | Table is already up to date | +--------+---------+----------+-----------------------------+ mysql> show index from t; +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | t | 1 | d2 | 1 | d2 | A | 924707 | NULL | NULL | | BTREE | | | t | 1 | d1 | 1 | d1 | A | 924707 | NULL | NULL | | BTREE | | | t | 1 | d3 | 1 | d3 | A | 924707 | NULL | NULL | | BTREE | | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
mysql> explain select * from t where d1 = 1199579155; +----+-------------+-------+------+---------------+------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+-------+------+-------+ | 1 | SIMPLE | t | ref | d1 | d1 | 4 | const | 1 | | +----+-------------+-------+------+---------------+------+---------+-------+------+-------+ mysql> explain select * from t where d2 = '2008-01-06 08:25:55'; +----+-------------+-------+------+---------------+------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+-------+------+-------+ | 1 | SIMPLE | t | ref | d2 | d2 | 4 | const | 1 | | +----+-------------+-------+------+---------------+------+---------+-------+------+-------+ mysql> explain select * from t where d3 = '2008-01-06 08:25:55'; +----+-------------+-------+------+---------------+------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+-------+------+-------+ | 1 | SIMPLE | t | ref | d3 | d3 | 8 | const | 1 | | +----+-------------+-------+------+---------------+------+---------+-------+------+-------+
mysql> explain select * from t where d1 >= 1199894400; +----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+ | 1 | SIMPLE | t | range | d1 | d1 | 4 | NULL | 121961 | Using where | +----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+ mysql> explain select * from t where d2 >= from_unixtime(1199894400); +----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+ | 1 | SIMPLE | t | range | d2 | d2 | 4 | NULL | 121961 | Using where | +----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+ mysql> explain select * from t where d3 >= from_unixtime(1199894400); +----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+ | 1 | SIMPLE | t | range | d3 | d3 | 8 | NULL | 120625 | Using where | +----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+
mysql> CREATE TABLE `t` ( `d1` int(10) unsigned NOT NULL default '0', `d2` timestamp NOT NULL default CURRENT_TIMESTAMP, `d3` datetime NOT NULL, KEY `d2` (`d2`), KEY `d1` (`d1`), KEY `d3` (`d3`) );
mysql> DELIMITER // CREATE PROCEDURE INS_T() BEGIN SET @i=1; WHILE 0<1 DO SET @i=@i+1; INSERT INTO i VALUES (1199116800+@i, FROM_UNIXTIME(1199116800+@i), FROM_UNIXTIME(1199116800+@i)); END WHILE; END;// DELIMITER ;
mysql> select count(*) from t; +----------+ | count(*) | +----------+ | 924707 | +----------+ mysql> analyze table t; +--------+---------+----------+-----------------------------+ | Table | Op | Msg_type | Msg_text | +--------+---------+----------+-----------------------------+ | test.t | analyze | status | Table is already up to date | +--------+---------+----------+-----------------------------+ mysql> show index from t; +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | t | 1 | d2 | 1 | d2 | A | 924707 | NULL | NULL | | BTREE | | | t | 1 | d1 | 1 | d1 | A | 924707 | NULL | NULL | | BTREE | | | t | 1 | d3 | 1 | d3 | A | 924707 | NULL | NULL | | BTREE | | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
mysql> explain select * from t where d1 = 1199579155; +----+-------------+-------+------+---------------+------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+-------+------+-------+ | 1 | SIMPLE | t | ref | d1 | d1 | 4 | const | 1 | | +----+-------------+-------+------+---------------+------+---------+-------+------+-------+ mysql> explain select * from t where d2 = '2008-01-06 08:25:55'; +----+-------------+-------+------+---------------+------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+-------+------+-------+ | 1 | SIMPLE | t | ref | d2 | d2 | 4 | const | 1 | | +----+-------------+-------+------+---------------+------+---------+-------+------+-------+ mysql> explain select * from t where d3 = '2008-01-06 08:25:55'; +----+-------------+-------+------+---------------+------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+-------+------+-------+ | 1 | SIMPLE | t | ref | d3 | d3 | 8 | const | 1 | | +----+-------------+-------+------+---------------+------+---------+-------+------+-------+
mysql> explain select * from t where d1 >= 1199894400; +----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+ | 1 | SIMPLE | t | range | d1 | d1 | 4 | NULL | 121961 | Using where | +----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+ mysql> explain select * from t where d2 >= from_unixtime(1199894400); +----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+ | 1 | SIMPLE | t | range | d2 | d2 | 4 | NULL | 121961 | Using where | +----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+ mysql> explain select * from t where d3 >= from_unixtime(1199894400); +----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+ | 1 | SIMPLE | t | range | d3 | d3 | 8 | NULL | 120625 | Using where | +----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+
本文出自 “爱MySQL” 博客,转载请与作者联系!