一个特定的查询需要非常的时间来运行,大约 540 秒(是的,9 分钟!)。这会导致负载尖峰。
有没有一种方法可以重写查询 - 或者添加索引 - 来加快速度? 发生了。
select DISTINCT tab3.id
from `tab1`
left join `tab2` on tab2.element_id = tab1.resource_id
and tab1.resource_type = 2
and tab2.element_type_id = 1
left join `tab3` on tab2.tab3nt_id = tab3.id
where tab1.group_tab1_id = 1
and tab1.domain_id = 2
and tab3.domain_id = 2
and tab3.tab3nt_start_date >= '2012-12-01'
and tab3.tab3nt_start_date <= '2013-03-01'
+----+-------------+-------+--------+-------------------------------------------------------------------------------------+----------------------------+---------+-------------------------------+------+-------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+-------------------------------------------------------------------------------------+----------------------------+---------+-------------------------------+------+-------------------------------------------+
| 1 | SIMPLE | tab1 | ref | group_tab1_id_resource_id,domain_id,domain_id_resource_type,tgtr_resource_domain_key | domain_id | 13 | const,const,const | 962 | Using where; Using index; Using temporary |
| 1 | SIMPLE | tab2 | ref | FK_tab3nt,element_type_id_element_id | element_type_id_element_id | 8 | const,svr.tab1.resource_id | 14 | Using where |
| 1 | SIMPLE | tab3 | eq_ref | PRIMARY,domain_id_tab3nt_type_cd | PRIMARY | 4 | svr.tab2.tab3nt_id | 1 | Using where |
+----+-------------+-------+--------+-------------------------------------------------------------------------------------+----------------------------+---------+-------------------------------+------+--------
mysql> show create table tab1\G
*************************** 1. row ***************************
Table: tab1
Create Table: CREATE TABLE `tab1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`domain_id` int(11) NOT NULL,
`group_tab1_id` int(11) NOT NULL,
`resource_id` int(11) NOT NULL,
`resource_type` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `group_tab1_id_resource_id` (`group_tab1_id`,`resource_id`),
KEY `domain_id` (`domain_id`,`group_tab1_id`,`resource_type`,`resource_id`),
KEY `domain_id_resource_type` (`domain_id`,`resource_type`),
KEY `tgtr_resource_domain_key` (`resource_id`,`domain_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2039461 DEFAULT CHARSET=latin1
1 row in set (0.01 sec)
mysql> show create table tab2\G
*************************** 1. row ***************************
Table: tab2
Create Table: CREATE TABLE `tab2` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`element_type_id` int(11) NOT NULL,
`tab3nt_id` int(11) NOT NULL,
`element_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_tab3nt` (`tab3nt_id`),
KEY `element_type_id_element_id` (`element_type_id`,`element_id`)
) ENGINE=InnoDB AUTO_INCREMENT=53195543 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> show create table tab3\G
*************************** 1. row ***************************
Table: tab3
Create Table: CREATE TABLE `tab3` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`create_user_id` int(11) DEFAULT NULL,
`tab3nt_type_cd` int(11) NOT NULL,
`tab3nt_start_date` datetime NOT NULL,
`tab3nt_end_date` datetime NOT NULL,
`domain_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `tab3nt_user_FK` (`create_user_id`),
KEY `domain_id_tab3nt_type_cd` (`domain_id`,`tab3nt_type_cd`)
) ENGINE=InnoDB AUTO_INCREMENT=9393276 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
最佳答案
尝试在 tab2.element_id 上放置一个简单(非复合)索引。
关于mysql - 两个连接查询需要 540 秒才能运行 - 我怎样才能加快速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15216191/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我注意到像bundler这样的项目在每个specfile中执行requirespec_helper我还注意到rspec使用选项--require,它允许您在引导rspec时要求一个文件。您还可以将其添加到.rspec文件中,因此只要您运行不带参数的rspec就会添加它。使用上述方法有什么缺点可以解释为什么像bundler这样的项目选择在每个规范文件中都需要spec_helper吗? 最佳答案 我不在Bundler上工作,所以我不能直接谈论他们的做法。并非所有项目都checkin.rspec文件。原因是这个文件,通常按照当前的惯例,只
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121
Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/