我是想让记录排序,这样直接select 就行了,不用order by,我的order by 太慢了。有没有办法?
create [unique] index index_test on yourtable(field1,...fieldn)
to BlueskyWide(谈趣者) 我的表比较特殊,无法在 where 条件和order by字段上建联合索引。并且表数据量大,order by 很慢。现在已经是在想特殊办法来去掉order by
原来的order by field1,field2,......,fieldn 现改为索引: create index indexname on yourtable (field1,field2,......,fieldn) 后,如果是单个表的话,就完全可以不用order by了。 一般地,order by 比索引慢。最好把语句给贴出来,让大家诊断诊断嘛!
如果有where 条件,必须在where 条件和order by上建联合索引,order by才能用到索引。单独在order by上建索引没有用处,无法使用索引.where 条件中是全文索引。无法和其他字段建立联合索引。select /* first_rows(20) */ iid from ip_info where contains(ctcontent,'中华人民共和国',1)>0 and ncatalog = 1 and ndeleteflag = 0 order by iid desc
还是有点听不懂, 为了改善查询性能,还是先建立一个索引: create index ip_info_iid on ip_info(iid); 之后继续使用你的sql吧。
to BlueskyWide(谈趣者)在IID上有primary key. 是有索引的,但跟踪发现,此sql语句实际上是把满足where条件的所有记录取出来后,再order by的,我想去掉此order by,这样速度可以提高N倍,并且可以分页显示。
to chenyanzhen(大法师) IID就是sequence产生的,相当于时间顺序,插入时,当然是IID小的先插入,大的后插入。问题是经过where 条件后,order by时,就没有用到iid的索引了,因为一般情况下,客户端(Web页面)只需要满足where条件的前面部分记录,如果是在where 条件和order by的字段上有联合索引,则取出部分记录时,数据库不会把整个where 条件的记录(可能数万条)都取出来,然后再排序,再返回部分记录。他会用优化算法,只取出部分记录,就返回了。现在目标是:数据库不用取出所有满足where 条件的记录,只需要取出部分记录就返回。任何方法都可以考虑。Thanks.
select /* first_rows(20) */ iid from ip_info where contains(ctcontent,'中华人民共和国',1)>0 and ncatalog = 1 and ndeleteflag = 0 and rownum<=10 order by iid desc
to BlueskyWide(谈趣者) 这样速度是提上去了,结果不对。他是先取出10条记录,然后在10条记录中排序。没有整体排序。
select tab.id from (select iid id from ip_info where contains(ctcontent,'中华人民共和国',1)>0 and ncatalog = 1 and ndeleteflag = 0 order by iid desc) tab where rownum<=20
物理顺序无从考证,
但逻辑顺序则是记录显示的主要方法,如对字段的索引(index)和排序(order by),
缺省时,是按物理的(rowid),但决不是随机的。
现改为索引:
create index indexname on yourtable (field1,field2,......,fieldn)
后,如果是单个表的话,就完全可以不用order by了。
一般地,order by 比索引慢。最好把语句给贴出来,让大家诊断诊断嘛!
where contains(ctcontent,'中华人民共和国',1)>0
and ncatalog = 1
and ndeleteflag = 0
order by iid desc
为了改善查询性能,还是先建立一个索引:
create index ip_info_iid on ip_info(iid);
之后继续使用你的sql吧。
where contains(ctcontent,'中华人民共和国',1)>0
and ncatalog = 1
and ndeleteflag = 0 and rownum<=10
order by iid desc
where contains(ctcontent,'中华人民共和国',1)>0
and ncatalog = 1
and ndeleteflag = 0
order by iid desc) tab
where rownum<=20
比如说,插入按“学号”顺序,而显示按“成绩”从高到低排序时,你说咋办?
当然啦!