rails添加有序,索引或完整以加快查询速度

时间:2015-05-05 20:28:30

标签: ruby-on-rails ruby oracle ruby-on-rails-3

因此,我的rails代码会生成一个巨大的连接查询,其开头如下:

SELECT "CREDITRSP"."ML_TRAN_ID"       AS t0_r0,
...
...
...

我注意到,当我在sql developer中运行时,这需要大约5分钟。

尝试了以下替代方案,在3-10秒内返回结果:

 1.SELECT /*+ FULL("CREDITRSP") */ "CREDITRSP"."ML_TRAN_ID" AS t0_r0,
    ...
    ...


2. SELECT /*+ INDEX("CREDITRSP" IDX2_CREDITRSP) */ "CREDITRSP"."ML_TRAN_ID" AS t0_r0, 
...
...



3. SELECT /*+ ORDERED */ "CREDITRSP"."ML_TRAN_ID" AS t0_r0, 
...
...

上述3个选项中的任何一个都可以在rails中实现吗?

注意:我无法控制修改,更新表模式迁移。该应用程序只具有对这些表的只读访问权限。

这是现有模型代码的一部分。

class Search

  def search_by_date_range
    get_transactions(FILTER_TO_SEARCH[self.filter])
  end

  FILTER_TO_SEARCH = {
    Filter::AllTran => {
      :filter => "all_tran.between_dates(self.start_date, self.end_date)",
      :models => [
        "credit_responses"
      ]
    }
  }


  def get_transactions(search_hash={})
    if (filter = search_hash[:filter])
      trans = []
      search_hash[:models].each do |m|
        trans += eval("Terminal.#{m}.#{filter}")
      end
      [trans, nil]
    else
      [false, "Incorrect Filter"]
    end
  end


class Terminal < Tv2g::Base
  self.table_name = "terminal"


  has_many :credit_responses
end

我正在使用rails 3和&#39; activerecord-oracle_enhanced-adapter&#39;,&#39; 1.4.2&#39;宝石。

编辑:

这是我的范围查找特定模型的方式:

class CreditResponse 
  self.table_name = "creditrsp"
  has_one :transaction_code, :through => :credit_request


  default_scope includes(:credit_request, :transaction_code)

1 个答案:

答案 0 :(得分:0)

您是否尝试修改rails代码以指定select语句?

public class EchoServer {

    public static void main(String[] args) {

        int port = 8080;
        ServerSocket server = new ServerSocket(port);
        while (true) {

            //wait for next client to connect
            Socket cliSocket = server.accept();

            //hand off socket to another thread
            MyHandler handler = new MyHandler(cliSocket);
            Thread clientHandler = new Thread(handler);
            clientHandler.start();    
        }
    }
}

public class MyHandler implements Runnable {

    public MyHandler(Socket cliSocket)
    {
        //store socket
    }

    @override
    public void run()
    {
        while(true) {
            //handle client comms
        }
    }
}