同一张桌子上有多个内部联接

时间:2019-09-24 02:41:14

标签: mysql

我试图在同一个表上进行多个联接,并且在添加许多MYSQL后会失去连接。我知道我对这段代码做了太多的工作,并且阻塞了数据库。这是我正在使用的代码。我该如何简化并提高效率?

SELECT 
  y.`value` AS `Year`,
  m.`value` AS `Make`,
  mo.`value` AS `Model`,
  t.`value` AS `Title`,
  se.`value` AS `Seller Email`,
  v.`value` AS `VIN`,
  p.`value` AS `Posted`,
  e.`value` AS `Expired`,
  d.`value` AS `Expire Date`
FROM
  (SELECT `record`, `value` FROM thrn1_facileforms_subrecords WHERE `name`='vehicleYear') AS y
  INNER JOIN
  (SELECT `record`, `value` FROM thrn1_facileforms_subrecords WHERE `name`='sellerEmail') AS se
  USING (`record`)
  INNER JOIN
(SELECT `record`, `value` FROM thrn1_facileforms_subrecords WHERE `name`='vehicleVin') AS v
  USING (`record`)
  INNER JOIN
  (SELECT `record`, `value` FROM thrn1_facileforms_subrecords WHERE `name`='yearMakeModel') AS t
  USING (`record`)
  INNER JOIN
  (SELECT `record`, `value` FROM thrn1_facileforms_subrecords WHERE `name`='vehicleadExpired') AS e
  USING (`record`)
  INNER JOIN
  (SELECT `record`, `value` FROM thrn1_facileforms_subrecords WHERE `name`='vehicleModel') AS mo
  USING (`record`)
  INNER JOIN
  (SELECT `record`, `value` FROM thrn1_facileforms_subrecords WHERE `name`='vehicleMake') AS m
  USING (`record`)
  INNER JOIN
  (SELECT `record`, `value` FROM thrn1_facileforms_subrecords WHERE `name`='expireDate') AS d
  USING (`record`)
  INNER JOIN
  (SELECT `record`, `value` FROM thrn1_facileforms_subrecords WHERE `name`='datePosted') AS p
  USING (`record`)```

1 个答案:

答案 0 :(得分:0)

连接丢失很可能是因为客户端在等待结果太长时间后(客户端超时)正在关闭连接。您可以增加此值,并最终得到答案。

这里真正的问题是使用“实体值属性”反模式。您正在使用不同的行来存储不同类型的数据,因此必须运行此巨型联接才能获得所需的数据。相反,您应该尝试将给定记录的所有数据放在一行中,并根据需要添加许多列。

我认为EVA模式的唯一原因是当应用程序允许用户定义“自定义字段”时。是的,太多的此类字段将生成效率极低的查询。