select t1.a, t1.b, t2.c, t2.d from t1, t2 while (t1.a = t2.a and t2.a = 5)

kun_bu 2009-08-27 01:09:44
select t1.a, t1.b, t2.c, t2.d from t1, t2 where (t1.a = t2.a and t2.e = 123)
由于t2过于庞大,希望能对上面进行优化,
实际上就是希望t1能和一个查询出来的子集进行join

望高手指点
...全文
370 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
kun_bu 2009-08-27
  • 打赏
  • 举报
回复
select t1.a,t1.b,t2.d from t1 inner join (select a,d from t2 where a=123) t2 on t1.a=t2.a;
nianzhang747应该是这个意思

看起来好象可以了,
先结贴.
vinsonshen 2009-08-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 nianzhang747 的回复:]
select t1.a,b,t2.c,t2.d from t1  inner join  (select a,c,d,e from t2) t2 on t1.a=t2.a where t2.e=123;
[/Quote]


兄台,像你这里(select a,c,d,e from t2)有点多此一举吧?假如T2的记录很多,会慢得要命
wwwwb 2009-08-27
  • 打赏
  • 举报
回复
1、贴记录及要求结果出来看看;
2、索引情况、主键是什么;
3、EXPLAIN 你的SQL语句。
vinsonshen 2009-08-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 kun_bu 的回复:]
CREATE TABLE t1 (a int not null  primary key, b int, c int);
CREATE TABLE t2 (id int not null  auto_increment primary key, a int, d int);

select t1.a, t1.b, t1.c, t2.d from t1, t2 where (t1.a = t2.a and t2.e = 123)
如何对这个语句进行优化
[/Quote]
从你这里提供的信息看来,你这里面已经是最好的内联接了
我觉得如果你是基于这个SQL来优化的话,那建议你考虑对t1(a)建立索引,对t2(e,a)建立索引

否则,如果不是基于你的这个SQL语句,那考虑改变处理逻辑来优化了,如建立临时表来处理等等。
kun_bu 2009-08-27
  • 打赏
  • 举报
回复
nianzhang747

我试一下呵.
kun_bu 2009-08-27
  • 打赏
  • 举报
回复
这个不是复合字段的问题啊.
kun_bu 2009-08-27
  • 打赏
  • 举报
回复
CREATE TABLE t1 (a int not null primary key, b int, c int);
CREATE TABLE t2 (id int not null auto_increment primary key, a int, d int);

select t1.a, t1.b, t1.c, t2.d from t1, t2 where (t1.a = t2.a and t2.e = 123)
如何对这个语句进行优化
nianzhang747 2009-08-27
  • 打赏
  • 举报
回复
select t1.a,b,t2.c,t2.d from t1 inner join (select a,c,d,e from t2) t2 on t1.a=t2.a where t2.e=123;
vinsonshen 2009-08-27
  • 打赏
  • 举报
回复
那考虑对t2建立复合字段索引 (e,a)试下看看
kun_bu 2009-08-27
  • 打赏
  • 举报
回复
t1.a为主键,t2.a,t2.e均为索引

就是表太大,千万数量级,
从此表中查出子集为select t2.c, t2.d from t2 while t2.e = 5;
这个行数比较少,千行以内.
希望能以这个子集和t1 join 取出关联的t1.a, t1.b
vinsonshen 2009-08-27
  • 打赏
  • 举报
回复
建议贴出以下如何结果,以便于分析:

show table status like 't2' \G

show table status like 't1' \G

show index from t1;

show index from t2;

explain 你的SQL语句
vinsonshen 2009-08-27
  • 打赏
  • 举报
回复
你的表数据分布情况怎样?

可以试下:
考虑对t2建立复合字段索引 (e,a);

对t1建立索引(a)

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧