Description:
Two parts to this, first the single result return for a unique key timestamp after a summer time change has caused it to have duplicate reported values. Second, the different behavior for the timestamp range test between the unique key field and the unindexed field in teh three column version of the test.
mysql> select version();
+-------------+
| version() |
+-------------+
| 4.1.13a-log |
+-------------+
1 row in set (0.03 sec)
mysql> create table dates (date timestamp, unique key dates$date (date));
Query OK, 0 rows affected (0.11 sec)
mysql> set time_zone='GMT';
Query OK, 0 rows affected (0.01 sec)
mysql> insert into dates values ('2003-09-26 03:00:00'),
-> ('2003-09-26 04:00:00'),
-> ('2003-09-26 05:00:00'),
-> ('2003-09-26 06:00:00'),
-> ('2003-09-26 07:00:00'),
-> ('2003-10-26 03:00:00'),
-> ('2003-10-26 04:00:00'),
-> ('2003-10-26 05:00:00'),
-> ('2003-10-26 06:00:00'),
-> ('2003-10-26 07:00:00');
Query OK, 10 rows affected (0.01 sec)
Records: 10 Duplicates: 0 Warnings: 0
mysql> select * from dates;
+---------------------+
| date |
+---------------------+
| 2003-09-26 03:00:00 |
| 2003-09-26 04:00:00 |
| 2003-09-26 05:00:00 |
| 2003-09-26 06:00:00 |
| 2003-09-26 07:00:00 |
| 2003-10-26 03:00:00 |
| 2003-10-26 04:00:00 |
| 2003-10-26 05:00:00 |
| 2003-10-26 06:00:00 |
| 2003-10-26 07:00:00 |
+---------------------+
10 rows in set (0.00 sec)
mysql> set time_zone='EST5EDT';
Query OK, 0 rows affected (0.00 sec)
mysql> select * from dates;
+---------------------+
| date |
+---------------------+
| 2003-09-25 23:00:00 |
| 2003-09-26 00:00:00 |
| 2003-09-26 01:00:00 |
| 2003-09-26 02:00:00 |
| 2003-09-26 03:00:00 |
| 2003-10-25 23:00:00 |
| 2003-10-26 00:00:00 |
| 2003-10-26 01:00:00 |
| 2003-10-26 01:00:00 |
| 2003-10-26 02:00:00 |
+---------------------+
10 rows in set (0.01 sec)
mysql> -- observe now have two 01:00:00 times
mysql> select * from dates where date='2003-10-26 01:00:00';
+---------------------+
| date |
+---------------------+
| 2003-10-26 01:00:00 |
+---------------------+
1 row in set (0.00 sec)
mysql> -- Why only one result returned? Looks as though unique key is eliminatin
g multiple returns.
mysql> select * from dates where ( date>='2003-10-26 01:00:00' ) and ( date < '2
003-10-26 01:00:01' );
+---------------------+
| date |
+---------------------+
| 2003-10-26 01:00:00 |
+---------------------+
1 row in set (0.00 sec)
mysql>
mysql> -- Same tests with both unique key field and unindexed field
mysql> set time_zone='GMT';
Query OK, 0 rows affected (0.00 sec)
mysql> create table dates2 (date timestamp, datenotkey timestamp, id int auto_in
crement, unique key dates2$date (date), key sate2$id(id) );
Query OK, 0 rows affected (0.07 sec)
mysql> insert into dates2 (date, datenotkey) select date, date from dates;
Query OK, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0
mysql> select * from dates2;
+---------------------+---------------------+----+
| date | datenotkey | id |
+---------------------+---------------------+----+
| 2003-09-26 03:00:00 | 2003-09-26 03:00:00 | 1 |
| 2003-09-26 04:00:00 | 2003-09-26 04:00:00 | 2 |
| 2003-09-26 05:00:00 | 2003-09-26 05:00:00 | 3 |
| 2003-09-26 06:00:00 | 2003-09-26 06:00:00 | 4 |
| 2003-09-26 07:00:00 | 2003-09-26 07:00:00 | 5 |
| 2003-10-26 03:00:00 | 2003-10-26 03:00:00 | 6 |
| 2003-10-26 04:00:00 | 2003-10-26 04:00:00 | 7 |
| 2003-10-26 05:00:00 | 2003-10-26 05:00:00 | 8 |
| 2003-10-26 06:00:00 | 2003-10-26 06:00:00 | 9 |
| 2003-10-26 07:00:00 | 2003-10-26 07:00:00 | 10 |
+---------------------+---------------------+----+
10 rows in set (0.00 sec)
mysql> set time_zone='EST5EDT';
Query OK, 0 rows affected (0.01 sec)
mysql> -- unique key timestamp as before
mysql> select * from dates2;
+---------------------+---------------------+----+
| date | datenotkey | id |
+---------------------+---------------------+----+
| 2003-09-25 23:00:00 | 2003-09-25 23:00:00 | 1 |
| 2003-09-26 00:00:00 | 2003-09-26 00:00:00 | 2 |
| 2003-09-26 01:00:00 | 2003-09-26 01:00:00 | 3 |
| 2003-09-26 02:00:00 | 2003-09-26 02:00:00 | 4 |
| 2003-09-26 03:00:00 | 2003-09-26 03:00:00 | 5 |
| 2003-10-25 23:00:00 | 2003-10-25 23:00:00 | 6 |
| 2003-10-26 00:00:00 | 2003-10-26 00:00:00 | 7 |
| 2003-10-26 01:00:00 | 2003-10-26 01:00:00 | 8 |
| 2003-10-26 01:00:00 | 2003-10-26 01:00:00 | 9 |
| 2003-10-26 02:00:00 | 2003-10-26 02:00:00 | 10 |
+---------------------+---------------------+----+
10 rows in set (0.00 sec)
mysql> select * from dates2 where date='2003-10-26 01:00:00';
+---------------------+---------------------+----+
| date | datenotkey | id |
+---------------------+---------------------+----+
| 2003-10-26 01:00:00 | 2003-10-26 01:00:00 | 8 |
+---------------------+---------------------+----+
1 row in set (0.01 sec)
mysql> select * from dates2 where ( date>='2003-10-26 01:00:00' ) and ( date < '
2003-10-26 01:00:01' );
+---------------------+---------------------+----+
| date | datenotkey | id |
+---------------------+---------------------+----+
| 2003-10-26 01:00:00 | 2003-10-26 01:00:00 | 8 |
+---------------------+---------------------+----+
1 row in set (0.00 sec)
mysql> -- now the non-unique key timestamp
mysql> select * from dates2;
+---------------------+---------------------+----+
| date | datenotkey | id |
+---------------------+---------------------+----+
| 2003-09-25 23:00:00 | 2003-09-25 23:00:00 | 1 |
| 2003-09-26 00:00:00 | 2003-09-26 00:00:00 | 2 |
| 2003-09-26 01:00:00 | 2003-09-26 01:00:00 | 3 |
| 2003-09-26 02:00:00 | 2003-09-26 02:00:00 | 4 |
| 2003-09-26 03:00:00 | 2003-09-26 03:00:00 | 5 |
| 2003-10-25 23:00:00 | 2003-10-25 23:00:00 | 6 |
| 2003-10-26 00:00:00 | 2003-10-26 00:00:00 | 7 |
| 2003-10-26 01:00:00 | 2003-10-26 01:00:00 | 8 |
| 2003-10-26 01:00:00 | 2003-10-26 01:00:00 | 9 |
| 2003-10-26 02:00:00 | 2003-10-26 02:00:00 | 10 |
+---------------------+---------------------+----+
10 rows in set (0.00 sec)
mysql> select * from dates2 where datenotkey='2003-10-26 01:00:00';
+---------------------+---------------------+----+
| date | datenotkey | id |
+---------------------+---------------------+----+
| 2003-10-26 01:00:00 | 2003-10-26 01:00:00 | 8 |
| 2003-10-26 01:00:00 | 2003-10-26 01:00:00 | 9 |
+---------------------+---------------------+----+
2 rows in set (0.00 sec)
mysql> select * from dates2 where ( datenotkey>='2003-10-26 01:00:00' ) and ( da
tenotkey < '2003-10-26 01:00:01' );
+---------------------+---------------------+----+
| date | datenotkey | id |
+---------------------+---------------------+----+
| 2003-10-26 01:00:00 | 2003-10-26 01:00:00 | 8 |
| 2003-10-26 01:00:00 | 2003-10-26 01:00:00 | 9 |
+---------------------+---------------------+----+
2 rows in set (0.00 sec)
mysql> -- notice that this returned two results but only one was returned for th
e unique key field, yet the range test should have found both?
How to repeat:
create table dates (date timestamp, unique key dates$date (date));
set time_zone='GMT';
insert into dates values ('2003-09-26 03:00:00'),
('2003-09-26 04:00:00'),
('2003-09-26 05:00:00'),
('2003-09-26 06:00:00'),
('2003-09-26 07:00:00'),
('2003-10-26 03:00:00'),
('2003-10-26 04:00:00'),
('2003-10-26 05:00:00'),
('2003-10-26 06:00:00'),
('2003-10-26 07:00:00');
select * from dates;
set time_zone='EST5EDT';
select * from dates;
-- observe now have two 01:00:00 times
select * from dates where date='2003-10-26 01:00:00';
-- Why only one result returned? Looks as though unique key is eliminating multiple returns.
select * from dates where ( date>='2003-10-26 01:00:00' ) and ( date < '2003-10-26 01:00:01' );
-- Same tests with both unique key field and unindexed field
set time_zone='GMT';
create table dates2 (date timestamp, datenotkey timestamp, id int auto_increment, unique key dates2$date (date), key sate2$id(id) );
insert into dates2 (date, datenotkey) select date, date from dates;
select * from dates2;
set time_zone='EST5EDT';
-- unique key timestamp as before
select * from dates2;
select * from dates2 where date='2003-10-26 01:00:00';
select * from dates2 where ( date>='2003-10-26 01:00:00' ) and ( date < '2003-10-26 01:00:01' );
-- now the non-unique key timestamp
select * from dates2;
select * from dates2 where datenotkey='2003-10-26 01:00:00';
select * from dates2 where ( datenotkey>='2003-10-26 01:00:00' ) and ( datenotkey < '2003-10-26 01:00:01' );
-- notice that this returned two results but only one was returned for the unique key field, yet the range test should have found both?
Suggested fix:
If this is to return duplicate results for a unique key column which has duplicate timestamp values in it, it looks as though the optimiser needs to handle this possibility.