神秘地创建一个临时日期表"追加"数据到临时表一遍又一遍?

时间:2014-12-17 18:18:38

标签: mysql

我需要与日期表交叉加入,所以我使用了:

CREATE TEMPORARY TABLE IF NOT EXISTS myDates AS (

SELECT 

CAST((SYSDATE()+INTERVAL (H+T+U) DAY) AS date) d

FROM ( SELECT 0 H
    UNION ALL SELECT 100 UNION ALL SELECT 200 UNION ALL SELECT 300
  ) H CROSS JOIN ( SELECT 0 T
    UNION ALL SELECT  10 UNION ALL SELECT  20 UNION ALL SELECT  30
    UNION ALL SELECT  40 UNION ALL SELECT  50 UNION ALL SELECT  60
    UNION ALL SELECT  70 UNION ALL SELECT  80 UNION ALL SELECT  90
  ) T CROSS JOIN ( SELECT 0 U
    UNION ALL SELECT   1 UNION ALL SELECT   2 UNION ALL SELECT   3
    UNION ALL SELECT   4 UNION ALL SELECT   5 UNION ALL SELECT   6
    UNION ALL SELECT   7 UNION ALL SELECT   8 UNION ALL SELECT   9
  ) U

WHERE
  (SYSDATE()+INTERVAL (H+T+U) DAY) <= (SYSDATE()+INTERVAL 1 YEAR)

);

但由于某种原因,这会将日期附加到临时表myDates。为什么会这样?

编辑#1 :我无法在其他服务器上重现此问题。在问题服务器中,我在处理大查询时运行了此代码段。这可能是为什么?

编辑#2 :在我运行此问题服务器时:

CREATE TEMPORARY TABLE IF NOT EXISTS myDates AS (

SELECT 

CAST((SYSDATE()+INTERVAL (H+T+U) DAY) AS date) d

FROM ( SELECT 0 H
    UNION ALL SELECT 100 UNION ALL SELECT 200 UNION ALL SELECT 300
  ) H CROSS JOIN ( SELECT 0 T
    UNION ALL SELECT  10 UNION ALL SELECT  20 UNION ALL SELECT  30
    UNION ALL SELECT  40 UNION ALL SELECT  50 UNION ALL SELECT  60
    UNION ALL SELECT  70 UNION ALL SELECT  80 UNION ALL SELECT  90
  ) T CROSS JOIN ( SELECT 0 U
    UNION ALL SELECT   1 UNION ALL SELECT   2 UNION ALL SELECT   3
    UNION ALL SELECT   4 UNION ALL SELECT   5 UNION ALL SELECT   6
    UNION ALL SELECT   7 UNION ALL SELECT   8 UNION ALL SELECT   9
  ) U

WHERE
  (SYSDATE()+INTERVAL (H+T+U) DAY) <= (SYSDATE()+INTERVAL 1 YEAR)

);

SELECT COUNT(*) FROM myDates;

这次运行时我得到8738,下一个9104。它一直在追加它。 Whaaat?

1 个答案:

答案 0 :(得分:0)

每次运行查询时它都会附加366。 你有没有尝试过 DROP TABLE myDates; 一旦你完成了桌子?这不是一个解决方案,但可以作为一种解决方法。

您也可以尝试修复包括所有表格的数据库。 mysqlcheck --repair - 来自命令行sql的--all-databases,看看这是否有帮助。