以下は私が使用しているgemです
gem 'neo4j', github: 'andreasronge/neo4j
以下は、モデルのユーザーと友人です。
ユーザー.rb
class User
include Neo4j::ActiveNode
property :name, :type => String, :index => :exact
property :mood, :type => String
property :status, :type => String
property :dob, :type => DateTime, :index => :exact
property :uid, :type => String, :index => :exact, :unique => true
property :username, :type => String
property :email, :type => String, :index => :exact
property :gender, :type => String, :index => :exact
property :remember_token, :type => String, :index => :exact
property :fb_access_token, :type => String, :index => :exact
scope :gender_filter, ->(g){ where(gender: g)}
property :friends_list
serialize :friends_list
#before_save :create_remember_token
validates :email, :uniqueness => true
has_many :both, :friends, model_class: User, rel_class: Friend
has_many :both, :friend_girls, model_class: User, rel_class: Friend_girl
has_many :both, :friend_boys, model_class: User, rel_class: Friend_boy
has_many :both, :places, model_class: Location, rel_class: Place
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
フレンド.rb
class Friend
include Neo4j::ActiveRel
property :provider, :type => String
from_class User
to_class User
type 'friends'
end
以下は、クエリ実行のコンソール ログです。
CYPHER 1022ms MATCH (n:`User`) WHERE n.remember_token = {n_remember_token} RETURN n LIMIT 1
User#friend_boys 1077ms MATCH (user0:`User`), (result:`User`), user0-[rel0:`friend_boys` {_classname: "Friend_boy"}]-(result:`User`) WHERE ID(user0) = 0 RETURN resu
lt LIMIT 8
Rendered users/_list.html.erb (7425.9ms)
Rendered users/friends.html.erb within layouts/application (7456.0ms)
Rendered layouts/_shim.html.erb (1.0ms)
User#places 1063ms MATCH (user0:`User`), (result:`Location`), user0-[rel0:`places` {_classname: "Place"}]-(result:`Location`) WHERE ID(user0) = 0 RETURN result
Rendered layouts/_header.html.erb (25855.1ms)
Rendered layouts/_sidebar.html.erb (3.0ms)
Completed 200 OK in 36067ms (Views: 33981.5ms)
ただし、同じ個々のクエリは、Neo4j 管理コンソールで約 350 ミリ秒かかります。
neo4j-commnunity エディション '2.1.3' を使用しています
これは他のデータベースに比べて遅すぎるため、パフォーマンスを助けてください
更新 1:
示唆されたように、データ ディレクトリを消去して、neo4j-community (2.1.3) を再インストールしましたが、何も変わりませんでした。パフォーマンスはまだ遅かったので、新しいバージョンの neo4j-community (2.1.5) をインストールして、既存のデータ ディレクトリを維持することにしました。 -場所、再び同じ結果。次に、データ ディレクトリを削除し、neo4j-community (2.1.5) を再度インストールしました。今回は、以前のセットアップでのすべてのクエリで 1000 ミリ秒を超えていたのに対して、Rails アプリの一部のクエリでパフォーマンスが向上しました。
以下はログです。
インスタンス 1:
CYPHER 2025ms MATCH (n:`User`) WHERE n.remember_token = {n_remember_token} RETURN n LIMIT 1 | {:n_remember_token=>"dZGEOm0aKxDYkIQ5PfGjnw"}
CYPHER 16ms MATCH (result:`User`) WHERE result.email = {result_email} AND NOT ID(result) = {record_neo_id} RETURN COUNT(result) AS count | {:record_neo_id=>171, :result_email=>"achal.rvce@gmail.com"}
インスタンス 2:
CYPHER 15ms MATCH (n:`User`) WHERE n.remember_token = {n_remember_token} RETURN n LIMIT 1 | {:n_remember_token=>"dZGEOm0aKxDYkIQ5PfGjnw"}
User#friend_girls 25ms MATCH (user171:`User`), (result:`User`), user171-[rel0:`friend_girls`]-(result:`User`) WHERE ID(user171) = {ID_user171_} RETURN result LIMIT 8 | {:ID_user171_=>171}
インスタンス 1:
2 番目のクエリを見るとわかるように、前回のセットアップ試行では常に 1000 ミリ秒以上かかっていたのに比べて、わずか 16 ミリ秒しかかかりませんでした。
しかし、最初のクエリでは、パフォーマンスが遅すぎて 2 秒以上かかります。
インスタンス 2:
2 秒以上かかった同じクエリが 15 ミリ秒かかりました (おそらくクエリがキャッシュされています)。ただし、これは一貫性がありません。同じクエリを連続して実行すると、2 秒以上かかる場合もあれば、20 ミリ秒未満の場合もあります。
インスタンス 3:
CYPHER 1033ms MATCH (n:`Location`) WHERE n.address = {n_address} RETURN n LIMIT 1 | {:n_address=>"Vijayanagar, Karnataka, India"}
CYPHER 14ms MATCH (n:`User`) WHERE n.remember_token = {n_remember_token} RETURN n LIMIT 1 | {:n_remember_token=>"dZGEOm0aKxDYkIQ5PfGjnw"}
CYPHER 90ms START start = node(171), end = node(173) CREATE start-[rel0:`places` {_classname: "Place"}]->end
CYPHER 16ms MATCH (result:`User`) WHERE result.email = {result_email} AND NOT ID(result) = {record_neo_id} RETURN COUNT(result) AS count | {:record_neo_id=>171, :result_email=>"achal.rvce@gmail.com"}
User#places 59ms MATCH (user171:`User`), (result:`Location`), user171-[rel0:`places`]-(result:`Location`) WHERE ID(user171) = {ID_user171_} RETURN count(result) AS count | {:ID_user171_=>171}
User#places 14ms MATCH (user171:`User`), (result:`Location`), user171-[rel0:`places`]-(result:`Location`) WHERE ID(user171) = {ID_user171_} RETURN result | {:ID_user171_=>171}
インスタンス 3 では、最初のクエリだけが遅いことがわかります。他のすべてのクエリは高速です。
更新 2:
以下はその後のログですbundle update
Started GET "/" for 127.0.0.1 at 2014-10-28 08:14:26 +0530
Processing by StaticPagesController#home as HTML
CYPHER 1030ms MATCH (n:`User`) WHERE n.remember_token = {n_remember_token} RETURN n LIMIT 1 | {:n_remember_token=>"xqbwLZFzaULDZUKihEql5Q"}
Rendered users/_default_pic.html.erb (3.0ms)
Rendered users/_pics_grid.html.erb (4.0ms)
User#places 5ms MATCH (user184:`User`), (result:`Location`), user184-[rel0:`places`]-(result:`Location`) WHERE ID(user184) = {ID_user184_} RETURN count(result) AS c
ount | {:ID_user184_=>184}
Rendered users/_places.html.erb (17.0ms)
Rendered users/_my_badges.html.erb (1.0ms)
Rendered users/_profile_pics.html.erb (0.0ms)
Rendered users/_show.html.erb (200.1ms)
Rendered static_pages/home.html.erb within layouts/application (204.2ms)
Rendered layouts/_shim.html.erb (0.0ms)
User#places 0ms MATCH (user184:`User`), (result:`Location`), user184-[rel0:`places`]-(result:`Location`) WHERE ID(user184) = {ID_user184_} RETURN result | {:ID_user
184_=>184}
Rendered layouts/_header.html.erb (499.6ms)
Rendered layouts/_sidebar.html.erb (3.0ms)
Completed 200 OK in 2100ms (Views: 1064.7ms)
Started GET "/" for 127.0.0.1 at 2014-10-28 08:14:35 +0530
Processing by StaticPagesController#home as HTML
CYPHER 2036ms MATCH (n:`User`) WHERE n.remember_token = {n_remember_token} RETURN n LIMIT 1 | {:n_remember_token=>"xqbwLZFzaULDZUKihEql5Q"}
Rendered users/_default_pic.html.erb (2.0ms)
Rendered users/_pics_grid.html.erb (0.0ms)
User#places 5ms MATCH (user184:`User`), (result:`Location`), user184-[rel0:`places`]-(result:`Location`) WHERE ID(user184) = {ID_user184_} RETURN count(result) AS c
ount | {:ID_user184_=>184}
Rendered users/_places.html.erb (14.0ms)
Rendered users/_my_badges.html.erb (0.0ms)
Rendered users/_profile_pics.html.erb (17.9ms)
Rendered users/_show.html.erb (221.0ms)
Rendered static_pages/home.html.erb within layouts/application (226.1ms)
Rendered layouts/_shim.html.erb (0.0ms)
User#places 5ms MATCH (user184:`User`), (result:`Location`), user184-[rel0:`places`]-(result:`Location`) WHERE ID(user184) = {ID_user184_} RETURN result | {:ID_user
184_=>184}
Rendered layouts/_header.html.erb (474.4ms)
Rendered layouts/_sidebar.html.erb (1.0ms)
Completed 200 OK in 3094ms (Views: 1053.2ms)
Started GET "/assets/xeditable/img/loading.gif" for 127.0.0.1 at 2014-10-28 08:15:57 +0530
Started POST "/users/de30de56-7898-4d6b-8be4-c18a0c4915d6/update_status" for 127.0.0.1 at 2014-10-28 08:16:15 +0530
Processing by UsersController#update_status as JSON
Parameters: {"name"=>"xe_status", "value"=>"i am doing good", "pk"=>"1", "id"=>"de30de56-7898-4d6b-8be4-c18a0c4915d6"}
CYPHER 1016ms MATCH (n:`User`) WHERE n.remember_token = {n_remember_token} RETURN n LIMIT 1 | {:n_remember_token=>"xqbwLZFzaULDZUKihEql5Q"}
CYPHER 26ms MATCH (result:`User`) WHERE result.email = {result_email} AND NOT ID(result) = {record_neo_id} RETURN COUNT(result) AS count | {:record_neo_id=>184, :re
sult_email=>"achal.rvce@gmail.com"}
Completed 200 OK in 1305ms (Views: 1.0ms)
Gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.2'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
gem "will_paginate"
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data'
gem 'omniauth-facebook'
gem "geocoder"
gem 'rails4-autocomplete'
gem "font-awesome-rails"
gem 'koala'
gem 'newrelic_rpm'
gem 'jquery-turbolinks'
group :production do
gem 'rails_12factor'
end
gem 'jquery-ui-rails'
gem "string-urlize" # for making the title the primary key and work as an URL, see Neo4j.rb id_property
platforms :jruby do
gem 'neo4j-community', '~> 2.0.0'
end
gem 'sidekiq'
gem 'sinatra', require: false
gem 'slim'
gem 'neo4j'
gem 'cloudinary'
group :development do
gem 'spring'
gem 'os'
gem 'better_errors'
gem 'binding_of_caller'
gem 'meta_request'
gem 'debugger'
gem 'foreman'
end
Ruby version is 2.0.0 and Rails version is 4.0.2