我不太习惯 MySQL,但我认为它可以比现在快得多。
这是我的桌子:
CREATE TABLE `crashes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`added_date` int(11) NOT NULL,
`status` int(11) NOT NULL,
`issue_id` varchar(32) NOT NULL,
`report_id` text NOT NULL,
`app_version_code` text NOT NULL,
`app_version_name` text NOT NULL,
`package_name` varchar(80) NOT NULL,
`package_name_id` tinyint(4) NOT NULL,
`file_path` text NOT NULL,
`phone_model` text NOT NULL,
`android_version` text NOT NULL,
`build` text NOT NULL,
`brand` text NOT NULL,
`product` text NOT NULL,
`total_mem_size` int(11) NOT NULL,
`available_mem_size` int(11) NOT NULL,
`custom_data` text NOT NULL,
`stack_trace` text NOT NULL,
`initial_configuration` text NOT NULL,
`crash_configuration` text NOT NULL,
`display` text NOT NULL,
`user_comment` text NOT NULL,
`user_app_start_date` text NOT NULL,
`user_crash_date` text NOT NULL,
`dumpsys_meminfo` text NOT NULL,
`dropbox` text NOT NULL,
`logcat` text NOT NULL,
`eventslog` text NOT NULL,
`radiolog` text NOT NULL,
`is_silent` text NOT NULL,
`device_id` text NOT NULL,
`installation_id` text NOT NULL,
`user_email` text NOT NULL,
`device_features` text NOT NULL,
`environment` text NOT NULL,
`settings_system` text NOT NULL,
`settings_secure` text NOT NULL,
`shared_preferences` text NOT NULL,
`application_log` text NOT NULL,
`media_codec_list` text NOT NULL,
`thread_details` text NOT NULL,
`user_ip` text NOT NULL,
PRIMARY KEY (`id`),
KEY `package_name_id` (`package_name_id`)
) ENGINE=MyISAM AUTO_INCREMENT=202364 DEFAULT CHARSET=utf8
如您所见,它充满了 200k 行。我想检索当天的行added_date
(unix 时间戳)和行数。int(11)
所以我选择日期、日期(作为 YMD)和计数:
SELECT date_format(from_unixtime(added_date), '%Y-%c-%d') as date, added_date, count(*) as nb_crashes FROM crashes WHERE package_name = 'net.bicou.redmine' GROUP BY date ORDER BY date ASC
这很慢!在我主机上的专用 mysql 服务器上几乎 1.5 秒。
所以我想我可以稍微优化一下:我添加了一个 package_name_id ,它是 a tinyint
,它是唯一的package_name
(我package_name
在那 200k 行上有 5 个不同的值)。我这样做是为了INDEX
让 MySQL 可以更快地浏览它。
结果:0.9 秒。这好多了,但仍然没有达到我期望的性能!
我怎样才能优化这个东西?我想在每一行上创建日期,然后分组非常昂贵。但是我不知道如何使这更快...
编辑:
这是我为更新表格所做的:
ALTER TABLE `crashes` ADD `temp` DATETIME NOT NULL
UPDATE crashes SET temp = FROM_UNIXTIME( added_date )
ALTER TABLE `crashes` ADD INDEX ( `temp` )
这是更新的查询:
SELECT added_date, count(*) as nb_crashes FROM crashes WHERE package_name_id=3 GROUP BY year(temp),month (temp),dayofmonth(temp) ORDER BY temp ASC
我仍然有大约一秒钟的执行时间......我做错了什么吗?