我的Mysql很弱
我必须根据我编写的以下脚本的某些条件更新答案表的问题 ID,但由于数据量巨大,执行时间超过 700 秒,
谁能帮我解决这个问题
function UpdateAnswer()
{
ini_set('max_execution_time', 800);
$users= DB::select(DB::raw("select * from users where role!='admin_dd' and role!='sales_dd' "));
foreach($users as $user)
{
$answers= DB::select(DB::raw("select * from answers where user_id='$user->id' "));
foreach($answers as $answer)
{
$questionDetNew = '';
$questionDetOld = DB::select(DB::raw("select * from questions where id='$answer->question_id' "));
if(is_array($questionDetOld))
echo "questionDetOld true";
foreach ($questionDetOld as $question) {
# code...
echo "<br/>";
echo $question->id;
echo $question->question_text;
echo $user->event; echo"<br/>";
$questionDetNew= DB::select(DB::raw("select * from questions where question_text='$question->question_text' and event='$user->event' and question_group='$question->question_group' "));
}
if(is_array($questionDetNew)){
foreach ($questionDetNew as $questionx) {
# code..
if($questionx->id!=NULL){
echo "Updated<br/>";
//DB::select(DB::raw("update answers set question_id='$questionx->id' where id='$answer->id' "));
}
else {
echo "Its already existing ";
}
}
}
}
}
}
更新
根据提供的评论,我想出了以下内容,但执行时间仍然没有太大差异
ini_set('max_execution_time', 800);
$users= DB::select(DB::raw("select * from users where role!='admin_dd' and role!='sales_dd' "));
// $users = User::where('role','!=','admin_dd')->orWhere('role','!=','sales_dd')->orderby('name')->get();
$answers= DB::select(DB::raw("select * from answers "));
$questionDetOld = DB::select(DB::raw("select * from questions"));
foreach($users as $user)
{
foreach ($answers as $answer) {
if($answer->user_id==$user->id)
{
foreach($questionDetOld as $question)
{
if($question->id==$answer->question_id )
{
echo "<br/>";
echo $question->id;
echo $question->question_text;
echo $user->event; echo"<br/>";
//$questionDetNew= DB::select(DB::raw("select * from questions where question_text='$question->question_text' and event='$user->event' and question_group='$question->question_group' "));
foreach ($questionDetOld as $questionx) {
if($questionx->question_text==$question->question_text && $questionx->event==$user->event && $questionx->question_group==$question->question_group)
{
echo "Updated<br/>";
}
}
}
}
}
}
}
最佳答案
首先,你在数据库中有正确的索引吗? 其次,循环内有太多循环。在大量数据上,它可能非常慢。您可以通过准备由关键字段索引的数组来优化此脚本。这是一个例子:
ini_set('max_execution_time', 800);
$userIds = array();
$users= DB::select(DB::raw("select * from users where role!='admin_dd' and role!='sales_dd' "));
forach ($users as $user) {
$userIds[] = $user->id;
}
$questionIds = array();
$answers_tmp= DB::select(DB::raw("select * from answers where user_id in (".implode(',',$userIds).')'));
foreach ($answers_tmp as $answer) {
$answers[$answer->user_id][] = $answer;
$questionIds[] = $answer->question_id;
}
$questionDetOld_tmp = DB::select(DB::raw("select * from questions where id in (".implode(',',$questionIds).')'));
foreach ($questionDetOld_tmp as $question) {
$questionDetOld[$question->id][] = $question;
}
foreach($users as $user)
{
if (isset($answers[$user->id]))
{
foreach ($answers[$user->id] as $answer)
{
if (isset($questionDetOld[$answer->question_id]))
{
foreach($questionDetOld[$answer->question_id] as $question)
{
echo "<br/>";
echo $question->id;
echo $question->question_text;
echo $user->event; echo"<br/>";
//$questionDetNew= DB::select(DB::raw("select * from questions where question_text='$question->question_text' and event='$user->event' and question_group='$question->question_group' "));
foreach ($questionDetOld_tmp as $questionx)
{
if($questionx->question_text==$question->question_text && $questionx->event==$user->event && $questionx->question_group==$question->question_group)
{
echo "Updated<br/>";
}
}
}
}
}
}
}
关于php - 我的代码执行完成花费了太多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32494660/
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的主要目标是能够完全理解我正在使用的库/gem。我尝试在Github上从头到尾阅读源代码,但这真的很难。我认为更有趣、更温和的踏脚石就是在使用时阅读每个库/gem方法的源代码。例如,我想知道RubyonRails中的redirect_to方法是如何工作的:如何查找redirect_to方法的源代码?我知道在pry中我可以执行类似show-methodmethod的操作,但我如何才能对Rails框架中的方法执行此操作?您对我如何更好地理解Gem及其API有什么建议吗?仅仅阅读源代码似乎真的很难,尤其是对于框架。谢谢! 最佳答案 Ru
我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试
我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的
这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build