mysql索引了1500万客户,但查询仍然需要5分钟才能返回结果

um6iljoc  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(295)

我有1500万客户数据,分为3列,每列都有索引:
客户 Index ix_disabled_customer_id on zen_customers(customers_id, disabled); 客户属性 Index ix_attribute_id_and_name on zen\u客户 (attribute_id, attribute_name); 客户属性值。 Index ix_attribute_id_and_customer_id onzen_customers(customers_id, attribute_id); 我正在尝试使用性别筛选客户,返回结果花费的时间太长。
下面是查询

SELECT tcav.customers_id AS customers_id 
FROM customer_attribute_value tcav
JOIN customer_attribute tca
JOIN customers zc
WHERE tcav.attribute_id = tca.attribute_id
    AND tca.attribute_name = "Gender"
    AND tcav.attribute_value = "M"
    AND zc.customers_id = tcav.customers_id
    AND  zc.disabled = 0;

为扩展计划添加的图像
如果我能想出优化过滤的办法,我将不胜感激。谢谢

dced5bon

dced5bon1#

eav模式存在许多问题。在这种情况下,您需要花费大量的空间和时间来查找“gender”,而只需在主表中查找“gender”就可以更有效地进行查找。
您的模式通过规范化这些值,而不是将它们放在属性表中,使情况变得非常糟糕。
跟随标签[entity attribute value]获得进一步的启示。
除非您认真地修改模式,否则随着数据的增长,性能将变得越来越差。

iqjalb3h

iqjalb3h2#

首先,建议使用on子句而不是where子句来连接表。它不太可能对性能有任何影响,但它确实有助于提高查看哪些列与哪些表相关的能力。

SELECT tcav.customers_id AS customers_id 
FROM tulip_customer_attribute_value tcav
JOIN tulip_customer_attribute tca
ON tcav.attribute_id = tca.attribute_id
JOIN zen_customers zc
ON zc.customers_id = tcav.customers_id
WHERE tca.attribute_name = "Gender"
AND tcav.attribute_value = "M"
AND zc.disabled = 0

添加以下索引:

tulip_customer_attribute (attribute_name,attribute_id)

tulip_customer_attribute_value (attribute_id,attribute_value,customers_id)

索引中列的顺序很重要。

相关问题