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 1muHe0-0001Kq-VA for pgsql-general@arkaria.postgresql.org; Mon, 06 Dec 2021 17:19:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1muHdz-0006Va-SQ for pgsql-general@arkaria.postgresql.org; Mon, 06 Dec 2021 17:19:15 +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 1muHdz-0006VR-Gu for pgsql-general@lists.postgresql.org; Mon, 06 Dec 2021 17:19:15 +0000 Received: from mail-il1-x12d.google.com ([2607:f8b0:4864:20::12d]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1muHdw-0000Vk-2u for pgsql-general@lists.postgresql.org; Mon, 06 Dec 2021 17:19:14 +0000 Received: by mail-il1-x12d.google.com with SMTP id r2so10937064ilb.10 for ; Mon, 06 Dec 2021 09:19:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=qeiYBYIJNXel/r/2Xz8UQKCyqHRuJxaLDsPcvEGYn74=; b=RkTmn7z56XaV7BJIBxN9lv1Br8+5bUybBWtigh2kMNCNkpVXxFvoDbbey87sW4HQpv ghSVBPxdALHWTT1v7qSzkUPUUXH/5lTpS6FV2dtsxQIWxq+uaQ72eI34n8ghapNq+hKf hQN9nB8aE7EteEd3pG8exMLPcZ/ZMOIMPgLzfd7VFyLMwvggfUK1XjYRzTXZtL8J/BJZ kiyyj4+lyvzc4j2W0hQ2Q98UfT/ePOAKoG9OArdQA90NhVRgRTWja303iIDlryXtbU3X Gmx7sctUP1gTAp3u6vtskU720jUJS2Rud83fKm0xxeOqPoYUboe+BWhnAIyTMqCwLqGd mcDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=qeiYBYIJNXel/r/2Xz8UQKCyqHRuJxaLDsPcvEGYn74=; b=gO9zuf3bPwA22ChzzZriGqCuPhp4vWU5QttHAKU/0NLDeOmdtazk+Cq1PEdBH/NaLx oyE7EenjUM1aBNXyTUzEhnVM0rIifFe0rjQyYVjsnaeCoy7Yu9tceePT4BcNnYfseWRc almnmFa2wa+dOq8eNc4J1b3jcVdcyze7sfmUMyx4V0XS7i0CBPdmAsdhNgubU+l1Vjy+ NzbmVPAHaoY6NZDekAxuPrGKuIhIl2s7GDQMUImVmJl8ppDAQbwkEiFtLSyHCl9Vqm9U 69YGi+CtclbYaqLtxvOlFwpQm99z4XjBteh55A/wumLz82C8/AKnkm422ICwOlLLDUsS FbYw== X-Gm-Message-State: AOAM5304k6J2ORjw68xRytvC70pBmcRh1695Vq7Gs9fon3BNYQ/DzUO0 /8vmHu/0YciAU/T5aZq1DtVn8SmFrAk= X-Google-Smtp-Source: ABdhPJw3QELyQcvk6dSwPgOa5HlJ1sm2uwb+Dy8F43YHuoL1/4oq549pjPI6j7tlekJk3gN9wEZuYQ== X-Received: by 2002:a05:6e02:16c9:: with SMTP id 9mr31579603ilx.221.1638811150629; Mon, 06 Dec 2021 09:19:10 -0800 (PST) Received: from [10.0.0.94] (c-73-63-59-118.hsd1.ut.comcast.net. [73.63.59.118]) by smtp.gmail.com with ESMTPSA id h12sm7705317ila.81.2021.12.06.09.19.10 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 06 Dec 2021 09:19:10 -0800 (PST) Subject: Re: Query planner issue with preferring primary key over a better index when using ORDER BY and LIMIT To: pgsql-general@lists.postgresql.org References: From: Rob Sargent Message-ID: Date: Mon, 6 Dec 2021 10:19:09 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-CA List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 12/6/21 10:03 AM, Alan Hodgson wrote: > I keep running into problems like these: > > Devs are using an ORM. It really likes to produce queries like: > > SELECT "shipment_import_records".* FROM "shipment_import_records" > WHERE shipment_import_records"."shipment_import_id" = 5090609 ORDER BY > "shipment_import_records"."id" ASC LIMIT 1; > > I don't know why they do this. Usually it's more like 50 for > pagination which make more sense. But for whatever reason this keeps > coming up. > To be clear, is it the devs or the ORM that's adding the ORDER  and the LIMIT?  I'm betting on devs.  Do they need the smallest id (first occurrance?) or do they need data common to all 5096 entries (Name?) and any record will do?.  For the former they might be better off asking for just the attributes they need and for the latter you need to provide an option which gets them that single record.  Of course, If they have the "smallest id" in hand they should request that.