我正面临MongoDB查询执行时间缓慢的问题。
我的基本情况是,我正在使用MongoDB(Linux-x86_64-3.4.4)作为数据库在Sails(Node)上开发分类广告网站。
我有两个收藏集
我们为用户提供2个不同的类别-个人和专业。因此,我们将其保存到广告集中,每个记录包含其类型为“个人”或“专业”。
现在,我想根据如下所示的用户搜索过滤器来显示广告总数
因此,我在下面的查询中使用了该方法。但是,现在它的性能出现了问题。
当我尝试使用带有attrs的Lookup进行查询时,它花费了 7.5-12秒。当我仅查询Ad表时,它花费了 0.2-1.2秒。
db.ad.aggregate(
[
{
"$match":{
"$and":[
{
"is_active":{
"$eq":1
}
},
{
"is_delete":{
"$eq":0
}
},
{
"is_block":{
"$eq":0
}
},
{
"is_online":{
"$eq":1
}
},
{
"ad_type":{
"$eq":"offers"
}
}
]
}
},
{
"$lookup":{
"from":"ad_attr",
"localField":"_id",
"foreignField":"ad_id",
"as":"attrs"
}
},
{
"$addFields":{
"totalAttrs":{
"$size":"$attrs"
}
}
},
{
"$project":{
"ad_user_type":1,
"attrs":1
}
},
{
"$group":{
"_id":null,
"cnt":{
"$sum":1
},
"pro":{
"$sum":{
"$cond":[
{
"$eq":[
"$ad_user_type",
"professional"
]
},
1,
0
]
}
},
"per":{
"$sum":{
"$cond":[
{
" $eq":[
"$ad_user_type",
"personal"
]
},
1,
0
]
}
}
}
}
]
)
集合结构如下
广告集
{
attributes: {
_id: {
type: 'string'
},
user_id: {
type: 'string'
},
title: {
type: 'string'
},
description: {
type: 'longtext'
},
price: {
type: 'float'
},
price_max: {
type: 'float'
},
ad_type: {
type: 'string'
},
ad_user_type: {
type: 'string'
},
zipcode: {
type: 'string'
},
lat: {
type: 'float'
},
long: {
type: 'float'
},
loc: {
type: 'object'
},
cityzip: {
type: 'string'
},
town: {
type: 'string'
},
town_slug: {
type: 'string'
},
region: {
type: 'string'
},
region_slug: {
type: 'string'
},
department: {
type: 'string'
},
department_slug: {
type: 'string'
},
address: {
type: 'longtext'
},
telephone: {
type: 'string'
},
is_account_address: {
type: 'integer',
defaultsTo: 1
},
is_phone_hide: {
type: 'integer',
defaultsTo: 0
},
video: {
type: 'string'
},
is_video_check: {
type: 'integer',
defaultsTo: 0
},
is_urgent: {
type: 'integer',
defaultsTo: 0
},
additional_picture: {
type: 'integer',
defaultsTo: 0
},
is_in_showcase: {
type: 'integer',
defaultsTo: 0
},
showcase_date: {
type: 'date'
},
showcase_duration: {
type: 'string',
defaultsTo: ""
},
showcase_duration_id: {
type: 'string'
},
is_in_top_list: {
type: 'integer',
defaultsTo: 0
},
top_list_date: {
type: 'date',
defaultsTo: ''
},
top_list_duration: {
type: 'string',
defaultsTo: ""
},
top_list_duration_id: {
type: 'string'
},
is_direct_sale: {
type: 'integer',
defaultsTo: 0
},
is_active: {
type: 'integer',
defaultsTo: 0
},
is_block: {
type: 'integer',
defaultsTo: 0
},
post_on_from_backend: {
type: 'date',
defaultsTo: ''
},
post_on: {
type: 'date',
defaultsTo: ''
},
post_end_on: {
type: 'date',
defaultsTo: ''
},
payment_status: {
type: 'string',
defaultsTo: 'pending'
},
price_token: {
type: 'string',
},
total_views: {
type: 'integer',
defaultsTo: 0
},
total_clicks: {
type: 'integer',
defaultsTo: 0
},
is_online: {
type: 'integer',
defaultsTo: 0
},
is_change_req: {
type: 'integer',
defaultsTo: 0
},
have_shop: {
type: 'integer',
defaultsTo: 0
},
shop_id: {
type: 'string',
defaultsTo: 0
},
slug: {
type: 'string'
},
category: {
type: 'string'
},
category_price: {
type: 'integer',
defaultsTo: 0
},
parent_category: {
type: 'string'
},
user_ip: {
type: 'string'
},
is_delete: {
type: 'integer',
defaultsTo: 0
},
product_name: {
type: 'string'
},
product_price: {
type: 'float',
defaultsTo: 0
},
original_item_quantity: {
type: 'float',
defaultsTo: 0
},
item_quantity: {
type: 'float',
defaultsTo: 0
},
shipping_type: {
type: 'string'
},
shipping_value_simple: {
type: 'float',
defaultsTo: 0
},
shipping_value_variable_min_1: {
type: 'integer',
defaultsTo: 0
},
shipping_value_variable_max_1: {
type: 'integer',
defaultsTo: 0
},
shipping_value_variable_1: {
type: 'float',
defaultsTo: 0
},
shipping_value_variable_min_2: {
type: 'integer',
defaultsTo: 0
},
shipping_value_variable_max_2: {
type: 'integer',
defaultsTo: 0
},
shipping_value_variable_2: {
type: 'float',
defaultsTo: 0
},
shipping_value_variable_min_3: {
type: 'integer',
defaultsTo: 0
},
shipping_value_variable_max_3: {
type: 'integer',
defaultsTo: 0
},
shipping_value_variable_3: {
type: 'float',
defaultsTo: 0
},
seller_paypal_url: {
type: 'string',
defaultsTo: ""
},
direct_sale_opt_1: {
type: 'string',
defaultsTo: ""
},
direct_sale_value_1: {
type: 'longtext',
defaultsTo: ""
},
direct_sale_opt_2: {
type: 'string',
defaultsTo: ""
},
direct_sale_value_2: {
type: 'longtext',
defaultsTo: ""
},
direct_sale_opt_3: {
type: 'string',
defaultsTo: ""
},
have_update: {
type: 'integer',
defaultsTo: 0
},
update_payment_status: {
type: 'string',
defaultsTo: ""
},
direct_sale_value_3: {
type: 'longtext',
defaultsTo: ""
},
attrs: {
collection: 'ad_attr',
via: 'ad_id'
},
reference: {
type: 'longtext',
defaultsTo: ""
},
emp_id: {
type: 'string',
defaultsTo: "0"
},
},
}
广告属性集合
attributes: {
ad_id:{
model:'ad'
},
cat_id:{
type:'string',
},
attr_id:{
type:'string',
},
value:{
type:'string',
},
integer_value:{
type:'float',
defaultsTo: 0,
}
}
为收藏添加索引 广告-我已将索引添加到用于条件的所有属性
Id , loc , post_on, price, ad_type, is_delete, is_active, is_online, is_block, ad_user_type, attrs, category, parent_category, region_slug, department_slug, town_slug, slug
广告属性
Id, attr_id, value, ad_id, attrs
服务器配置-适用于当前数据库大小
我到目前为止已经完成 -谷歌为所有可能性 -删除不需要查询计数和列表的属性
我希望您能帮我解决以下问题
专家,我需要您的帮助来解决此问题。提前致谢! :)