I need to attach unlimited attributes to a record in a table, and I’ve already designed a system using #3 below using mysql. Unfortunately I am finding that searching across a million records is getting slow. Is #2 a better approach, or is there a better way alltogether? Is this a case for using a view? I’d like to have my keys table separate so I know what attributes are being stored for each record.
1: simple:
table records: id, recordname, valname
select recordname from records where valname = 'myvalue'
2: a little more complex:
table records: id recordname
table keyvalues: id recordid keyname valname
select r.recordname
from records r
right join keyvalues kv on kv.recordid = r.id
and kv.keyname='mykey'
and kv.valname = 'myvalue'
3: most complex:
table records: id recordname
table keys: id keyname
table values: id recordid keyid valname
select r.recordname
from records r
right join keys k on k.keyname='mykey'
right join values v on v.recordid = r.id
and v.keyid = k.id
and v.valname = 'myvalue'
I would use inner joins. This will give a smaller result set and the one you want.
Did you try this query?
However, I think the real way to do it is to have 4 tables
Then (with the right indexes) you can have a query like this
This should be quite fast… all it has to do is find the id in values and keys and then do a scan on j. If you have the right indexes these will be quick.