草庐IT

mysql - SELECT 查询偶尔会执行很长时间

coder 2023-10-14 原文

我的 MySQL InnoDB 数据库中有一个非常奇怪的问题。我有以下查询:

SELECT DISTINCT p.idProject AS idProject, p.name AS name, 0 AS isConfirm
FROM Projects p
    JOIN team_project tp ON (p.idProject = tp.idProject) 
    JOIN projtimes pt ON (p.idProject = pt.idProject) 
    JOIN CalledTimesTbl ctt ON (p.idProject = ctt.idProject)
    LEFT JOIN NextCalls nc ON (ctt.idCustomer = nc.idCustomer 
        AND ctt.idProject = nc.idProject) 
WHERE tp.idTeam = 158
    AND p.activated = 1 
    AND current_date >= p.projStart
    AND current_date < p.confirmStart 
    AND pt.invitesCount < pt.maxPerPresentation
    AND (nc.idCustomer IS NULL OR nc.nextCall < now())
ORDER BY p.name

通常查询运行良好,但有时 - 例如当我设置 tp.idTeam = 147 时,它运行得非常慢(比如 10 或 20 秒)。当我创建替代团队并调整适当的表值以获得具有不同 idTeam 值的相同结果时 - 查询在几分之一秒内执行。

我分析了查询并注意到当查询执行缓慢时 - 有一件事情消耗了大部分时间:

Copying to tmp table      | 12.489197

我对查询创建了一个 tmp 表感到有点惊讶,但是没关系——它会在每次查询执行时创建它——当它执行得很快时也是如此。 我只是补充说数据库设计得很好,有所有需要的外键等。

如何找到执行缓慢的根源并消除它?

编辑:EXPLAIN 结果:

id   select_type   table   type     possible_keys                    key              key_len   ref                                                   rows   Extra                             
1    SIMPLE        tp      ref      unique_row,idTeam                idTeam           4         const                                                 56     Using temporary; Using filesort   
1    SIMPLE        p       eq_ref   PRIMARY,projStart,confirmStart   PRIMARY          4         xxx.tp.idProject                                      1      Using where                       
1    SIMPLE        pt      ref      uniq_projtimes                   uniq_projtimes   4         xxx.tp.idProject                                      1      Using where; Distinct             
1    SIMPLE        ctt     ref      idProject                        idProject        4         xxx.tp.idProject                                      3966   Using index; Distinct             
1    SIMPLE        nc      eq_ref   PRIMARY,idProject                PRIMARY          8         xxx.ctt.idCustomer,xxx.tp.idProject                   1      Using where; Distinct     

EDIT2:EXPLAIN EXTENDED 的结果首先用于快速查询,其次用于慢速查询。

id   select_type   table   type     possible_keys                    key              key_len   ref                                           rows    filtered   Extra                  1    SIMPLE        tp      ref      unique_row,idTeam                idTeam           4         const                                                 1       100        Using temporary         
1    SIMPLE        p       eq_ref   PRIMARY,projStart,confirmStart   PRIMARY          4         xxx.tp.idProject                              1       100        Using where             
1    SIMPLE        pt      ref      uniq_projtimes                   uniq_projtimes   4         xxx.tp.idProject                              1       100        Using where; Distinct   
1    SIMPLE        ctt     ref      idProject                        idProject        4         xxx.tp.idProject                              46199   100        Using index; Distinct   
1    SIMPLE        nc      eq_ref   PRIMARY,idProject                PRIMARY          8         xxx.ctt.idCustomer,xxx.tp.idProject           1       100        Using index; Distinct  

id   select_type   table   type     possible_keys                    key              key_len   ref                                           rows   filtered   Extra                                
1    SIMPLE        p       eq_ref   PRIMARY,projStart,confirmStart   PRIMARY          4         xxx.ctt.idProject                             1      100        Using where                          
1    SIMPLE        pt      ref      uniq_projtimes                   uniq_projtimes   4         xxx.ctt.idProject                             1      100        Using where; Distinct                
1    SIMPLE        tp      ref      unique_row,idTeam                unique_row       8         xxx.pt.idProject,const                        1      100        Using where; Using index; Distinct   
1    SIMPLE        nc      eq_ref   PRIMARY,idProject                PRIMARY          8         xxx.ctt.idCustomer,xxx.tp.idProject           1      100        Using index; Distinct  

最佳答案

试试这个调整后的查询。 (它将加入更少的行)

SELECT DISTINCT p.idProject AS idProject, p.name AS name, 0 AS isConfirm
FROM Projects p
    JOIN projtimes pt ON 
        p.idProject = pt.idProject
        AND p.activated = 1
        AND current_date >= p.projStart
        AND current_date < p.confirmStart
        AND pt.invitesCount < pt.maxPerPresentation
    JOIN team_project tp ON 
        p.idProject = tp.idProject
        AND tp.idTeam = 158
    JOIN CalledTimesTbl ctt ON (p.idProject = ctt.idProject)
    LEFT JOIN NextCalls nc ON (ctt.idCustomer = nc.idCustomer 
        AND ctt.idProject = nc.idProject) 
WHERE (nc.idCustomer IS NULL OR nc.nextCall < now())
ORDER BY p.name

关于mysql - SELECT 查询偶尔会执行很长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15500426/

有关mysql - SELECT 查询偶尔会执行很长时间的更多相关文章

  1. ruby-openid:执行发现时未设置@socket - 2

    我在使用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

  2. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用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.

  3. ruby - Chef 执行非顺序配方 - 2

    我遵循了教程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

  4. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  5. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  6. ruby - 为什么 Ruby 的 each 迭代器先执行? - 2

    我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试

  7. ruby - 检查是否通过 require 执行或导入了 Ruby 程序 - 2

    如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby​​文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否

  8. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

  9. postman——集合——执行集合——测试脚本——pm对象简单示例02 - 2

    //1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json

  10. ruby-on-rails - rbenv:从 RVM 移动到 rbenv 后,在 Jenkins 执行 shell 中找不到命令 - 2

    我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions

随机推荐