Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k6hoR-0000JQ-QA for pgsql-performance@arkaria.postgresql.org; Fri, 14 Aug 2020 22:04:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k6hoP-000412-Lz for pgsql-performance@arkaria.postgresql.org; Fri, 14 Aug 2020 22:04:33 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k6hoP-00040v-A0 for pgsql-performance@lists.postgresql.org; Fri, 14 Aug 2020 22:04:33 +0000 Received: from mail-il1-x12e.google.com ([2607:f8b0:4864:20::12e]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1k6hoK-0006dq-8R for pgsql-performance@postgresql.org; Fri, 14 Aug 2020 22:04:32 +0000 Received: by mail-il1-x12e.google.com with SMTP id p13so9716780ilh.4 for ; Fri, 14 Aug 2020 15:04:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=2+/6KDL4r1k3zWHbwyWbyAP4YgcTXbR2M84AdEXOXJg=; b=yMc0hvxnml+m7GB4xjbErXREpUolCwCKRi6kAMWP8dn3ha6QwMOOj5/tvFocFBNGoW uYAL28Vihl3vq2iDXcnwebJi1C8Q+OUEWyHd9223iMPJstglpfOrSZHsn4agh4ubcJIM qiFLpXgWzxrj0INeAptkXsEahcfiiUk4z3zi9sskL3K6XR3in+J+rVEQuhaRT4DEb85Q gzmP3CQeEHVZazfuIWlvxrRlSegxL1/LbGDgd5QCCam0qAlO6ozhvgfnMtazqhz1mmOU vkPNz2AHkioCrpmnUeX15pq+HGsMtBq1cyOK1vcqsHeIU4pGVoRWK38PfmdwWWXYy8qo xk3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=2+/6KDL4r1k3zWHbwyWbyAP4YgcTXbR2M84AdEXOXJg=; b=TCEl0rHb7pSjAZ8016uYftWv4yuYZQtZHwVwOmheCYS3gxfgiF2CtIL9SOMYKqhYPm UrdLrk51G6P6atO2B/vH20RsNN/3ES3PgXHY40oAirczgfdrvdYzIanHiTraHgljRWqn ZIUgDk3lkYBTpp3B8TficIYzLC/uUryYMxUwnvI/yV/Z/tH7yXxeqrIPEmylTtc4USt2 YLRM3kp5vqbOPP2eSM4Y/vDxJiD+/9PSqfPHHJBvvZr9+JIEhtxD50/lIxGtUyfxhlrw WXLojTujZMRp3Kqnzi0+zRdkYUmarbFZf95ftcaoLRRWhsidebE0HzHeARDDcETPiHTK paKw== X-Gm-Message-State: AOAM533l4IJeQjxEsWo8wDWJy+tHrFgl/OBr+kGFMKGWeTXHF8rVn9y3 nbCi6tqOd132uGfGF1ShAHEDiQ== X-Google-Smtp-Source: ABdhPJzBPS1tox/+T4TO05x2YrtPm9X4m/yqZluQc7sv28Q+Od/VuAXLRlK6qMtNEDJG3f5vK6NBCA== X-Received: by 2002:a92:6b0d:: with SMTP id g13mr4367031ilc.255.1597442667361; Fri, 14 Aug 2020 15:04:27 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id v84sm1144946ilk.4.2020.08.14.15.04.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 14 Aug 2020 15:04:26 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 12E7880077E; Fri, 14 Aug 2020 17:04:25 -0500 (CDT) Date: Fri, 14 Aug 2020 17:04:24 -0500 From: Justin Pryzby To: Ken Tanzer Cc: pgsql-performance@postgresql.org Subject: Re: Query takes way longer with LIMIT, and EXPLAIN takes way longer than actual query Message-ID: <20200814220424.GK4561@telsasoft.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, Aug 14, 2020 at 02:34:52PM -0700, Ken Tanzer wrote: > Hi. I've got a query that runs fine (~50ms). When I add a "LIMIT 25" to > it though, it takes way longer. The query itself then takes about 4.5 > seconds. And when I do an explain, it takes 90+ seconds for the same query! Due to the over-estimated rowcount, the planner believes that (more) rows will be output (sooner) than they actually are: -> Nested Loop Semi Join (cost=47.11..31367302.81 ROWS=611586 width=336) (actual time=47.098..97236.123 ROWS=25 loops=1) So it thinks there's something to be saved/gained by using a plan that has a low startup cost. But instead, it ends up running for a substantial fraction of the total (estimated) cost. As for the "explain is more expensive than the query", that could be due to timing overhead, as mentioned here. Test with "explain (timing off)" ? https://www.postgresql.org/docs/12/using-explain.html#USING-EXPLAIN-CAVEATS -- Justin