表里有大概200w不到的记录,查询语句联合这张大表和另外一张小表(1000条不到的记录)一起查询的,根据大表的A字段(数值型),查A字段值在一定范围内的记录,发现当范围越大,查出来的结果越多时,越慢,加了索引貌似还是差不多,速度没有改善呢。

解决方案 »

  1.   

    你是不是比大小用了>号,建议用>=号
    或者你是不是在A字段上用了函数
    大表放小表前面
      

  2.   

    要直接解决,可以把sql和建的索引贴出来。
    要学习,就去搜索一下oracle解释计划和类型转换,找几篇博客仔细研究一下。
      

  3.   

    看来我是真的想的太简单,请大家帮我看看SQL吧:
    select t3.* from (select t1.id,t1.deviceid,t2.device_type_id,t1.geomtype,t1.left,t1.top,t1.right,t1.bottom,t1.orientation,t1.scale,t1.text,t1.height,t1.font_orientation from t1,t2 where t1.geomtablename=t2.geom_table_name) t3,t4 where (t4.min_scale<={:scale} and t4.max_scale>={:scale} and t3.device_type_id=t4.device_type_id) and ((t3.left>={:xmin} and t3.left<={:xmax}and t3.bottom>={:ymin} and t3.bottom<={:ymax}) or (t3.right>={:xmin} and t3.right<={:xmax} and t3.top>={:ymin} and t3.top<={:ymax}) or (t3.left>={:xmin} and t3.left<={:xmax} and t3.top>={:ymin} and t3.top<={:ymax}) or (t3.right>={:xmin} and t3.right<={:xmax} and t3.bottom>={:ymin} and t3.bottom<={:ymax}) or (t3.left<={:xmin} and t3.bottom<={:ymin} and t3.right>={:xmax} and t3.top>={:ymax}) or (((t3.left>={:xmin} and t3.left<={:xmax}) or (t3.right>={:xmin} and t3.right<={:xmax})) and t3.top>={:ymax} and t3.bottom<={:ymin}) or (((t3.bottom>={:ymin} and t3.bottom<={:ymax}) or (t3.top>={:ymin} and t3.top<={:ymax})) and t3.left<={:xmin} and t3.right>={:xmax}))
      

  4.   

    t1表记录200多万条,t2、t4不到200条记录,t1表以下字段建了索引:id\left\top\right\bottom\deviceid\geomtablename,t2和t4都没有建索引,另外6楼讲到如果出现>=之类的索引就无效,那我这样的SQL是不是就是建了索引也没用啊?
      

  5.   

    RE“发现当范围越大,查出来的结果越多时,越慢,加了索引貌似还是差不多,速度没有改善呢。”
    别人应该不会有那么多的精力给你看这个sql你要查询的表,和索引其实是两个段,就类似于一个是你字典的汉字部分,索引是你字典的偏旁部首部分。直接翻字典,那是 全表扫描。按照偏旁部首查找字典,那个就是索引,具体的是,根据偏旁部首再去读字典(回表)。但是,如果如你所说,查询的数据很多,Oracle还是会选择直接去翻字典的,因为大量的数据,Oracle直接去把字典读一遍,就不用一条条的“回表”了。
      

  6.   

    那10楼的哥哥,请问我改如何优化呢,现在速度比较慢?按照你的意思,我这种查范围的SQL,建立索引是无效的吧,那有什么可以推荐的方法啊?
      

  7.   

    deviceid\geomtablename建个复合索引就可以了,
    另外left\top\right\bottom与x和y的最小最大到底什么关系,感觉语义上就可以优化,不用这么多比较吧