针对性能优化Cypher查询-社交图

时间:2019-06-25 10:57:01

标签: neo4j cypher

我有一个Cypher查询,可用于获取实时用户供稿。用户有朋友,用户也关注其他人。该查询返回正确的结果,但是返回结果花费的时间相对较长(11395毫秒)。

我已经在节点的以下属性上创建了索引 用户(userId,IsActive,FeaturedUser) Feed(AccessScope)

我还试图找出是否可以创建关系索引但没有运气。

Query Execution Plan

  MATCH (u:User { SpFeaturedUser: true })<-[:CREATED_BY]-(fe)-[:FEED_TYPE]->(:ServiceType{Id: 13})
                                WITH fe
                                ORDER BY fe.UpdatedUTCDateTime DESC
                                SKIP 0
                                LIMIT 100
                                OPTIONAL MATCH (fe)-[:CREATED_BY]->(u:User)
                                OPTIONAL MATCH (fe)-[:FEED_TYPE]->(st:ServiceType)
                                OPTIONAL MATCH (fe)-[endrs:ENDORSED]->(p:Place)
                                OPTIONAL MATCH (p:Place)-[placeImage:RELATED_IMAGE]->(pImg:Image)
                                OPTIONAL MATCH(fe)< -[likes: LIKES] - ()
                                OPTIONAL MATCH(fe)< -[cmts: COMMENT_ON_FEED] - (c)
                                OPTIONAL MATCH (m:Mentions)-[:MENTIONED_IN]->(fe)
                                OPTIONAL MATCH (fe)-[:RELATED_IMAGE]->(img:Image)
                                OPTIONAL MATCH (fe)-[:RELATED_SERVICE_URL]->(su)
                                WITH {
                                    Description: fe.Description,
                                    DescriptionEncoded: fe.DescriptionEncoded,
                                    EndorsementType: fe.PostType,
                                    CreatedUTCDateTime: fe.CreatedDateTime,
                                    UpdatedUTCDateTime: fe.UpdatedDateTime,
                                    StartDate: fe.StartDate,
                                    EndDate: fe.EndDate,
                                    AccessScope: fe.AccessScope,
                                    Rating: fe.Rating,
                                    EndorsementId: fe.ServiceId,
                                    ServiceTypeId: st.Id,
                                    LikeCount: Count(distinct(likes)),
                                    IsLiked: EXISTS((fe) < -[:LIKES] - (: User{ UserId: "4F97D90E-922C-4C44-8F68-8311C60D76D9"}) ),
                                    CommentsCount: Count(distinct(cmts)),
                                    LastActivity: { 
                                        en: fe.Activity, 
                                        da: fe.DanishActivity
                                        },
                                    User: {
                                        UserId: u.UserId,
                                        FirstName: u.FirstName,
                                        FileName: u.FileName,
                                        SocialMediaAttribution: {

                                        }
                                    },
                                    Endorsement_Mentions: CASE WHEN m IS NOT NULL THEN Collect(distinct {
                                        Name: m.Name,
                                        Type: m.TagType,
                                        PlaceHolder: m.Placeholder,
                                        TagID: m.TagId
                                    }) ELSE [] END ,
                                    Endorsement_Image: CASE WHEN img IS NOT NULL THEN Collect(distinct {
                                        ImageUrl: img.ImageUrl,
                                        Width: img.Width,
                                        Height: img.Height,
                                        Extension: img.Extension,
                                        CreatedUTCDateTime: img.CreatedUTCDateTime
                                    }) ELSE [] END,
                                    URLPreviews: CASE WHEN su IS NOT NULL THEN Collect(distinct {
                                      Title           : su.UrlTitle         ,
                                      Description     : su.UrlDescription   ,
                                      ImageURL        : su.PreviewImage      ,
                                      URL             : su.Url           ,
                                      Width           : su.Width         ,
                                      Height          : su.Height        ,
                                      Extension       : su.Extension     ,
                                      IsVideoUrl      : su.IsVideoUrl    ,
                                      VideoStreamUrl  : su.VideoStreamUrl,
                                      VideoType       : su.VideoType       
                                    }) ELSE [] END,
                                    Object: {
                                        ObjectId: p.ObjectId,
                                        Name: p.ObjectName,
                                        IsFollowed: EXISTS((fe)-[:ENDORSED]->()<-[:FOLLOW]-({UserId : "4F97D90E-922C-4C44-8F68-8311C60D76D9"})),
                                        AverageRating: {
                                            Count: p.ObjectAvgRating
                                        },
                                        EndorsementCount: Count(distinct(endrs)),
                                        Object_Image: CASE WHEN pImg IS NOT NULL THEN
                                              Collect(distinct {
                                                ImageUrl: pImg.ImageUrl,
                                                Width: pImg.Width,
                                                Height: pImg.Height,
                                                Extension: pImg.Extension,
                                                CreatedUTCDateTime: pImg.CreatedUTCDateTime
                                              }) ELSE [] END,
                                        Category: {
                                            CategoryId: p.CategoryId 
                                        },
                                        Country: {
                                            ShowBarometer: false
                                        }
                                    }
                                } as Feed 
            RETURN DISTINCT(Feed)
            ORDER BY Feed.UpdatedUTCDateTime DESC

预期结果是,此查询应运行得足够快,以在其他查​​询已在运行的情况下在2-3秒内返回结果。

1 个答案:

答案 0 :(得分:0)

由于您的查询不需要OPTIONAL MATCH子句中的任何结果,因此删除它们可以加快查询速度并避免可能出现的重复结果。