美文网首页Ruby & Rails
Rails 中间强制使用 Index - 看到社区所以写一下

Rails 中间强制使用 Index - 看到社区所以写一下

作者: 我天真无邪 | 来源:发表于2017-11-09 10:43 被阅读24次
    class ApplicationRecord < ActiveRecord::Base
      self.abstract_class = true
    
      def self.use_index(index)
        from("#{self.table_name} USE INDEX(#{index})")
      end
    end
    
    
     ActiveRecord::Base.connection.tables
     ActiveRecord::Base.connection.indexes("users")
     ActiveRecord::Base.connection.indexes("users").pluck(:name)
    
    Module
    ActiveRecord::QueryMethods
     
    Methods
    from
    
    from(value, subquery_name = nil)
    Specifies table from which the records will be fetched. For example:
    
    Topic.select('title').from('posts')
    # SELECT title FROM posts
    Can accept other relation objects. For example:
    
    Topic.select('title').from(Topic.approved)
    # SELECT title FROM (SELECT * FROM topics WHERE approved = 't') subquery
    
    Topic.select('a.title').from(Topic.approved, :a)
    # SELECT a.title FROM (SELECT * FROM topics WHERE approved = 't') a
    

    GitHub - line 819

    相关文章

      网友评论

      本文标题:Rails 中间强制使用 Index - 看到社区所以写一下

      本文链接:https://www.haomeiwen.com/subject/dglpmxtx.html