这是我第一次接近一个非常高容量的情况。这是一个基于 MySQL 的广告服务器。但是,使用的查询包含很多 JOIN 并且通常很慢。(这是 Rails ActiveRecord,顺便说一句)
sel = Ads.find(:all, :select => '*', :joins => "在 ads.campaign_id =campaign.id 上加入活动campaign.id LEFT JOIN 关键字 ON keywords.campaign_id = campaign.id", :conditions => [flashstr + "keywords.word = ? AND ads.format = ? AND campaign.cenabled = 1 AND (countries.country IS NULL OR countries .country = ?) AND ads.enabled = 1 AND campaign.dailyenabled = 1 AND users.uenabled = 1", kw, format, viewer['country'][0]], :order => order, :limit =>限制)
我的问题:
有没有像 MySQL 这样支持 JOIN 但速度更快的替代数据库?(我知道有 Postgre,仍在评估它。)
否则,启动 MySQL 实例,将本地数据库加载到内存中并每 5 分钟重新加载一次会有帮助吗?
否则,有什么方法可以将整个操作切换到 Redis 或 Cassandra,并以某种方式更改 JOIN 行为以匹配 NoSQL 的(不可加入)性质?
谢谢!
编辑:这里有更多细节:
使用扁平化选择(上面截断)的完整执行 SQL:
选择活动.id、活动.guid、活动.user_id、活动.dailylimit、活动.impressions、活动.cenabled、活动.dayspent、活动.dailyenabled、活动.fr、ads.id、ads.guid、ads.user_id、广告.campaign_id, ads.format, ads.enabled, ads.datafile, ads.data1, ads.data2, ads.originalfilename, ads.aid, ads.impressions, countries.id, countries.guid, countries.campaign_id, countries.country ,keywords.id,keywords.campaign_id,keywords.word,keywords.bid FROM
ads
在 ads.campaign_id =campaign.id 上加入广告系列 在campaigns.user_id = users.id 上加入用户 在国家/地区.campaign_id =campaigns.id 上左加入国家/地区在keywords.campaign_id =campaigns.id 上加入关键字' AND ads.format = 10 AND campaign.cenabled = 1 AND (countries.country IS NULL OR countries.country = 82) AND ads.enabled = 1 AND campaign.dailyenabled = 1 AND users.uenabled = 1 AND ads.datafile ! = '') ORDER BY keywords.bid DESC LIMIT 1,1
解释/执行计划:
+----+-------------+-----------+--------+------------------+-------------+---------+------------------------------------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+--------+------------------+-------------+---------+------------------------------------+------+----------------------------------------------+
| 1 | SIMPLE | keywords | ref | campaign_id,word | word | 257 | const | 9 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | ads | ref | campaign_id | campaign_id | 4 | e_development.keywords.campaign_id | 8 | Using where |
| 1 | SIMPLE | campaigns | eq_ref | PRIMARY | PRIMARY | 4 | e_development.keywords.campaign_id | 1 | Using where |
| 1 | SIMPLE | users | eq_ref | PRIMARY | PRIMARY | 4 | e_development.campaigns.user_id | 1 | Using where |
| 1 | SIMPLE | countries | ALL | campaign_id | NULL | NULL | NULL | 4 | Using where |
+----+-------------+-----------+--------+------------------+-------------+---------+------------------------------------+------+----------------------------------------------+
(这是在一个开发数据库上,它的行数几乎没有生产版本那么多。)
定义的指数:
ads -> id (primary, autoinc) + aid (unique) + campaign_id (index) + user_id (index)
campaigns -> id (primary, autoinc) + user_id (index)
countries -> id (primary, autoinc) + campaign_id (index) + country (index) + user_id (index)
keywords -> id (primary, autoinc) + campaign_id (index) + word (index) + user_id (index)
user -> id (primary, autoinc)