我有一个实时服务器和我的开发服务器,我发现在我的LIVE(不是开发)服务器上的查询运行速度慢 10 倍,即使实时服务器更强大并且它们都是运行可比较的负载。这不是数据库结构问题,因为我将实时服务器的备份加载到我的开发服务器中。
有人知道我可以在哪里寻找差异吗?它可能是 MySQL 配置的东西吗?我应该从哪里开始寻找?
直播服务器:
mysql> SELECT count(`Transaction`.`id`) as count, sum(`Transaction`.`amount`) as sum, sum(Transaction.citiq_margin+rounding + Transaction.citiq_margin_vat) as revenue FROM `transactions` AS `Transaction` LEFT JOIN `meters` AS `Meter` ON (`Transaction`.`meter_id` = `Meter`.`id`) LEFT JOIN `units` AS `Unit` ON (`Meter`.`unit_id` = `Unit`.`id`) WHERE (NOT (`Unit`.`building_id` IN ('1', '85')) AND NOT (`Transaction`.`state` >= 90)) AND DAY(`Transaction`.`created`) = DAY(NOW()) AND YEAR(`Transaction`.`created`) = YEAR(NOW()) AND (MONTH(`Transaction`.`created`)) = MONTH(NOW()); +-------+---------+---------+ | count | sum | revenue | +-------+---------+---------+ | 413 | 3638550 | 409210 | +-------+---------+---------+ 1 row in set (2.62 sec) [root@mises ~]# uptime 17:11:57 up 55 days, 1 min, 1 user, load average: 0.45, 0.56, 0.60
开发服务器(结果计数不同,因为备份有轻微的时间延迟):
mysql> SELECT count(`Transaction`.`id`) as count, sum(`Transaction`.`amount`) as sum, sum(Transaction.citiq_margin+rounding + Transaction.citiq_margin_vat) as revenue FROM `transactions` AS `Transaction` LEFT JOIN `meters` AS `Meter` ON (`Transaction`.`meter_id` = `Meter`.`id`) LEFT JOIN `units` AS `Unit` ON (`Meter`.`unit_id` = `Unit`.`id`) WHERE (NOT (`Unit`.`building_id` IN ('1', '85')) AND NOT (`Transaction`.`state` >= 90)) AND DAY(`Transaction`.`created`) = DAY(NOW()) AND YEAR(`Transaction`.`created`) = YEAR(NOW()) AND (MONTH(`Transaction`.`created`)) = MONTH(NOW()); +-------+---------+---------+ | count | sum | revenue | +-------+---------+---------+ | 357 | 3005550 | 338306 | +-------+---------+---------+ 1 row in set (0.22 sec) [www@smith test]$ uptime 18:11:53 up 12 days, 1:57, 4 users, load average: 0.91, 0.75, 0.62
实时服务器(2 x Xeon Quadcore):
processor : 7 vendor_id : GenuineIntel cpu family : 6 model : 44 model name : Intel(R) Xeon(R) CPU E5620 @ 2.40GHz stepping : 2 cpu MHz : 2395.000 cache size : 12288 KB physical id : 0 siblings : 8 core id : 10 cpu cores : 4
开发服务器(1 个四核)
processor : 3 vendor_id : GenuineIntel cpu family : 6 model : 23 model name : Intel(R) Core(TM)2 Quad CPU Q8300 @ 2.50GHz stepping : 10 microcode : 0xa07 cpu MHz : 1998.000 cache size : 2048 KB physical id : 0 siblings : 4 core id : 3 cpu cores : 4
直播服务器:
- 中央操作系统 5.7
- MySQL 版本 5.0.95
开发服务器:
- ArchLinux
- MySQL 版本 5.5.25a
最佳答案
显然首先要检查的是您的 MySql 配置文件,以确保您使用了适当数量的内存来进行查询……例如 key_buffer、sort_buffer 等……有比我聪明得多的人有整个博客专门用于配置 MySql。
您还可以在查询前加上“解释”,以查看花费最多时间的是什么……但这可能只是以后的一般用途。
实际上,您的“实时”服务器具有缓存功能和两倍数量的内核来进行这些查询,并且它可能有足够的能力和内存来解释服务器之间查询时间的差异。
关于与另一台服务器相比,一台服务器上的 MySQL 速度慢 10 倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11762406/