MySQL存储过程在服务器上比localhost花费更长的时间

时间:2012-10-15 08:50:58

标签: mysql webserver localhost

我创建了一个存储过程,其查询在localhost上运行:

在0,0065s(0,1426s)中提取21行

在服务器上:

在0,0037s(15,9947s)中获取了21行

我尝试了以下选项,以便在localhost上获得与服务器相同的结果:

  • SQL_NO_CACHE
  • 重置缓存
  • 刷新缓存

但它没有给我相同的结果,所以我想知道为什么在服务器上运行的查询要慢得多。

我已经运行了一个解释,但我真的不知道我能从中做些什么:

1, 'PRIMARY', '<derived2>', 'ALL', '', '', '', '', 16106, 100.00, 'Using temporary; Using filesort'
1, 'PRIMARY', 'A', 'ALL', 'PRIMARY', '', '', '', 5214, 100.00, 'Using where; Using join buffer'
2, 'DERIVED', 'T', 'ALL', 'PRIMARY', '', '', '', 76211, 100.00, 'Using wher

E'

这是我正在运行的查询:

SELECT
        `A`.`country_description`,
    SUM(`turnover_ytd`) AS `turnover_currentyear_ytd`,
    SUM(`turnover_ly`) AS `turnover_lastyear_ytd`,
    SUM(`turnover_sly`) AS `turnover_2ndlastyear_ytd`,
    SUM(`turnover_ytd`) AS `volume_currentyear_ytd`,
    SUM(`turnover_ly`) AS `volume_lastyear_ytd`,
    SUM(`turnover_sly`) AS `volume_2ndlastyear_ytd`,
    COALESCE(((SUM(`turnover_ytd`)-SUM(`turnover_ly`))/SUM(`turnover_ly`))*100, 0) AS `turnover_percentage_currentandlastyear_ytd`,
        COALESCE(((SUM(`volume_ytd`)-SUM(`volume_ly`))/SUM(`volume_ly`))*100, 0) AS `volume_percentage_currentandlastyear_ytd`,
        COALESCE(((SUM(`turnover_ly`)-SUM(`turnover_sly`))/SUM(`turnover_sly`))*100, 0) AS `turnover_percentage_lastand2ndlastyear_ytd`,
        COALESCE(((SUM(`volume_ly`)-SUM(`volume_sly`))/SUM(`volume_sly`))*100, 0) AS `volume_percentage_lastand2ndlastyear_ytd`


  FROM
        `agent` AS `A`
        INNER JOIN
        (
            SELECT
                `debtor_number`,
                IF(SUBSTR(`period`,1,4) = SUBSTR(201210,1,4), `turnover_ytd`, 0) AS `turnover_ytd`,
                IF(SUBSTR(`period`,1,4) = SUBSTR(201210-100,1,4), `turnover_ytd`, 0) AS `turnover_ly`,
        IF(SUBSTR(`period`,1,4) = SUBSTR(201210-200,1,4), `turnover_ytd`, 0) AS `turnover_sly`,
        IF(SUBSTR(`period`,1,4) = SUBSTR(201210,1,4), `volume_ytd`, 0) AS `volume_ytd`,
                IF(SUBSTR(`period`,1,4) = SUBSTR(201210-100,1,4), `volume_ytd`, 0) AS `volume_ly`,
        IF(SUBSTR(`period`,1,4) = SUBSTR(201210-200,1,4), `volume_ytd`, 0) AS `volume_sly`
            FROM
                `turnover_volume` AS `T`
            WHERE
        `company_number` = 01
      AND
        SUBSTR(`period`,5,2) <= SUBSTR(201210,5,2)
      AND
        SUBSTR(`period`,1,4) IN (SUBSTR(201210,1,4), SUBSTR(201210-100,1,4), SUBSTR(201210-200,1,4))
      AND
        `turnover_ytd` != 0
      AND
        `volume_ytd` != 0

    ) AS `T` ON (`T`.`debtor_number` = `A`.`debtor_number`)

    WHERE
    `A`.`agent_number` = 003
  GROUP BY
    `A`.`country_description`;

服务器规格是:

主机是运行ESX的物理服务器,Web服务器实际上是作为ESX内的来宾运行。

主机规格为:

品牌和类型:Altos R720 CPU:Xeon E5404(2ghz) 存储:通过Iscsi存储连接。 (Eternus DX80) 内存:6 GB

嘉宾规格如下: CPU:2x vCPU 内存:2 GB

操作系统是:Red Hat 4.1.2-44

除了这些规格外,客人还可以无限制地访问主机。

我的本​​地PC规格是:

CPU:Intel Core 2 Quad CPU Q9300 @ 2.50GHz 内存:3 GB 操作系统:Windows 7 32位

非常感谢帮助。

2 个答案:

答案 0 :(得分:0)

您是否考虑过与本地计算机相比的服务器硬件?

这是一个共享主机包吗?专用服务器?云?

答案 1 :(得分:0)

我发现了问题,不知何故,网络服务器上的Collat​​ion类型与我的localhost数据库相比有所不同。

在localhost上我有:

latin1_swedish_ci

在服务器上它是:

utf8_unicode_ci

表上的引擎也不同,InnoDB vs MyIsam在服务器上......

通过浏览不同的Collat​​ion类型之间的翻译,这会导致查询变慢。