如果您想了解使用find的RailsSQL查询的相关知识,那么本文是一篇不可错过的文章,我们将对sql中find函数用法进行全面详尽的解释,并且为您提供关于GrailsSQL查询、RailsMySQL
如果您想了解使用find的Rails SQL查询的相关知识,那么本文是一篇不可错过的文章,我们将对sql中find函数用法进行全面详尽的解释,并且为您提供关于Grails SQL查询、Rails MySQL查询时间混乱、Rails select子查询(如果可能,不使用finder_sql)、Rails SQL查询与find的有价值的信息。
本文目录一览:- 使用find的Rails SQL查询(sql中find函数用法)
- Grails SQL查询
- Rails MySQL查询时间混乱
- Rails select子查询(如果可能,不使用finder_sql)
- Rails SQL查询与find
使用find的Rails SQL查询(sql中find函数用法)
我希望使用find在rails控制器中编写此SQL查询:
select id,name from questionswhere id not in (select question_id from levels_questions where level_id=15)
我该怎么做?我正在使用Rails框架和MySQL。提前致谢。
答案1
小编典典简单方法:
ids = LevelsQuestion.all(:select => "question_id", :conditions => "level_id = 15").collect(&:question_id)Question.all(:select => "id, name", :conditions => ["id not in (?)", ids])
一枪:
Question.all(:select => "id, name",:conditions => ["id not in (select question_id from levels_questions where level_id=15)"])
Grails SQL查询
想象一下,我有这样的事情:
def example = {
def temp = ConferenceUser.findAllByUser(User.get(session.user))
[temp: temp]
}
解释我的问题:尽管动态查找器非常易于使用和学习,但是我必须替换网站的动态查找器以进行sql查询,因为这是必需的。由于我不太了解SQL,所以我的主要问题是:
a)我使用的是SQLS数据库,其驱动程序和数据源配置正确,我的网站现在可以正常工作。如果我想将“
findAllByUser”替换为sql语句,我应该做这样的事情:
def dataSource
...
def db = new Sql(dataSource)
def temp = db.rows("SELECT ... ")
b)那行得通吗?我的意思是,如果使用“ findAllByUser”,则临时对象将是一个列表,是否需要打开与数据库的连接=?
Rails MySQL查询时间混乱
我有一个计数计算查询,我在我的Rails应用程序中运行了数千次,对于db中的每个客户.
当我在MySQL客户端中运行查询并禁用查询缓存时,查询所需的时间不超过1毫秒.
但是,当我从启用了查询输出的Rails控制台运行我的任务时,我注意到在前几次查询非常快的时候,其余查询的时间突然从不到1毫秒突然增加到大约180毫秒.
我已经减少了innodb_buffer_pool_size,以便查看行为的变化,但没有注意到任何事情.
这是控制台的输出:
EmailCampaignReport::Open Columns (143.2ms) SHOW FIELDS FROM `email_campaign_report_opens`
sql (0.3ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332330)
sql (0.2ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 333333)
sql (0.2ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332661)
sql (0.1ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332326)
sql (0.1ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332665)
sql (0.2ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 336027)
sql (0.2ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 333001)
sql (0.2ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 331983)
sql (0.1ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332668)
sql (0.1ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332316)
sql (0.1ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332325)
sql (0.1ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 331995)
sql (0.2ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 334007)
sql (0.2ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 333326)
sql (0.1ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332998)
sql (183.9ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 334673)
sql (183.7ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 336751)
sql (183.6ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 333334)
sql (186.3ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332663)
sql (183.7ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332328)
sql (186.3ms) SELECT count(*) AS count_all FROM `email_campaign_report_opens` WHERE (customer_id = 332659)
该表中的customer_id列有一个索引.
有没有人有任何关于为什么会发生这种情况的建议?
谢谢
SELECT customer_id,count(*) AS count_all FROM `email_campaign_report_opens` GROUP BY customer_id;
如果您有这么多记录,您担心将它们全部归还,那么请分批进行,但我只是不明白为什么您真的想要为每个客户运行此查询.
Rails select子查询(如果可能,不使用finder_sql)
我有一个称为“对象”的模型(这并不重要)
它具有默认价格(列称为“价格”)。
然后是一个Schedule对象,该对象可以覆盖特定日期的价格。
我希望能够在SQL查询期间确定MINIMUM价格(根据定义,它是默认价格和“当前”价格之间的MINIMUM),以便能够按计算出的最低价格进行订购
我想让我的搜索查询尽可能高效,我想知道是否可以做这样的事情:
Object.select("id AS p_id, id, (SELECT MIN(`schedules`.`price`) FROM `schedules` WHERE `schedules`.`object_id` = p_id`) AS objects.min_price").limit(5)
但是,它会生成一个奇怪的SQL,如下所示:
SELECT `objects`.`id` AS t0_r0, `objects`.`title` AS t0_r1, `objects`.`created_at` AS t0_r2, `objects`.`updated_at` AS t0_r3, `objects`.`preferences` AS t0_r4 ........ (a lot of columns here) ... ` WHERE `objects`.`id` IN (1, 2, 3, 4 ....)
因此,如您所见,它不起作用。首先-它加载了对象表中的所有列,其次-它看起来很可怕。
我不想使用finder_sql的原因是我有很多可选参数和东西,因此在获取结果本身之前,强烈建议使用AR :: Relation对象。
除了上面提到的以外,我在数据库中还有很多记录,我认为将它们全部加载到内存中并不是一个好主意,这就是我想要执行此子查询的主要原因-
只是为了过滤掉尽可能多的记录。
有人可以帮助我如何更有效地做到这一点吗?
答案1
小编典典如果分别生成子查询并使用联接而不是相关的子查询,则可以使此操作更容易:
subquery = Schedule.select(''MIN(price) as min_price, object_id'') .group(:object_id) .to_sqlObject.joins("JOIN (#{subquery}) schedules ON objects.p_id = schedules.object_id") .select(''objects.*, schedules.min_price'') .limit(5)
Rails SQL查询与find
select id,name from questions where id not in (select question_id from levels_questions where level_id=15)
我该怎么做?我正在使用Rails框架和MysqL.
提前致谢.
解决方法
ids = LevelsQuestion.all(:select => "question_id",:conditions => "level_id = 15").collect(&:question_id) Question.all(:select => "id,name",:conditions => ["id not in (?)",ids])
一枪:
Question.all(:select => "id,:conditions => ["id not in (select question_id from levels_questions where level_id=15)"])
我们今天的关于使用find的Rails SQL查询和sql中find函数用法的分享就到这里,谢谢您的阅读,如果想了解更多关于Grails SQL查询、Rails MySQL查询时间混乱、Rails select子查询(如果可能,不使用finder_sql)、Rails SQL查询与find的相关信息,可以在本站进行搜索。
本文标签: