Use more efficient queries.
authorRoger Braun <roger@rogerbraun.net>
Tue, 20 Jun 2017 14:18:42 +0000 (16:18 +0200)
committerRoger Braun <roger@rogerbraun.net>
Tue, 20 Jun 2017 14:18:42 +0000 (16:18 +0200)
lib/pleroma/activity.ex
priv/repo/migrations/20170620133028_add_object_activity_index.exs [new file with mode: 0644]

index f48199cae876b8441af760973b5868b1df7d7924..40e14005b07d4d50ed64ea112c041924058af4a4 100644 (file)
@@ -17,11 +17,12 @@ defmodule Pleroma.Activity do
 
   def all_by_object_ap_id(ap_id) do
     Repo.all(from activity in Activity,
-      where: fragment("? @> ?", activity.data, ^%{object: %{id: ap_id}}))
+      where: fragment("(?)->'object'->>'id' = ?", activity.data, ^to_string(ap_id)))
   end
 
   def get_create_activity_by_object_ap_id(ap_id) do
     Repo.one(from activity in Activity,
-      where: fragment("? @> ?", activity.data, ^%{type: "Create", object: %{id: ap_id}}))
+      where: fragment("(?)->'object'->>'id' = ?", activity.data, ^to_string(ap_id))
+             and fragment("(?)->>'type' = 'Create'", activity.data))
   end
 end
diff --git a/priv/repo/migrations/20170620133028_add_object_activity_index.exs b/priv/repo/migrations/20170620133028_add_object_activity_index.exs
new file mode 100644 (file)
index 0000000..1abbd78
--- /dev/null
@@ -0,0 +1,7 @@
+defmodule Pleroma.Repo.Migrations.AddObjectActivityIndex do
+  use Ecto.Migration
+
+  def change do
+    create index(:objects, ["(data->'object'->>'id')", "(data->>'type')"], name: :activities_create_objects_index)
+  end
+end