我有3张表,其中2张表有20万份记录,另一张表有180万份记录。我确实使用两个约束(即OCN and TIMESTAMP(month,year) )合并了这3个表。前两个表的月份和年份列为Monthx (包括月份、日期和年份)。和其他表作为每个月和年份的单独列。我提出了这个问题,
mysql--> insert into trail
select * from A,B,C
where A.OCN=B.OCN
and B.OCN=C.OCN
and C.OCN=A.OCN
and date_format(A.Monthx,'%b')=date_format(B.Monthx,'%b')
and date_format(A.Monthx,'%b')=C.IMonth
and date_format(B.Monthx,'%b')=C.month
and year(A.Monthx)=year(B.Monthx)
and year(B.Monthx)=C.Iyear
and year(A.Monthx)=C.Iyear我给了这个查询4天后,它的静止'%b' u告诉我这个查询是正确的还是错误的,并提供了一个准确的查询..(我给了tat ,因为我的C表有一个列,列的格式为一月,三月)。
发布于 2011-06-21 22:06:55
请不要使用隐含的地方加入,掩埋它在1989年,它属于哪里。使用显式联接代替
select * from a inner join b on (a.ocn = b.ocn and
date_format(A.Monthx,'%b')=date_format(B.Monthx,'%b') ....查询的这个选择部分(必须重写它,因为我拒绝处理'89语法‘)
select * from A
inner join B on (
A.OCN=B.OCN
and date_format(A.Monthx,'%b')=date_format(B.Monthx,'%b')
and year(A.Monthx)=year(B.Monthx)
)
inner join C on (
C.OCN=A.OCN
and date_format(A.Monthx,'%b')=C.IMonth
and date_format(B.Monthx,'%b')=C.month
and year(B.Monthx)=C.Iyear
and year(A.Monthx)=C.Iyear
)有一个的问题。
在字段上使用函数的
,
(A = B)和(B = C),那么从逻辑上讲,(A = C) 我建议您重写表以使用不需要转换(使用函数)但可以直接比较的字段。
像yearmonth : char(6)这样的字段,例如201006,可以更快地进行索引和比较。
如果表A、B、C中有一个名为ym的字段,它比查询更短:
INSERT INTO TRAIL
SELECT a.*, b.*, c.* FROM a
INNER JOIN b ON (
a.ocn = b.ocn
AND a.ym = b.ym
)
INNER JOIN c ON (
a.ocn = c.ocn
AND a.ym = c.ym
);如果将索引放在ocn (可能是主索引)和ym上,那么查询应该每秒运行大约一百万行(或更多)。
发布于 2011-06-21 08:55:03
若要测试查询是否正常,请将一小部分记录从A、B和C导入到临时数据库中,并测试它们的记录子集。
您的隐式联接中存在冗余,因为您将加入A.OCN和B.OCN,B.OCN与C.OCN,然后C.OCN到A.OCN,这些都可以删除。如果A.OCN = B.OCN和B.CON = C.OCN,则A.OCN = C.OCN是隐含的。此外,我猜你的日期比较中也有冗余。
https://stackoverflow.com/questions/6421307
复制相似问题