这样的SQL查询如何写执行效率能高点?
表table1有大量字段,其中table1.publish是标记该记录是否发布。
表table2只有少量字段,其中table2.count是标记该记录的访问次数。
table1和table2的关联是table2.recordid=table1.id
要取出table1表中publish标记为TRUE的记录,并按他们在table2中的count字段降序排列。
由于table1的数据量比较多,所以SQL语句如何写在执行效率上会高点呢?
问题点数:20、回复次数:5Top
1 楼tomuno(特别行动组)回复于 2006-02-03 15:54:44 得分 10
select * from table1 t1,table2 t2
where table1.publish='true'
and table1.id=table2.recordid
order by table2.count desc
把*号换成具体的字段
数据库解析sql时是从后往前
所以把最小最准确的放在后面
以提高速度
Top
2 楼scjpsz1860(友情UP友情接分)(快乐升星!预祝明天更好!:)回复于 2006-02-03 16:38:30 得分 0
学习一下:)Top
3 楼ymfhcn(这痞子真帅)回复于 2006-02-03 19:35:25 得分 0
select * from table1 t1,table2 t2
where table1.publish='true'
and table1.id=table2.recordid
order by table2.count desc
这种写法算不算用联连
如果是,那是内联连,还是外联接,还是交叉联接
我想这应该是交叉联接Top
4 楼danger1(我很危险,但我也能解决问题)回复于 2006-02-04 14:14:28 得分 5
上面的SQL语句有问题,如果table2中没有与table1关联的记录,tabel1中的记录就无法显示,所以应该用作连接
select * from table1 t1,table2 t2
where table1.publish='true'
and table1.id*=table2.recordid
order by table2.count descTop
5 楼dlxu(脱离纯粹Coding阶段)回复于 2006-02-04 21:40:18 得分 5
如果是Oracle的话,外联结是这么写的吧
select *
from
table1 t1,
table2 t2
where
table1.publish='true' and
table1.id(+)=table2.recordid
order by
table2.count descTop




