Derby数据库的一些应用操作

Derby数据库下有三张表:

CS_ESI_EMAIL_CONFIGWHERE(用户注册表)1834条记录

CS_ESI_EMAIL_LOG(邮件id和用户名)61301条记录,包括时间段2012-02-01到2012-05-14

CS_ESI_EMAIL_LOG_BAK120305(邮件id和用户名)78735条记录,包括时间段2011-10-01到2012-03-05

要求找出2012-01-01到2012-07-01期间没有邮件的用户


我的解决方法如下:

sql1:

CREATE VIEW TESTV ASSELECT EMAIL_CONFIG_UUID,MAILBOX_NAME,C

S_USER_ID,STATUS FROM CS_ESI_EMAIL_CONFIGWHERE EMAIL_CONFIG_UUID NOT IN(SELECT

EMAIL_CONFIG_UUID FROM CS_ESI_EMAIL_LOG WHERE RECEIVED_DT >'2012-02-01' AND REC

EIVED_DT <= '2012-07-01' UNION SELECTEMAIL_CONFIG_UUID FROM CS_ESI_EMAIL_LOG_BA

K120305 WHERE RECEIVED_DT >='2012-01-01' AND RECEIVED_DT <= '2012-02-01');

 sql2:

SELECTEMAIL_CONFIG_UUID,MAILBOX_NAME FROM(SELECT ROW_NUMBER() OVER() AS row

num, TESTV.* FROM TESTV) AS TMP WHEREROWNUM <= 50 AND ROWNUM >= 0;

由于上面的查询语句查了一个多钟头都查不出来,后来我把上面的蓝色部分抽取出来,放到一个表里面,然后在建立视图view的时候用表代替那段查询语句,变成了下面的代码,结果几秒钟就查出来了(查出801条数据),原因应该是如果直接嵌入的话需要拆开做很多的and和判断操作

sql1:(临时表用于保存查询的数据)

CREATE TABLE TESTTABLE(EMAIL_CONFIG_UUID INTEGER PRIMARY KEY);

sql2:(把查询结果先存到表里面)(insert into table select语句和union语句)

INSERT INTO TESTTABLE(EMAIL_CONFIG_UUID)SELECT EMAIL_CONFIG_UUIDFROM CS_ESI_EMAIL_LOG 

WHERE RECEIVED_DT > '2012-02-01' AND RECEIVED_DT <= '2012-07-01'UNION 

SELECT EMAIL_CONFIG_UUID FROM CS_ESI_EMAIL_LOG_BAK120305 WHERE RECEIVED_DT >=
 '2012-01-01' AND RECEIVED_DT <= '2012-02-01';

sql3:(创建视图保存结果,用于后面查询)

CREATE VIEW TESTV AS SELECT EMAIL_CONFIG_UUID,MAILBOX_NAME,CS_USER_ID,STATUS
 FROM CS_ESI_EMAIL_CONFIG WHERE EMAIL_CONFIG_UUID NOT IN(SELECT EMAIL_CONFIG_UUI
D FROM TESTTABLE) ORDER BY EMAIL_CONFIG_UUID ASC;

sql4:(显示查询结果的一部分)(显示部分结果语句)

SELECT EMAIL_CONFIG_UUID,MAILBOX_NAME FROM(SELECT ROW_NUMBER() OVER() ASROWNUM 

TESTV.* FROM TESTV) AS TMP WHERE ROWNUM <= 100 AND ROWNUM >= 0;


遗留问题:

1、Derby中是否不能用create table as select语句创建表,老是报Encountered <EOF> 异常

2、Derby中是否没有全外联结full outer join

3、我有些查询字段有创建了索引,有帮助的吗?



你可能感兴趣的:(Derby数据库的一些应用操作)