# Stuck With Bad Category Filter Performance

**URL:** <https://forums.percona.com/t/stuck-with-bad-category-filter-performance/890>\
**Category:** Other MySQL® Questions\
**Created:** [August 16, 2008, 6:52am UTC](https://forums.percona.com/t/stuck-with-bad-category-filter-performance/890 "2008-08-16T06:52:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![w00t08](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@w00t08](https://forums.percona.com/u/w00t08)\
**Post date:** [August 16, 2008, 6:52am UTC](https://forums.percona.com/t/stuck-with-bad-category-filter-performance/890/1 "2008-08-16T06:52:35Z")

</div>

Hey folks,

I’m a little stuck. I got the following tables:

CREATE TABLE `items` ( `id` int(10) unsigned NOT NULL auto\_increment, `unlocked` tinyint(1) NOT NULL default ‘0’, `name` varchar(200) NOT NULL, `meta_cat_type` enum(‘blah’,‘foo’,‘bar’) default NULL, `post_time` int(10) unsigned NOT NULL default ‘0’, `unlock_time` int(10) unsigned NOT NULL, `allow_comments` tinyint(1) NOT NULL default ‘1’, `posted_by` int(10) unsigned NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`), KEY `post_time` (`post_time`), KEY `meta_cat_type` (`meta_cat_type`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- --------------------------------------------------------CREATE TABLE `item_cats` ( `item_id` int(10) unsigned NOT NULL, `cat_id` smallint(5) unsigned NOT NULL, KEY `item_id` (`item_id`), KEY `cat_id` (`cat_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- --------------------------------------------------------CREATE TABLE `item_filters` ( `user_id` int(10) unsigned NOT NULL, `cat_id` smallint(5) unsigned NOT NULL, `meta_cats` set(‘blah’,‘foo’,‘bar’) NOT NULL, `filter_type` enum(‘overview’,‘global’) NOT NULL, UNIQUE KEY `id` (`user_id`,`cat_id`,`filter_type`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;

So each item can belong to many categories. Pretty much standard stuff.

Now I have a typical view-items-with-pagination-and-stuff thing set up, so I need to get the total number of items and the items on the current page. My current query to get these looks like this, but it’s rather slow as the sub query has to be executed once for every row in the items table (which currently has about 50,000 rows and is growing continuously).

SELECT COUNT( \* )FROM itemsWHERE unlocked =1AND ( NOT EXISTS ( SELECT \* FROM item\_filters f, item\_cats c WHERE f.user\_id = “823” AND f.cat\_id = c.cat\_id AND c.item\_id = items.id AND f.filter\_type = “overview” AND FIND\_IN\_SET( items.meta\_cat\_type, meta\_cats ))

Any help on how to speed this up is greatly appreciated! Thanks in advance. )

---

<div class="post-metadata">

**Author:** ![Speeple](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Speeple](https://forums.percona.com/u/Speeple)\
**Post date:** [August 17, 2008, 9:00am UTC](https://forums.percona.com/t/stuck-with-bad-category-filter-performance/890/2 "2008-08-17T09:00:29Z")

</div>

Just a quick observation is that you have no primary key for item\_cats.

Try creating a PK:

PRIMARY KEY (`item_id`, `cat_id`)

Possible storage reductions:

INT + SMALLINT = 6 bytes

INDEX(`cat_id`) (for tag clouds I assume?): 2 bytes

Total: 6 + 2 = 8

Currently:

6 bytes (InnoDB auto PK) + 4 bytes (INT) + 2 bytes (SMALLINT) = 12 bytes

Lookups on a “clustered” primary key are also more efficient.

Other than that, I don’t know how you can really improve the performance of the query without making some design changes.

---

<div class="post-metadata">

**Author:** ![w00t08](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@w00t08](https://forums.percona.com/u/w00t08)\
**Post date:** [August 17, 2008, 9:43am UTC](https://forums.percona.com/t/stuck-with-bad-category-filter-performance/890/3 "2008-08-17T09:43:49Z")

</div>

Hey, thanks for PK hints, that made sense and eliminated a “using where” in the EXPLAIN output. )

And other than that…? I’m not averse to table/database design changes as long as they’ll deliver better performance. Unfortunately, I’m a pretty clueless as to how these could look like. )
