mysql eav_如何优化这个复杂的EAV MYSQL查询?

bd96500e110b49cbb3cd949968f18be7.png

Is it possible to optimize this query I have written

I've created a sort of dynamic virtual database to give my users the ability to add custom fields without affecting the database structure. Here is a very simplified view of the stucture so far.

tables | columns

db_cases | caseid

db_structure | fieldname

db_data | fieldname, data, caseid

db_names | nameid

We can create a new field by adding a row to db_structure

Any data we wish to record is recorded to db_data.

Names are stored in db_names and the name_id is stored in db_data

I am trying to output the cases to a html table

Hopefully the rest is self explanatory, you can see how unefficient it is. Can I do the same thing via joins?

SELECT

case_id,

(SELECT data_field_value

FROM db_data

WHERE data_case_id = case_id AND data_field_name = 'casestatus'

) AS casestatus,

(SELECT forename_company

FROM db_names

WHERE name_id = (SELECT data_field_value

FROM db_data

WHERE data_case_id = case_id AND data_field_name = 'client1'

)

) AS client1_forename_company

FROM db_cases

Thanks

解决方案

Actually, Chibuzo is right. Start by deleting it :-)) But before, play with it a little, it's a good brain excercise, like chess or something :-)

select

case_id,

d_status.data_field_value as case_status,

d_client1_name.forename_company as client1_forename_company

from db_cases

join db_data as d_status

on d_status.data_case_id = case_id

AND d_status.data_field_name = 'casestatus'

join db_data as d_client1

on d_client1.data_case_id = case_id

AND d_client1.data_field_name = 'client1'

join db_names as d_client1_name

on d_client1_name.name_id = d_client1.data_field_value

I would expect these direct joins without subqueries to be much more efficient, though you'll have to test it - there are often surprises in optimizations.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值