`
orcl_zhang
  • 浏览: 234542 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

USE INDEX with Active Record finders(转)

阅读更多
可以通过强制指定index的方法优化find
MySQL doesn’t always pick the right index for your queries. Hence, sometimes you must tell it which index to use. Consider the example :

Activity.all(:conditions => ['created_at >= ? AND country_id = ?', 10.days.ago, 79])
Running EXPLAIN on the above query :

EXPLAIN SELECT * FROM `activities` WHERE (created_at >= '2009-07-27 12:58:44' AND country_id = 79);

Possible keys : index_activities_on_created_at,index_activities_on_created_at_and_country_id
Using the key : index_activities_on_created_at
As you can see, even though the table has index on both the fields involved in the query – index_activities_on_created_at_and_country_id, MySQL still uses index_activities_on_created_at. You can explicitly ask MySQL to use the index you want by supplying USE INDEX(http://dev.mysql.com/doc/refman/5.1/en/index-hints.html)

SELECT * FROM `activities` USE INDEX(index_activities_on_created_at_and_country_id)
  WHERE (created_at >= '2009-07-27 12:58:44' AND country_id = 79);
Active Record does not have any finder option to specify the index hint. Hence the solution is to exploit the :from option :

from = "#{quoted_table_name} USE INDEX(index_activities_on_created_at_and_country_id)"
Activity.all(:from => from,
             :conditions => ['created_at >= ? AND country_id = ?', 10.days.ago, 79])
分享到:
评论

相关推荐

    张文钿 Rails Best Practices 幻灯片

    * Keep Finders on Their Own Model * Love named_scope * the Law of Demeter * Use Observer Lesson 4. Migration * Isolating Seed Data * Always add DB index Lesson 5. Controller * Use before_...

    nokogiri_bang_finders:将 XML 查找器添加到 Nokogiri,如果未找到任何内容则引发

    nokogiri_bang_finders 这颗宝石说“Nokogiri,如果你找不到我想要的 XML,请大喊大叫。” 例如: doc = Nokogiri :: XML ( ...# with nokogiri_bang_finders doc . at! ( 'robot' ) . content # Nokogiri::XML

    finders

    发现者

    ARIA-2.7.5.exe

    200, SICK LMS-100, and Hokuyo URG 04-LX laser-range finders, control of the pan-tilt-zoom camera or pan-tilt unit, Pioneer Gripper and Arm, and more. (Some other devices are supported by separate ...

    finders_keepers:免费项目列表变得容易

    寻找者守护者背景和概述Finders Keepers是一个利用MERN的网络应用程序,该应用程序允许用户发布免费赠品,供其他人领取。 拥有帐户的用户可以创建他们想要免费赠送的物品图片的帖子。 因果用户(没有帐户的用户)将...

    Probabilistic Robotics .pdf

    英文版高清带书签 Contents Preface xvii Acknowledgments xix I Basics 1 1 Introduction 3 1.1 Uncertainty in Robotics 3 1.2 Probabilistic Robotics 4 1.3 Implications 9 1.4 Road Map 10 ...Index 639

    python3.6.5参考手册 chm

    index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python What’s New In Python 3.6 Summary – Release highlights New Features PEP 498: Formatted ...

    magento-finder:Magento Finder 扩展了 Symfony Finder 组件并查找 Magento 模块; 模型、控制器、助手等; 重写、cron 作业和事件声明等等

    magento-finder ##注意:此工具目前正在开发中 Magento Finder 扩展了 Symfony Finder 组件并查找特殊的 Magento 文件和目录,这些文件和目录可以是: 模块; 模型、控制器、助手等。 更进一步,它会在模块或...

    bangoo:Bangoo是基于Django的内容管理系统

    班古Bangoo是Django之上的内容管理系统。安装从INSTALLED_APPS及其相关的所有内容(例如: urls.py import)中删除...FINDERS设置为: STATICFILES_FINDERS = ( 'bangoo.theming.staticfiles.finders.FileSystemFind

    django-bower:在Django项目中使用Bower的简单方法

    安装安装django-bower软件包: pip install django-bower 在您的设置中将django-bower添加到INSTALLED_APPS: 'djangobower' , 将staticfinder添加到STATICFILES_FINDERS: 'djangobower.finders.BowerFinder' , ...

    Grails A Quick-Start Guide:Dave Klein (DK)

    10.1 Search Using Dynamic Finders . . . . . . . . . . . . . . 163 10.2 Hibernate Criteria Builder . . . . . . . . . . . . . . . . . 166 10.3 The Big Guns: The Searchable Plug-In . . . . . . . . . . ...

    Peter Hlavaty - Back To The CORE (syscan2015)-计算机科学

    CORE Back to thePoC• ACE• Target for ACE• ...- Historical issue- First ROP appear inMSDOS- Widely used asbypass for DEP- Using gadgets- ROP compilers /finders- Depends on preparedstack layouthttp://...

    graphite-cyanite:一个将石墨网与基于cassandra的Cyanite存储后端一起使用的插件

    api配置文件中: cyanite: urls: - http://cyanite-host:portfinders: - cyanite.CyaniteFinder与石墨网一起使用在石墨的local_settings.py : STORAGE_FINDERS = ( 'cyanite.CyaniteFinder',)CYANITE_URLS = ( '...

    python django 访问静态文件出现404或500错误

    django static文件夹下面的内容方法不了 出现404 500错误 需要查看自己的settings文件确保有一下内容 import os PROJECT_ROOT = os.path.dirname(__... 'django.contrib.staticfiles.finders.FileSystemFinder', 'd

    mers:猫鼬快速休息服务

    #Mers * _猫鼬* _Express * _Rest * _Service Mers is a plugin for express to expose mongoose finders as simple crud/rest operations. Thebasic idea being you should just define your model/finders and the...

    转录因子和组蛋白修饰的分布特征

    基于染色质免疫共沉淀的二代测序技术得到了大量转录因子结合和组蛋白修饰的ChIP-seq数据,借助于信号峰搜索(Peak finders)算法,ENCODE数据库提供了转录因子结合和组蛋白修饰信号峰(Peaks)数据.利用人类GM12878...

    计算机应用技术(实用手册)

    Memory Testing: 524288k ok with 32M shared memory 内存为512兆 32兆的二级缓存 IDE Channel 0 master : None IDE Channel 0 Slave : None IDE Channel 1 master : HL-DT-STDVD-ROM GDR8164B 0L06 IDE Channel 1 ...

    rna-tools::wrench:rna工具

    rna工具 一个工具箱,用于分析RNA的序列,结构和模拟(还有更多方法!) 在查找其他项目。 最新的 (有关详细说明,请参见 ) 20-10 / 12 :py3包装器并将其包括在... 20-03-21 是一个新工具,用于在创建的Finders

    JWorkBench-开源

    JWorkbench 是一组基于 Java 的 GUI 和非 GUI 框架,允许开发人员轻松构建所需的功能并将其合并到应用程序中。 目前可用的框架包括 Finders、Object Factories 和 Exception Management (m

Global site tag (gtag.js) - Google Analytics