与 Timo Kähkönen 的回答类似,我使用 TIMESTAMPDIFF 来确定日期是否像 ISDATE 一样有效。我在两个日期参数中使用相同的日期。如果是日期,则返回零,否则返回 NULL。
我用 BENCHMARK 运行了所有三个示例,其中包含有效和无效日期。我在 ISP JustHost 的共享服务器上运行了这个,MYSQL 版本 5.6.32-78.1:
SELECT benchmark(10000000, DAYNAME('2016-06-21 18:17:47'));
-- 1 row(s) returned 3.215 sec / 0.0000072 sec
Mine: Query took 3.5333 seconds.
SELECT benchmark(10000000, STR_TO_DATE('2016-06-21 18:17:47', '%d,%m,%Y'));
-- 1 row(s) returned 7.905 sec / 0.0000081 sec
Mine: Query took 7.9635 seconds.
SELECT benchmark(10000000, TIMESTAMPDIFF(DAY,'2016-06-21 18:17:47','2016-06-21 18:17:47'));
Mine: Query took 5.1373 seconds.
...........................
With bad date (June 41st?)
SELECT benchmark(10000000, DAYNAME('2016-06-41 18:17:47'));
Mine: Query took 7.3872 seconds.
SELECT benchmark(10000000, STR_TO_DATE('2016-06-41 18:17:47', '%d,%m,%Y'));
Mine: Query took 7.9919 seconds.
SELECT benchmark(10000000, TIMESTAMPDIFF(DAY,'2016-06-41 18:17:47','2016-06-41 18:17:47'));
Mine: Query took 7.3792 seconds.
STR_TO_DATE 比其他两个稍慢。如果您主要使用有效日期,则 DAYNAME 方法似乎最快。但没有一个是真正糟糕的方式。