【问题标题】:AWS EC2 PHP, Laravel, MySQL PerformanceAWS EC2 PHP、Laravel、MySQL 性能
【发布时间】:2016-07-28 18:50:40
【问题描述】:

我们刚刚运行了一个亚马逊 EC2 实例,我们已经在 ec2 实例上部署了我们的 Web 应用程序,并且应用程序是在 Laravel 5.0 框架中开发的。但是我们面临一些与性能相关的问题,需要一些建议/输入同样。基本上我们正在做的是我们有一个AWS RDS实例,我们的表结构如下:

    CREATE TABLE IF NOT EXISTS `adwords_data` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `client_id` bigint(20) NOT NULL,
  `date_preset` enum('YESTERDAY','LAST_7_DAYS','LAST_14_DAYS','LAST_30_DAYS','ALL_TIME','7_DAYS_PRIOR_YESTERDAY','30_DAYS_PRIOR_YESTERDAY','60_DAYS_PRIOR_YESTERDAY','90_DAYS_PRIOR_YESTERDAY') COLLATE utf8_unicode_ci NOT NULL,
  `CampaignId` bigint(20) NOT NULL,
  `CampaignName` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `AdgroupId` bigint(20) NOT NULL,
  `AdgroupName` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `Clicks` int(11) NOT NULL,
  `Impressions` int(11) NOT NULL,
  `ConvertedClicks` int(11) NOT NULL,
  `Conversions` int(11) NOT NULL,
  `Cost` decimal(10,2) NOT NULL,
  `AveragePosition` decimal(10,2) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`),
  KEY `adwords_data_client_id_index` (`client_id`),
  KEY `adwords_data_date_preset_index` (`date_preset`),
  KEY `adwords_data_campaignid_index` (`CampaignId`),
  KEY `adwords_data_adgroupid_index` (`AdgroupId`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

和上面一样,我有更多的 6 个表,但字段有点不同

这是 my.cnf 的外观

#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
[client]
loose-local-infile = 1
port        = 3306
socket      = /var/run/mysqld/mysqld.sock

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

# This was formally known as [safe_mysqld]. Both versions are currently parsed.
[mysqld_safe]
socket      = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqld]
#
# * Basic Settings
#
local-infile = 1
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
basedir     = /usr
datadir     = /var/lib/mysql
tmpdir      = /tmp
lc-messages-dir = /usr/share/mysql
lower_case_table_names=1
table_open_cache=512
query_alloc_block_size=16384
thread_concurrency=8
join_buffer_size=2M
read_buffer_size=4M
tmp_table_size=1024M
max_heap_table_size=1024M # added
innodb_lock_wait_timeout=50
#table_cache=4960
query_cache_size=99M # changed from 1024
query_cache_type=ON # added
myisam_sort_buffer_size=64M
symbolic-links=0
thread_cache_size=16
read_rnd_buffer_size=32M
query_prealloc_size=16384
key_buffer_size=1024M
sort_buffer_size=2M
tmpdir="/tmp"
ft_min_word_len=3
default-storage-engine=MyISAM
innodb_file_per_table=1
innodb_flush_log_at_trx_commit=2
max_allowed_packet=16M
open_files_limit=49996
user            = mysql


skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address        = 127.0.0.1
#
# * Fine Tuning
#
key_buffer      = 16M
max_allowed_packet  = 16M
thread_stack        = 192K
thread_cache_size       = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover         = BACKUP
max_connections        = 3000
#table_cache            = 64
#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit   = 1M
query_cache_size        = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
#log_slow_queries   = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id      = 1
#log_bin            = /var/log/mysql/mysql-bin.log
expire_logs_days    = 10
max_binlog_size         = 100M
#binlog_do_db       = include_database_name
#binlog_ignore_db   = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem



[mysqldump]
quick
quote-names
max_allowed_packet  = 16M

[mysql]
local-infile = 1
#no-auto-rehash # faster start of mysql but no tab completition

[isamchk]
key_buffer      = 16M

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/

我们的应用程序针对不同的日期范围调用 Google Adwords API,并将返回的数据存储在 CSV 文件中。我们处理这个 CSV 文件,将数据存储在 mysql 数据库表中(大约 200,000 条记录),然后我们需要处理这 200,000记录,我们需要扫描 200 K 记录中的每一个,然后应用一些业务逻辑(那是我们的内部逻辑意味着应用一些 mysql 聚合,如 Sum、Avg 等)但是处理这 200 K 记录需要很长时间,大约 2 小时但是我们需要缩短处理时间。

下面是我们用来处理这 20 万条记录的代码库。我们使用 Laravel 的 chunk 方法来批量处理数据

https://laravel.com/docs/5.0/eloquent

\DB::table('adwords_data')->select('CampaignId', 'AdgroupId')->where('client_id', '=', $client_id)->groupBy('CampaignId', 'AdgroupId')->chunk('500', function ($adwords_data) {
                foreach($adwords_data as $data){
                    // process each AdgroupId by checking the summation of conversion stats and apply conditions
                }
}

在完成表 adwords_data 的过程后,我们对其他 6 个表重复上述过程。但性能不如我们需要更快地执行。

谁能建议我们提高性能的最佳方法?

谢谢!

【问题讨论】:

  • 您的问题缺乏足够的细节,无法让任何人提供好的建议。我不是 MySQL 专家,但你的表定义对我来说看起来不错,不是火箭科学 - 所以潜在的问题是 1)MySQL 配置(“my.cnf”)没有配置为最佳 2)你的处理代码写得不好 3)你如果您使用的是 AWS nano,可能需要更大的机器。我建议您提供 my.cnf 文件以及您的代码,这可能会增加您获得合理帮助的机会。祝你好运
  • @fiprojects 当然我可以为您提供!你能分享我你的电子邮件ID更多细节我会通过电子邮件向你解释
  • 没有更多信息可以回答?我的电子邮件就是我的个人资料
  • Ronak - 这是一个技术论坛 - 虽然我可能会提供帮助,但我可能无法提供最佳解决方案 - 您最好在论坛中分享您的代码,以增加获得最佳解决方案的机会最好的人的帮助。
  • @fiprojects 我已经编辑了帖子,现在有用吗?

标签: php mysql amazon-web-services amazon-ec2 laravel-5


【解决方案1】:

Ronak - 您可能认为人们对上面的 cmets 过于苛刻,但您正在寻找多个问题的答案(您甚至不清楚这些问题是什么)。民间会帮你拧紧一些松动的螺丝,但他们不会做繁重的工作。你需要自己为此承担一些责任——要么通过发展自己的技能,要么从一个或多个人那里获得有偿帮助。

根据您的项目的信息和缺乏背景,那些好心的人将竭尽全力帮助您前进。

您遇到的问题需要多种技能,而不仅仅是在任何论坛上进行一次问答,首先要确定问题的根源,然后再进行更多问答来实施解决方案。

本论坛将帮助回答与特定任务相关的技术问题。用最基本的术语来说,stackoverflow 可能会帮助您更换汽车的车轮,但它不会为您设计汽车。

问问自己...您遇到的性能问题可能在哪里?性能问题可能是磁盘、网络、cpu、内存或以上所有问题。仅仅因为某些事情花费的时间比您预期的要长,就意味着您需要将问题分解...“分而治之”。

这台机器上还有其他东西在运行吗?什么尺寸的机器?

您是否对您的数据库配置进行了基准测试或选择了一个开箱即用的配置,而没有考虑过您将投入什么?

这可能是您省略并总结为 “通过检查转化统计数据和应用条件的总和来处理每个 AdgroupId”的大部分代码中的问题 您可能正在咀嚼大量数据在您的代码中,并可能使用构思不当的方法进行排序或查找,对内存或 CPU 的使用不足,或者,您总是可以在一台不符合您期望的机器上拥有完美的代码和完美的数据库。

分而治之 - 测量在 for/while 循环之外处理的持续时间,测量读取/写入数据库的持续时间,首先检查那些耗时最长的。

祝你好运

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 2014-05-20
    相关资源
    最近更新 更多