X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id B1AAB9DC877 for ; Wed, 22 Feb 2006 13:27:00 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 49129-06 for ; Wed, 22 Feb 2006 13:26:59 -0400 (AST) Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 5728C9DC99D for ; Wed, 22 Feb 2006 13:26:58 -0400 (AST) Received: from zeus.liquidweb.com (zeus.liquidweb.com [64.91.224.170]) by svr4.postgresql.org (Postfix) with ESMTP id 86D4C5AF18C for ; Wed, 22 Feb 2006 17:26:58 +0000 (GMT) Received: from cpusoftw by zeus.liquidweb.com with local (Exim 4.52) id 1FBxlH-0003Kt-9V for pgsql-performance@postgresql.org; Wed, 22 Feb 2006 12:26:47 -0500 From: "ryan groth" To: pgsql-performance@postgresql.org Subject: Joins and full index scans...mysql vs postgres? X-Mailer: NeoMail 1.27 X-IPAddress: 127.0.0.1 MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Message-Id: Date: Wed, 22 Feb 2006 12:26:47 -0500 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - zeus.liquidweb.com X-AntiAbuse: Original Domain - postgresql.org X-AntiAbuse: Originator/Caller UID/GID - [32069 32069] / [47 12] X-AntiAbuse: Sender Address Domain - cpusoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[none] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200602/347 X-Sequence-Number: 17333 I am issing a query like this: SELECT * FROM users users LEFT JOIN phorum_users_base ON users.uid = phorum_users_base.user_id LEFT JOIN useraux ON useraux.uid = users.uid; The joins are all on the PKs of the tables. It takes 1000ms to run on postgres. The identical mysql version runs in 230ms. The problem seems to stem from postgres's insistence to do three complete table scans, where mysql does one and joins 1:1 against the results of the first. I have switched the joins to inner joins and the difference is negligible. Here are the explains on both postgres and mysql. Is there a way to optimize this basic query for postgres that I am missing? Postgres Explain Merge Left Join (cost=0.00..2656.36 rows=6528 width=1522) Merge Cond: ("outer".uid = "inner".uid) -> Merge Left Join (cost=0.00..1693.09 rows=6528 width=1264) Merge Cond: ("outer".uid = "inner".user_id) -> Index Scan using users_pkey on users (cost=0.00..763.81 rows=6528 width=100) -> Index Scan using phorum_users_base_pkey on phorum_users_base (cost=0.00..822.92 rows=9902 width=1168) -> Index Scan using useraux_pkey on useraux (cost=0.00..846.40 rows=7582 width=262) MySQL Explain: id,select_type,table,possible_keys,key,key_len,ref,rows,extra 1, 'PRIMARY', 'USERS', 'ALL', '', '', '', '', 6528, '' 1, 'PRIMARY', 'phorum_users_base', 'eq_ref', 'PRIMARY', 'PRIMARY', '4', 'wh2o.USERS.UID', 1, '' 1, 'PRIMARY', 'useraux', 'eq_ref', 'PRIMARY', 'PRIMARY', '4', 'wh2o.USERS.UID', 1, ''