Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cxYJ0-00086P-Cl for pgsql-performance@arkaria.postgresql.org; Mon, 10 Apr 2017 12:20:26 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cxYIz-00064S-Up for pgsql-performance@arkaria.postgresql.org; Mon, 10 Apr 2017 12:20:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cxYHF-0002tt-BC for pgsql-performance@postgresql.org; Mon, 10 Apr 2017 12:18:37 +0000 Received: from lb2-smtp-cloud2.xs4all.net ([194.109.24.25]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1cxYH8-0001zL-6U for pgsql-performance@postgresql.org; Mon, 10 Apr 2017 12:18:36 +0000 Received: from webmail.xs4all.nl ([IPv6:2001:888:0:22:194:109:20:215]) by smtp-cloud2.xs4all.net with ESMTP id 6cJU1v00K13if5201cJUAz; Mon, 10 Apr 2017 14:18:28 +0200 Received: from mail.genj.nl ([77.242.116.162]) by webmail.xs4all.nl with HTTP (HTTP/1.1 POST); Mon, 10 Apr 2017 14:18:28 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Mon, 10 Apr 2017 14:18:28 +0200 From: vinny To: Haider Ali Cc: pgsql-performance@postgresql.org Subject: Re: Understanding PostgreSQL query execution time In-Reply-To: References: Message-ID: <1e4f7641ac0b76f9b655117c291322c0@xs4all.nl> X-Sender: vinny@xs4all.nl User-Agent: XS4ALL Webmail X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On 2017-04-07 16:56, Haider Ali wrote: > Hello > > I want to understand execution time of a query in PostgreSQL then I > want to relate it to the problem i am getting. According to my > observation ( I can't explain why this happen ) whenever we query a > table first time its execution will be high (sometimes very high) as > compare to queries made on same table in a short period of time > followed by first query on that table. For example query given below The first time a query is executed it is quite likely that the data it needs is not in RAM yet, so it must fetch the data from disk, which is slow. But, benchmarking is an art; did you execute these queries separately from the commandline? Otherwise where may be other forces at work here... > > Having experience above behaviour of PostgreSQL now I am using > PostgreSQL managed by Amazon RDS. Observation is no matter how many > times I execute same query its execution times remain same ( although > execution time of a query on RDS is comparatively high as compare to > query running on local instance of PostgreSQL that I can understand is > because of Network latency) The problem may go away entirely if the database/OS has enough RAM available, and configured, for caching. The problem on your local system may be simply a case of PostgreSQL or the OS removing tuples/index data from RAM when it feels it can make better use of that RAM space for other things if you don't access that data for a while. Try spying on your system with iotop and such tools to see what the server is actually doing during the first query. If there is a spike in disk-IO then you've found the cause; the tuples where not in RAM. You may also want to run an EXPLAIN to make sure that the fast queries are not purely the result of some query-result cache. -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance