I have the following tables in my database which I cannot change or modify. I kept the Log table simple but LogDetail is the same as in my database.
Log Table
Id User Department Service Method
21 John Sales UserService GetUser
LogDetail Table
Id LogRef ParamName ParamValue
30 21 FirstName Adam
31 21 LastName Smith
32 21 Age 35
33 21 Gender M
Now, I am using following query to get who searched (Adam, Smith, 35, M)
SELECT
L.*, D1.ParamName, D2.ParamName, D3.ParamName, D4.ParamName
FROM Log as L
INNER JOIN LogDetail as D1 on L.Id = D1.LogRef
INNER JOIN LogDetail as D2 on L.Id = D2.LogRef
INNER JOIN LogDetail as D3 on L.Id = D3.LogRef
INNER JOIN LogDetail as D4 on L.Id = D4.LogRef
WHERE
D1.ParamName='FirstName' and D1.ParamValue='Adam' and
D2.ParamName='LastName' and D2.ParamValue='Smith' and
D3.ParamName='Age' and D3.ParamValue=35 and
D4.ParamName='Gender' and D4.ParamValue='M'
Is there a better way to do this?
This is what happens when you use an EAV schema (key-value pairs basically)
There isn’t much you can except add an index on
(ParamName, ParamValue)forLogDetail. This assumes the clustered index stays asLogRef