【问题标题】:How to find what mysql query consuming more cpu resources?如何找到消耗更多cpu资源的mysql查询?
【发布时间】:2019-10-21 06:15:30
【问题描述】:

我用的是VPS服务器,cpu资源一直显示99%,怎么找mysql查询哪个cpu资源比较消耗。

【问题讨论】:

    标签: mysql resources magento-1.9 cpu-usage vps


    【解决方案1】:

    首先你应该 ssh 进入服务器然后检查高处理查询:

    连接mysql -u root -p 输入SHOW PROCESSLIST; 您也可以使用SHOW FULL PROCESSLIST 来查看完整的查询。

    这将显示当时正在运行的所有查询。

    第二:你应该启用Slow queries log并检查它。确保你没有任何查询锁定键表太久。

    第三:你应该检查你的mysql用户并确保没有人远程访问它。

    最后:启用默认的 magento 分析器并检查缓慢的进程。 为了启用它,请转到:

    System > Configuration > Advanced > Developer > Debug > Profiler > Yes

    然后打开 Magento 安装的根 index.php 文件,然后查找已注释的 Varien_Profiler::enable(); 部分并取消注释。

    如果要启用 sql 分析,请将 <profiler>true</profiler> 添加到 app/etc/local.xml

    例子:

    <resources>
     <db>
      <table_prefix><![CDATA[]]></table_prefix>
     </db>
     <default_setup>
      <connection>
       <host><![CDATA[localhost]]></host>
       <username><![CDATA[root]]></username>
       <password><![CDATA[]]></password>
       <dbname><![CDATA[magento_1_8_1_0]]></dbname>
       <initStatements><![CDATA[SET NAMES utf8]]></initStatements>
       <model><![CDATA[mysql4]]></model>
       <type><![CDATA[pdo_mysql]]></type>
       <pdoType><![CDATA[]]></pdoType>
       <active>1</active>
       <profiler>true</profiler>
      </connection>
     </default_setup>
    </resources>
    

    您甚至可以通过添加来分析单个代码

    //First, enable the profiler
    Varien_Profiler::enable();
    
    //Second, start the profiler with a unique label
    
    Varien_Profiler::start('LabelForCodeFragmentToProfile');
    
    ///... here some code we like to get statistics for
    
    //next, stop your profiler with the same label
    Varien_Profiler::stop('LabelForCodeFragmentToProfile');
    
    //Last, disable your profiler
    Varien_Profiler::disable();
    

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 我正在使用 MySQL,如何找到它? @Abhinaw
      • 从SSH登录,考虑使用SHOW FULL PROCESSLIST;另一个有价值的资源是慢查询日志。考虑以下 my.cnf,A) slow_query_log=1 B) min_examined_row_limit=1 C) long_query_time=2(秒)要确定自启动以来是否有任何 SLOW QUERIES,SHOW GLOBAL STATUS LIKE 'slow_queries';随时。如果 performance_schema 为 ON,您有很多方法可以找到“慢”事件。
      猜你喜欢
      • 1970-01-01
      • 2019-06-15
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 2015-10-30
      相关资源
      最近更新 更多