mbleigh / acts-as-taggable-on

A tagging plugin for Rails applications that allows for custom tagging along dynamic contexts.
http://mbleigh.lighthouseapp.com/projects/10116-acts-as-taggable-on
MIT License
4.95k stars 1.18k forks source link

`:any => true` performance issue #1084

Closed aovertus closed 2 years ago

aovertus commented 2 years ago

Hi there,

I'm trying to use the gem to filter out a list of customer which could have any of the given tag. The built in query from the gem provide this query result:

MetaCustomer.tagged_with(preferred_brands, :on => :preferred_brands, any: true).to_sql

SELECT `meta_customers`.* FROM `meta_customers` WHERE EXISTS (
    SELECT * FROM `taggings` WHERE `taggings`.`taggable_id` = `meta_customers`.`id` AND 
                                   `taggings`.`taggable_type` = 'MetaCustomer' AND 
                                   `taggings`.`tag_id` IN (
                                        SELECT `tags`.`id` FROM `tags` WHERE (`tags`.`name` LIKE 'nike' ESCAPE '!' OR `tags`.`name` LIKE 'adidas' ESCAPE '!')) AND 
                                   `taggings`.`context` = 'preferred_brands' limit 1
)

This is really slow (~12 secs) has we have more than a million of customers. I'm not really familiar with EXISTS but running EXPLAIN showcase that no index is then used on the MetaCustomer where query


I did a small tweak so the subquery return ids instead to avoid using exist and rely on the primary key index. The result run in 78ms for the same result.

Is anyone having the same issue? Am I missing an index ?

aovertus commented 2 years ago

migration issue. Its working properly on proper setup, something messed up during installation for some system