Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.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
发送的行数:12行检查的行数:549024-如何优化mySQL查询?_Mysql_Performance_Optimization - Fatal编程技术网

发送的行数:12行检查的行数:549024-如何优化mySQL查询?

发送的行数:12行检查的行数:549024-如何优化mySQL查询?,mysql,performance,optimization,Mysql,Performance,Optimization,我有一个数据库,上面有相当好的服务器列表(四核Xeon 2.0Ghz、16GB RAM、SSD驱动器)。该数据库大约有180000个列表。服务器上还没有流量,我只是用大量的列表对其进行测试,以确保在以后有那么多实时列表和实际流量时不会出现问题 但即使没有交通,我觉得他们应该比实际返回的速度更快 从慢速查询日志中,我可以找到以下内容: # Query_time: 1.575742 Lock_time: 0.000113 Rows_sent: 12 Rows_examined: 549024

我有一个数据库,上面有相当好的服务器列表(四核Xeon 2.0Ghz、16GB RAM、SSD驱动器)。该数据库大约有180000个列表。服务器上还没有流量,我只是用大量的列表对其进行测试,以确保在以后有那么多实时列表和实际流量时不会出现问题

但即使没有交通,我觉得他们应该比实际返回的速度更快

从慢速查询日志中,我可以找到以下内容:

# Query_time: 1.575742  Lock_time: 0.000113 Rows_sent: 12  Rows_examined: 549024
有180000条记录,它只需要返回12条,但它检查了500000条记录,需要超过1.5秒?一定是出了什么事,对吧(

实际查询是:

SELECT a.listing_id, a.name, a.item_price, a.max, a.nb, a.currency,
     a.end_time, a.closed, a.bold, a.hl, a.buy_price, a.is_offer, a.reserve,
     a.owner_id, a.postage_amount, a.fb_current_bid, a.type, a.start_time,
     a.is_relisted_item, a.enable
     FROM db_listings a
     LEFT JOIN db_users u ON u.user_id=a.owner_id  WHERE a.active=1 AND
     a.approved=1 AND a.deleted=0 AND a.creation_in_progress=0 AND
     a.closed=0 AND (a.list_in='store' OR u.shop_active='1')
     GROUP BY a.listing_id
     ORDER BY a.list_in ASC, a.end_time ASC  LIMIT 0, 12;
已经在db_listings中的listing_id和db_users中的user_id上设置了索引。我不认为db_users加入是一个问题,因为现在只有2个用户

如果您需要任何其他信息来解决这个问题,请告诉我


非常感谢您的帮助:)

首先,您的查询有问题。使用LEFT JOIN,但使用where子句会变成隐式内部联接: 和(a.list_in='store'或u.shop_active='1')

为什么这会将左连接变成隐式内部连接?因为当没有匹配的用户时,左连接将为u.shop_active生成NULL值,但NULL永远不会等于“1”。这会将查询转换为内部联接,因为外部联接生成的任何行都将由WHERE条件过滤

此筛选器也是性能问题的原因。两个不同表中的列之间存在OR条件。没有一个索引可以满足这样的条件

这是另一种可能表现更好的方法。此版本将仅搜索(a.list_in!='store'和u.shop_active='1')中的列表(当列表_in='store'列表少于12个时)

要使用以下内容,请确保有索引(列表输入,结束时间)


非常感谢,我将尝试一下,并报告结果!:))
SELECT * FROM
(
    SELECT a.listing_id, a.name, a.item_price, a.max, a.nb, a.currency,
           a.end_time, a.closed, a.bold, a.hl, a.buy_price, a.is_offer, a.reserve,
           a.owner_id, a.postage_amount, a.fb_current_bid, a.type, a.start_time,
           a.is_relisted_item, a.enable
     FROM db_listings a
    WHERE list_in = 'store'
     a.active=1 AND
     a.approved=1 AND 
     a.deleted=0 AND 
     a.creation_in_progress=0 AND
     a.closed=0
    ORDER BY end_time 
    LIMIT 12 
    )
    UNION ALL
    (
        SELECT a.listing_id, a.name, a.item_price, a.max, a.nb, a.currency,
           a.end_time, a.closed, a.bold, a.hl, a.buy_price, a.is_offer, a.reserve,
           a.owner_id, a.postage_amount, a.fb_current_bid, a.type, a.start_time,
           a.is_relisted_item, a.enable
        FROM db_listings a
        JOIN users u 
          ON a.owner_id = u.user_id
         AND u.shop_active = '1'
       WHERE list_in != 'store' AND
       a.active=1 AND
       a.approved=1 AND 
       a.deleted=0 AND 
       a.creation_in_progress=0 AND
       a.closed=0
       ORDER BY end_time 
       LIMIT 12 
    )
) sq
ORDER BY list_in, end_time
LIMIT 12;