acts-as-taggable-on icon indicating copy to clipboard operation
acts-as-taggable-on copied to clipboard

Am I doing too much queries?

Open davideluque opened this issue 6 years ago • 2 comments

I am trying to add the following tag list

@extracted_tags = ["cool", "podcast", "languages", "baby", "kissme"]

To a resource called "Podcast".

This is how my code looks like.

@podcast_tags = @podcast.all_tags_list
tags_to_add = @extracted_tags - @podcast_tags
@podcast.tag_list.add(tags_to_add) if tags_to_add.any?
@podcast.save if tags_to_add.any? || @remove.any?

I get first the tags that the resource has to see if there are tags that I need to remove or to add. For two reasons:

  • I don't want to do queries to the database if the tag list is the same
  • I have a method to update the hashtags, which basically adds new tags and remove the olds.

I'm getting all these queries to the database and it seems strange to me.

↳ app/controllers/api/v1/podcasts_controller.rb:15
  ActsAsTaggableOn::Tagging Load (9.1ms)  SELECT "taggings".* FROM "taggings" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ?  [["taggable_id", 42], ["taggable_type", "Podcast"]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:15
   (109.8ms)  commit transaction
  ↳ app/controllers/api/v1/podcasts_controller.rb:15
   (0.1ms)  begin transaction
  ↳ app/controllers/api/v1/podcasts_controller.rb:16
   (0.1ms)  commit transaction
  ↳ app/controllers/api/v1/podcasts_controller.rb:16
  ActsAsTaggableOn::Tag Load (0.4ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND (taggings.context = 'tags') GROUP BY tags.id  [["taggable_id", 42], ["taggable_type", "Podcast"]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:61
  ActsAsTaggableOn::Tag Load (0.3ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)  [["taggable_id", 42], ["taggable_type", "Podcast"]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:66
   (0.1ms)  begin transaction
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tag Load (0.4ms)  SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('cool') OR LOWER(name) = LOWER('podcast') OR LOWER(name) = LOWER('languages') OR LOWER(name) = LOWER('baby') OR LOWER(name) = LOWER('kissme'))
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tag Exists (0.2ms)  SELECT  1 AS one FROM "tags" WHERE "tags"."name" = ? LIMIT ?  [["name", "cool"], ["LIMIT", 1]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tag Create (2.4ms)  INSERT INTO "tags" ("name") VALUES (?)  [["name", "cool"]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tag Load (0.3ms)  SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('cool') OR LOWER(name) = LOWER('podcast') OR LOWER(name) = LOWER('languages') OR LOWER(name) = LOWER('baby') OR LOWER(name) = LOWER('kissme'))
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  CACHE ActsAsTaggableOn::Tag Load (0.0ms)  SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('cool') OR LOWER(name) = LOWER('podcast') OR LOWER(name) = LOWER('languages') OR LOWER(name) = LOWER('baby') OR LOWER(name) = LOWER('kissme'))
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  CACHE ActsAsTaggableOn::Tag Load (0.0ms)  SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('cool') OR LOWER(name) = LOWER('podcast') OR LOWER(name) = LOWER('languages') OR LOWER(name) = LOWER('baby') OR LOWER(name) = LOWER('kissme'))
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  CACHE ActsAsTaggableOn::Tag Load (0.1ms)  SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('cool') OR LOWER(name) = LOWER('podcast') OR LOWER(name) = LOWER('languages') OR LOWER(name) = LOWER('baby') OR LOWER(name) = LOWER('kissme'))
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tag Load (0.3ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)  [["taggable_id", 42], ["taggable_type", "Podcast"]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tag Load (0.8ms)  SELECT  "tags".* FROM "tags" WHERE "tags"."id" = ? LIMIT ?  [["id", 17], ["LIMIT", 1]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tagging Exists (0.3ms)  SELECT  1 AS one FROM "taggings" WHERE "taggings"."tag_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."taggable_id" = ? AND "taggings"."context" = ? AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL LIMIT ?  [["tag_id", 17], ["taggable_type", "Podcast"], ["taggable_id", 42], ["context", "tags"], ["LIMIT", 1]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tagging Create (0.5ms)  INSERT INTO "taggings" ("tag_id", "taggable_type", "taggable_id", "context", "created_at") VALUES (?, ?, ?, ?, ?)  [["tag_id", 17], ["taggable_type", "Podcast"], ["taggable_id", 42], ["context", "tags"], ["created_at", "2019-06-14 11:50:03.100157"]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tag Update All (0.3ms)  UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) + 1 WHERE "tags"."id" = ?  [["id", 17]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tag Load (0.2ms)  SELECT  "tags".* FROM "tags" WHERE "tags"."id" = ? LIMIT ?  [["id", 13], ["LIMIT", 1]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tagging Exists (0.2ms)  SELECT  1 AS one FROM "taggings" WHERE "taggings"."tag_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."taggable_id" = ? AND "taggings"."context" = ? AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL LIMIT ?  [["tag_id", 13], ["taggable_type", "Podcast"], ["taggable_id", 42], ["context", "tags"], ["LIMIT", 1]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tagging Create (0.2ms)  INSERT INTO "taggings" ("tag_id", "taggable_type", "taggable_id", "context", "created_at") VALUES (?, ?, ?, ?, ?)  [["tag_id", 13], ["taggable_type", "Podcast"], ["taggable_id", 42], ["context", "tags"], ["created_at", "2019-06-14 11:50:03.108946"]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tag Update All (0.2ms)  UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) + 1 WHERE "tags"."id" = ?  [["id", 13]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tag Load (0.1ms)  SELECT  "tags".* FROM "tags" WHERE "tags"."id" = ? LIMIT ?  [["id", 14], ["LIMIT", 1]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tagging Exists (0.2ms)  SELECT  1 AS one FROM "taggings" WHERE "taggings"."tag_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."taggable_id" = ? AND "taggings"."context" = ? AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL LIMIT ?  [["tag_id", 14], ["taggable_type", "Podcast"], ["taggable_id", 42], ["context", "tags"], ["LIMIT", 1]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tagging Create (0.2ms)  INSERT INTO "taggings" ("tag_id", "taggable_type", "taggable_id", "context", "created_at") VALUES (?, ?, ?, ?, ?)  [["tag_id", 14], ["taggable_type", "Podcast"], ["taggable_id", 42], ["context", "tags"], ["created_at", "2019-06-14 11:50:03.114768"]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tag Update All (0.3ms)  UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) + 1 WHERE "tags"."id" = ?  [["id", 14]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tag Load (0.1ms)  SELECT  "tags".* FROM "tags" WHERE "tags"."id" = ? LIMIT ?  [["id", 10], ["LIMIT", 1]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tagging Exists (0.4ms)  SELECT  1 AS one FROM "taggings" WHERE "taggings"."tag_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."taggable_id" = ? AND "taggings"."context" = ? AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL LIMIT ?  [["tag_id", 10], ["taggable_type", "Podcast"], ["taggable_id", 42], ["context", "tags"], ["LIMIT", 1]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tagging Create (0.3ms)  INSERT INTO "taggings" ("tag_id", "taggable_type", "taggable_id", "context", "created_at") VALUES (?, ?, ?, ?, ?)  [["tag_id", 10], ["taggable_type", "Podcast"], ["taggable_id", 42], ["context", "tags"], ["created_at", "2019-06-14 11:50:03.123215"]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tag Update All (0.3ms)  UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) + 1 WHERE "tags"."id" = ?  [["id", 10]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tag Load (0.1ms)  SELECT  "tags".* FROM "tags" WHERE "tags"."id" = ? LIMIT ?  [["id", 15], ["LIMIT", 1]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tagging Exists (0.2ms)  SELECT  1 AS one FROM "taggings" WHERE "taggings"."tag_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."taggable_id" = ? AND "taggings"."context" = ? AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL LIMIT ?  [["tag_id", 15], ["taggable_type", "Podcast"], ["taggable_id", 42], ["context", "tags"], ["LIMIT", 1]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tagging Create (0.2ms)  INSERT INTO "taggings" ("tag_id", "taggable_type", "taggable_id", "context", "created_at") VALUES (?, ?, ?, ?, ?)  [["tag_id", 15], ["taggable_type", "Podcast"], ["taggable_id", 42], ["context", "tags"], ["created_at", "2019-06-14 11:50:03.131247"]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
  ActsAsTaggableOn::Tag Update All (0.2ms)  UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) + 1 WHERE "tags"."id" = ?  [["id", 15]]
  ↳ app/controllers/api/v1/podcasts_controller.rb:67
   (136.4ms)  commit transaction

Any help? Thanks!

davideluque avatar Jun 14 '19 12:06 davideluque

I see nobody wanted to check this. I had a similar issue, in the end I gave up on using acts_as_taggable_on for that project. I was adding 10 tags at a time and getting close to 100 queries. But would still like to know if anyone has a fix for this

Cremz avatar Sep 08 '20 09:09 Cremz

You can check out this repo https://github.com/xumi/ActsAsTaggableOnActiveRecordRelation , it might help you

bogdanRada avatar Sep 08 '20 14:09 bogdanRada