我正在从fixtures
表中显示用户最喜欢的球队的3场即将举行和过去3场比赛。我的查询工作正常,但我需要改进查询。因为当大约有100位用户在应用程序上活动时,我面临着很高的CPU负载。我不是mongoDB的专家。
我在users
表中存储了用户喜欢的团队ID。 users
表中的示例数据
[{
"_id":1,
"favorite_team_ids" : "#228764#,#742#,#18#,#19#",
"udid" : "724DDF36-",
"state" : "1",
"PN_Token" : "e2304daed",
"status":"active"
},
{
"_id":2,
"favorite_team_ids" : "#17#,#742#,#28231#,#19#",
"udid" : "724DD545A-",
"state" : "1",
"PN_Token" : "e2304daed",
"status":"active"
}]
我使用单独的mongodb查询获取团队ID,并将其分解为数组。
然后,我根据fixtures
表中的每个团队ID进行循环,以获取$lte
时间戳记的3场即将进行的比赛和3场过去的比赛的数据
从Fixtures
表中采样数据
[{
"_id" : 10443712,
"league_id" : 1270,
"season_id" : 14977,
"stage_id" : 7743503,
"round_id" : 155104,
"venue_id" : 292239,
"localteam_id" : 228764,
"visitorteam_id" : 28231,
"weather_report" : null,
"commentaries" : false,
"attendance" : null,
"pitch" : null,
"winning_odds_calculated" : false,
"formations" : {
"localteam_formation" : null,
"visitorteam_formation" : null
},
"scores" : {
"localteam_score" : 0,
"visitorteam_score" : 0,
"localteam_pen_score" : 0,
"visitorteam_pen_score" : 0,
"ht_score" : null,
"ft_score" : null,
"et_score" : null
},
"time" : {
"status" : "NS",
"starting_at" : {
"date_time" : "2018-11-01 00:00:00",
"date" : "2018-11-01",
"time" : "00:00:00",
"timestamp" : 1541030400,
"timezone" : "UTC"
},
"minute" : null,
"second" : null,
"added_time" : null,
"extra_minute" : null,
"injury_time" : null
},
"localTeam" : {
"data" : {
"id" : 228764,
"legacy_id" : null,
"name" : "Mazarrón FC",
"short_code" : null,
"twitter" : null,
"country_id" : null,
"national_team" : null,
"founded" : null,
"logo_path" : null,
"venue_id" : null
}
},
"visitorTeam" : {
"data" : {
"id" : 28231,
"legacy_id" : 10184,
"name" : "UCAM Murcia II",
"short_code" : null,
"twitter" : null,
"country_id" : 32,
"national_team" : false,
"founded" : 0,
"logo_path" : "",
"venue_id" : 20474
}
},
"venue" : {
"data" : {
"id" : 292239,
"name" : "Estadio Municipal de Mazarrón (Mazarrón)",
"surface" : "",
"address" : "",
"city" : "Mazarrón",
"capacity" : null,
"image_path" : null,
"coordinates" : null
}
},
"added_on" : "2018-11-01 04:42:27",
"updated_on" : "2018-11-01 04:44:35"
},
{
"_id" : 10652017,
"league_id" : 331,
"season_id" : 13253,
"stage_id" : 7743445,
"round_id" : 156903,
"venue_id" : null,
"referee_id" : null,
"localteam_id" : 742,
"visitorteam_id" : 27912,
"weather_report" : null,
"commentaries" : false,
"attendance" : null,
"pitch" : null,
"winning_odds_calculated" : false,
"formations" : {
"localteam_formation" : null,
"visitorteam_formation" : null
},
"scores" : {
"localteam_score" : 0,
"visitorteam_score" : 0,
"localteam_pen_score" : 0,
"visitorteam_pen_score" : 0,
"ht_score" : null,
"ft_score" : null,
"et_score" : null
},
"time" : {
"status" : "NS",
"starting_at" : {
"date_time" : "2018-11-01 13:00:00",
"date" : "2018-11-01",
"time" : "13:00:00",
"timestamp" : 1541077200,
"timezone" : "UTC"
},
"minute" : 0,
"second" : null,
"added_time" : null,
"extra_minute" : null,
"injury_time" : null
},
"leg" : "1/1",
"colors" : null,
"deleted" : false,
"localTeam" : {
"data" : {
"id" : 742,
"legacy_id" : 861,
"name" : "Apollon Smirnis",
"short_code" : null,
"twitter" : null,
"country_id" : 125,
"national_team" : false,
"founded" : 1891,
"logo_path" : "",
"venue_id" : 223
}
},
"visitorTeam" : {
"data" : {
"id" : 27912,
"legacy_id" : 9174,
"name" : "Apollon Paralimniou",
"short_code" : null,
"twitter" : null,
"country_id" : 125,
"national_team" : false,
"founded" : 0,
"logo_path" : "",
"venue_id" : 22197
}
},
"added_on" : "2018-11-01 04:42:27",
"updated_on" : "2018-11-01 04:44:36"
}]
基本上,我正在使用两个查询来获取单个团队的数据。 第一场获得3场即将进行的比赛,第二场获得过去3场比赛
查询即将进行的比赛
{
"find" : "tbl_fixtures",
"filter" : {
"$or" : [
{
"localteam_id" : 228481
},
{
"visitorteam_id" : 228481
}
],
"time.starting_at.timestamp" : {
"$gte" : 1545220545
}
},
"projection" : {
"league_id" : 1,
"season_id" : 1,
"stage_id" : 1,
"round_id" : 1,
"scores" : 1,
"time" : 1,
"localTeam" : 1,
"visitorTeam" : 1,
"_id" : 1
},
"sort" : {
"time.starting_at.timestamp" : 1
},
"limit" : 3,
}
查询过去的比赛
{
"find" : "tbl_fixtures",
"filter" : {
"$or" : [
{
"localteam_id" : 228481
},
{
"visitorteam_id" : 228481
}
],
"time.starting_at.timestamp" : {
"$lte" : 1545220545
}
},
"projection" : {
"league_id" : 1,
"season_id" : 1,
"stage_id" : 1,
"round_id" : 1,
"scores" : 1,
"time" : 1,
"localTeam" : 1,
"visitorTeam" : 1,
"_id" : 1
},
"sort" : {
"time.starting_at.timestamp" : 1
},
"limit" : 3,
}
我在MongoDB 4.0.4
上将PHP 7.2
与CentOS 7
一起使用