How to filter items by multiple attribures using eav model?

159 Views Asked by At

I am building online catalog using eav antipatern. I know this is bad pattern, but i need to be able to add properties at runtime, so i choosed this.Also i cant use solr, because i use hotsted plateform. I have following tables: Table values:

id| property_id | value
1   1             rtx3060
2   2             intel i5
3   2             intel i7

Table property:

id| name
1   gpu
2   cpu

Table item_value:

item_id | value_id
1         1
1         2
2         1
2         3

Table **item**:
id | name
1    laptop-1
2    laptop-2

I want to filter items in my catalog by multiple parametrs, like laptop which has both processor intel i5 and gpu rtx 3060. How can i do this with sql?

One brute force way to do this is following: select item_id from item_value where value_id = (select id from value where value = 'intel i5') and item_id in (select item_id from item_value where value_id = (select id from value where value = 'rtx3060')); This query finds intersection of all ids from item_value table. It can be expanded by and item_id in (select item_id from item_value where value_id = (select id from value where value = 'some') to filter by more values. But is there any faster approach?

0

There are 0 best solutions below