Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fe8yG-0002c6-Sj for pgsql-hackers@arkaria.postgresql.org; Sat, 14 Jul 2018 01:03:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fe8yC-0005Cg-1m for pgsql-hackers@arkaria.postgresql.org; Sat, 14 Jul 2018 01:03:32 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fe8yB-0005CY-J2 for pgsql-hackers@lists.postgresql.org; Sat, 14 Jul 2018 01:03:31 +0000 Received: from mail-wm0-x22c.google.com ([2a00:1450:400c:c09::22c]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fe8y7-0006kL-VH for pgsql-hackers@lists.postgresql.org; Sat, 14 Jul 2018 01:03:30 +0000 Received: by mail-wm0-x22c.google.com with SMTP id s13-v6so5647769wmc.1 for ; Fri, 13 Jul 2018 18:03:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=Bzd8fcFal2GtKKFXtfv9Sq8rewnJSPftpHsqzgjYExI=; b=VTRZETR6Zidn6vJtVcdLHb02q2MnJFPo5KAmCHCYP3jVSRWL7O8cwjOaiimBQ0CsT3 XLVypt+eDhSrCCXsAH/TX+pW3X0E49Ffv5FVgrMWlTbjUJdVdedjaBruNtyBuGz4daWh uBImr3yhedlC3hniLlVaP0UiLSqcRUrqLX7j+i1HIdui9Qopah65bfKLAUOz3k2GMGBm 83gj44L4lMN4JHJEOt4qjnK1EYghTqNhLrNCRwpTo4jhNqwTXDmhUtm/YsK8j63CDzG2 psCd+UPVwgYHGS0fbk8x+nhkjJq8noZWkA9PyLQdU3ycxreb8wEkjCJ4WBaPknWoJMF1 ONdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=Bzd8fcFal2GtKKFXtfv9Sq8rewnJSPftpHsqzgjYExI=; b=FYG4YvElbwfXMaIFrVc8bOH7fNWghmmUC5VUlwwoEODt3WEB0vAzHsmSF3uUftozQR bV/8xtZb6xvFf2x1DL94RmjynMEY8nbDkq7f4UkC2GCmkMN+PnhXQnnIyNKz5Z1mHSct SbKgjZ3yCl/HuGgbwEE/H+uxPJcoR/h4lYO4O3dMBV5XSCnyR0IIjAh/ZYItdMtmJt17 U1k53qykf3MG4MoC/cGLN1MuSLkcIadkstTr2UGweTYPGotj5l8BjR50P2xu/m6l0Fe0 8Jm4f46kUrQSJkwo67vYcb5tLS2AZ159uPJYUUBBBfmO2v+kix6HhqvXZcqXmHHyjDwA sntQ== X-Gm-Message-State: AOUpUlGDVhpqq5xHpOya9q8VLBkSg7KHOR1dg9F9Ro0CNCHsuiDFvUlt r0yvSfqFe/2MnJGfqcYKzXQdHx5geBB81/Fz5nDwUFvstVUJwrAao6g5eEaEREjcP/zwHfMStY+ TzQDVq2lT9Ega/S0y5iAxYo7hDteT2ZF18c0o7glNiSIgRNMZJ4lFvNxERbpsofGTkiNStCMvTp cB30eNsCjMCfBDvo4P X-Google-Smtp-Source: AAOMgpd3VPumlvYS3zeUEurDzZePLr5O3hQwNadDADAeojNO3RHtaRvX6gyGxfshA4ScxxAf0fqgow== X-Received: by 2002:a1c:69d2:: with SMTP id z79-v6mr5233865wmh.92.1531530205681; Fri, 13 Jul 2018 18:03:25 -0700 (PDT) Received: from [10.137.2.19] (ip-78-102-97-226.net.upcbroadband.cz. [78.102.97.226]) by smtp.gmail.com with ESMTPSA id l10-v6sm56610880wrv.23.2018.07.13.18.03.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 13 Jul 2018 18:03:24 -0700 (PDT) Subject: Re: Fwd: GSOC 2018 Project - A New Sorting Routine To: Kefan Yang , PostgreSQL Hackers References: <57ae7691-6dc8-d6d0-361a-a7269e656273@2ndquadrant.com> From: Tomas Vondra Message-ID: <2af33cf9-16cf-df04-e8c4-6ab33a1b6f68@2ndquadrant.com> Date: Sat, 14 Jul 2018 03:03:21 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 07/14/2018 12:04 AM, Kefan Yang wrote: > > ... > > And finally, I see the PDF reports "CPU clocks" but I'm not sure what > that actually is? Is that elapsed time in milliseconds or something > else? > > > Sorry for the confusion, but "CPU clocks" actually means CPU clock > ticks, which are units of time of a constant but system-specific length. > OK, how do you measure this metric? > After reading your test results, I think the main problems of intro sort are > > 1. Slow on CREATE INDEX cases. > > I am still trying to figure out where the bottleneck is. Is the data > pattern in index creation very different from other cases? Also, > pg_qsort has 10%-20% advantage at creating index even on sorted data > (faster CPU, N = 1000000). This is very strange to me since the two > sorting routines execute exactly the same code when the input data is > sorted. > > 2. Slow on faster CPU and large data set. > > The main difference is still on CREATE INDEX.  But there are several > SELECT cases(faster CPU, line 179, 206, 377) where pg_qsort can have > more than 60% advantage, which is crazy. All these cases are dealing > with mostly sorted data.  > > Personally, I would say intro sort is good as long as it has nearly the > same performance as pg_qsort on average cases because of the better > worst-case complexity. In fact, we can make the two sorting routines as > similar as we want by increasing the threshold that intro sort switches > to heap sort. Therefore, I think by no means could pg_qsort be 10%-20% > faster than a well-optimized intro sort because they execute the same > code most of the time. There must be something special about CREATE > INDEX test cases, and I am trying to figure it out. Also, I am > considering performing the preordered check on every recursion, like > what pg_qsort does, and see how it goes. > I don't know. All I have is the results I shared. I suggest you try reproducing the tests on your system - the scripts are in the git repositories. > Finally, you've mentioned > > But even if it was, I guess the easiest way to deal with it would be to > randomize the selection of pivots. > > > I am wondering if pg_sort with random pivot selecting could be faster > than intro sort. I've just done some simple tests, which shows that > intro sort in faster in all cases. But I guess it depends on how we > would implement the random number generation. > Unlikely. The pivot randomization is merely a way to defeat an adversary attempting to perform DoS by triggering sorts on a killer sequence. Randomization makes it much harder/impossible, because the killer sequence changes over time. It's not a regular performance optimization. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services