Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
即使使用索引,Mysql查询仍然很慢_Mysql_Query Optimization - Fatal编程技术网

即使使用索引,Mysql查询仍然很慢

即使使用索引,Mysql查询仍然很慢,mysql,query-optimization,Mysql,Query Optimization,我已经尝试优化mysql查询,我使用它根据地址从数据库中获取客户。 这个问题快把我逼疯了,所以我非常感谢你的帮助:) 我有两个与此查询相关的表:customers\u address和systems\u address。 我只想得到有地址的客户,我可以为这个系统显示 例如: 我想从customers_address表中获取客户,该表具有属于system_id 2的address_id 使用in子句的最佳查询: select distinct customer_id from customers_

我已经尝试优化mysql查询,我使用它根据地址从数据库中获取客户。 这个问题快把我逼疯了,所以我非常感谢你的帮助:)

我有两个与此查询相关的表:customers\u address和systems\u address。 我只想得到有地址的客户,我可以为这个系统显示

例如:

我想从customers_address表中获取客户,该表具有属于system_id 2的address_id

使用in子句的最佳查询:

select distinct customer_id from customers_address use index(address_id_customer_id) where address_id in (select distinct address_id from systems_address where system_id = 2) and address_id !=-1\G;
问题是子查询只返回一行(值2),如果我使用子查询的值运行整个查询,它会非常快:

select customer_id from customers_address use index(address_id_customer_id) where address_id !=-1 and address_id in (2)\G;
时间从10秒以上下降到0.00秒

我也尝试过使用联接进行查询,但与将value替换为in子句时的查询相比,它的性能仍然很慢(超过7秒)。 下面是使用联接的相同查询:

select distinct customer_id from customers_address use index(address_id_customer_id) inner join systems_address where systems_address.address_id = customers_address.address_id and system_id = 2 and customer_id != -1\G
我已向客户发送了地址816000行和系统发送了地址400000行。 以下是这些表的模式(简化了的表使问题更容易找到):

有没有办法让查询更快

下面是我运行Bohemian的查询(在创建了这些新索引和更新之后)时解释的结果

id:1
选择类型:简单
表:系统地址
类型:ref
可能的密钥:地址id、系统id、地址id、系统id、idx1、, 关键字:idx1 重点:5 参考:常数 行数:1999年 额外:在何处使用;使用临时

id:2
选择类型:简单
表:客户地址
类型:ref
可能的密钥:客户id、地址id、地址id、客户id、idx2
关键字:地址\标识\客户\标识
键组:5
参考:数据库系统地址地址id
行数:45375

额外:在何处使用;使用索引

颠倒表的顺序并使用联接条件,其中包括额外条件:

select distinct customer_id
from systems_address
join customers_address on systems_address.address_id = customers_address.address_id
    and customer_id != -1
where system_id = 2
这应该表现得很好,使用索引并最小化访问的行数

确保已定义以下索引:

create index idx1 on systems_address(system_id);
create index idx2 on customers_address(address_id);
为了确保这一点,还需要更新统计信息:

analyze systems_address, customers_address;

在子查询中删除
DISTINCT
,应该可以加快速度。另外,将连接条件放在
ON
子句中而不是
WHERE
子句中应该可以改进您的连接。如果您仍然有问题,您可以发布
EXPLAIN
输出吗?@user2735698。如果这解决了问题,那么您应该接受答案。感谢Bohemian给出的答案,但性能仍然保持不变,~7秒。请参阅编辑-现在尝试定义索引和更新统计信息。感谢您effors Bohemian和Joachim Isakson,但我运行的查询仍然需要6秒钟…我在问题中添加了“为波西米亚人解释”查询
analyze systems_address, customers_address;