Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TGxp5-0006y6-Tr; Wed, 26 Sep 2012 20:03:08 +0000 Received: from smtp.peak6.com ([38.98.137.6]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TGxp3-0000vZ-Cy; Wed, 26 Sep 2012 20:03:06 +0000 X-AuditID: 0a64cb1e-b7fb36d000000f29-54-50635f517b48 Message-ID: <50635F77.60008@optionshouse.com> Date: Wed, 26 Sep 2012 15:03:03 -0500 From: Shaun Thomas Reply-To: Organization: Peak6 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120827 Thunderbird/15.0 MIME-Version: 1.0 To: Robert Sosinski CC: , , Spike Grobstein Subject: Re: Inaccurate Explain Cost References: In-Reply-To: Content-Type: text/plain; charset="UTF-8"; format=flowed content-transfer-encoding: 7bit X-Originating-IP: [10.10.201.64] X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrOIsWRmVeSWpSXmKPExsXClcIrphsYnxxgcO+FnsX0u1vZLK7PPMds 8XffOjaLR9sfszmweFzYN43dY//j9SwBTFENjDaJeXn5JYklqQopqcXJtkr+BSWZ+XnFHvml xakKLpnFyTmJmbmpRUoKmSm2SuZKCgU5icmpual5JbZKiQUFqXkpSnZcChjABqgsM08hNS85 PyUzL91WyTPYX9fCwtRS11DJLh4ZJHSyZDSd3stYsEqsouHwPrYGxseCXYycHBICJhLbX35n hrDFJC7cW88GYvMK6ErMaPgJZHNwsAioSsxqFAYJswGFJ/WvACsRElCR6Dt7gwnE5hcQltj3 oQlsjKhAkMSyjd+ZIMYISpyc+YQFxBYRMJbo/fAMrJdZoFTi18Kz7CC2sICexPSpb6BmOkkc 3fGOEWQtp4CzxKNOfYhyC4nFbw6yQ9jyEtvfzmGGKFeVWNE8kQ3iegWJ/1feMU1gFJqFZPMs JO2zkLQvYGRexSheXJaYnJFpWlCcW1JgqFeQmphtppeXWrKJERzJp+V2ME5fqniIUYCDUYmH d2NAcoAQa2JZcWXuIUYJDmYlEd5nWUkBQrwpiZVVqUX58UWlOanFhxhNgSE2kVlKNDkfmGTy SuINjQ0tTS1NzCzNjY0NlMR5bfuUAoQE0oGJITs1tSC1CKaPiYNTqoFx4t7FTCdeZm8qOvf8 zQOVysk+7lvT2bVVWpflM7Q1s9zbb3S2YUpXeZN+qF1eUnN3MUvjvHcKbwJE7mvF8qnP/bbv 4Nbtq7Zkli7j91Zbd8s29bWJ4P1FH/gm18o9/DK5Y1GeK8/KRVz6bUvUlt1Qk7/K4tryYuWf OEVzNh0NFgXdJ7mx2UuVWIozEg21mIuKEwHn6zTg+gIAAA== X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201209/143 X-Sequence-Number: 47889 On 09/26/2012 01:38 PM, Robert Sosinski wrote: > I seem to be getting an inaccurate cost from explain. Here are two > examples for one query with two different query plans: Well, there's this: Nested Loop (cost=0.00..151986.53 rows=2817 width=4) (actual time=163.275..186869.844 rows=43904 loops=1) If anything's a smoking gun, that is. I could see why you'd want to turn off nested loops to get better execution time. But the question is: why did it think it would match so few rows in the first place? The planner probably would have thrown away this query plan had it known it would loop 20x more than it thought. I think we need to know what your default_statistics_target is set at, and really... all of your relevant postgresql settings. Please see this: http://wiki.postgresql.org/wiki/Slow_Query_Questions But you also may need to look a lot more into your query itself. The difference between a 2 or a 3 minute query isn't going to help you much. Over here, we tend to spend more of our time turning 2 or 3 minute queries into 20 or 30ms queries. But judging by your date range, getting the last 2-months of data from a table that large generally won't be fast by any means. That said, looking at your actual query: SELECT COUNT(DISTINCT eu.id) FROM exchange_uploads eu JOIN upload_destinations ud ON ud.id = eu.upload_destination_id LEFT JOIN uploads u ON u.id = eu.upload_id LEFT JOIN import_errors ie ON ie.exchange_upload_id = eu.id LEFT JOIN exchanges e ON e.id = ud.exchange_id WHERE eu.created_at >= '2012-07-27 21:21:57.363944' AND ud.office_id = 6; Doesn't need half of these joins. They're left joins, and never used in the query results or where criteria. You could just use this: SELECT COUNT(DISTINCT eu.id) FROM exchange_uploads eu JOIN upload_destinations ud ON (ud.id = eu.upload_destination_id) WHERE eu.created_at >= '2012-07-27 21:21:57.363944' AND ud.office_id = 6; Though I presume this is just a count precursor to a query that fetches the actul results and does need the left join. Either way, the index scan from your second example matches 3.3M rows by using the created_at index on exchange_uploads. That's not really very restrictive, and so you have two problems: 1. Your nested loop stats from office_id are somehow wrong. Try increasing your stats on that column, or just default_statistics_target in general, and re-analyze. 2. Your created_at criteria above match way too many rows, and will also take a long time to process. Those are your two actual problems. We can probably get your query to run faster, but those are pretty significant hurdles. -- Shaun Thomas OptionsHouse | 141 W. Jackson Blvd. | Suite 500 | Chicago IL, 60604 312-444-8534 sthomas@optionshouse.com ______________________________________________ See http://www.peak6.com/email_disclaimer/ for terms and conditions related to this email