我在Docker中启动了一个MySQL服务器,就像这样,只是使用默认值:
docker run -d --rm --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql
并创建了这样的数据库:
docker exec -it mysql mysql -e 'create database if not exists test'
然后像这样连接交互式会话:
docker exec -it mysql mysql test
然后我在上面填了3200万个随机日期…
INSERT into dates select date(from_unixtime(rand()*unix_timestamp(now())) );
然后运行几十次:
INSERT into dates select date(from_unixtime(rand()*unix_timestamp(now())) ) from dates;
现在我的约会次数几乎是你的两倍:
mysql> explain select * from dates;
+----+-------------+-------+------------+------+---------------+------+---------+------+----------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+----------+----------+-------+
| 1 | SIMPLE | dates | NULL | ALL | NULL | NULL | NULL | NULL | 33497947 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+------+---------+------+----------+----------+-------+
1 row in set, 1 warning (0.00 sec)
最后,我可以演示如何快速搜索表格:
mysql> select count(*), d from dates where d between '2001-01-01' and '2001-12-31' group by d order by d desc;
....
365 rows in set (4 min 31.17 sec)
有道理,2001年每天都有几千个结果。(请记住,这些日期是在1970年到现在之间随机分布的)。
没有索引或任何东西,也没有sql优化。花了4.5分钟。希望这能为您的服务器和查询性能提供一个基线。