原sql如下:
select
t1.*
from t1
inner join t2 on t1.c_str1=t2.c_str1
where t1.n_create between 20130101 and 20130430;
说明:
表t1和表t2数据量都很大,t1.c_str1是varchar字段,不是任何key,t2.c_str1是unique key,sql最初开发时执行情况还可以,过了一段时间后频繁出现超时的情况,这种情况是不能接受的,必须优化。
优化后:
select
t1.*
from t1
inner join t2 on t1.c_str1=t2.c_str1
where t1.n_create between 20130101 and 20130430
and t1.c_str1 > '';
在sql的where部分加了一句 t1.c_str1 > '', 执行耗时20多秒,还可以接受。
加这个条件的前提是t1.c_str1中绝大部分是没有值的,基本都是NULL或者'',因为t1.c_str1=t2.c_str1,所以肯定有t1.c_str1 > '',想到在where中加上试试,果然效果明显。