e25ec5b271d3ba3073de307e3d80bc2503ad7207
angie
  Thu Dec 22 11:49:14 2016 -0800
tableExists was always opening a sql connection, but only rarely do we get a table with a sql wildcard that needs a sql query.  So open a sql conn only when necessary -- speeds up all.joiner searches with many matches to tables in different databases like knownGene.

diff --git src/hg/lib/joiner.c src/hg/lib/joiner.c
index 280f0f1..3231c55 100644
--- src/hg/lib/joiner.c
+++ src/hg/lib/joiner.c
@@ -1103,50 +1103,67 @@
 }
 
 static struct joinerField *joinerSetIncludesTable(struct joinerSet *js, 
 	char *database, char *table)
 /* If joiner set includes database and table, return the associated field. */
 {
 struct joinerField *jf;
 for (jf = js->fieldList; jf != NULL; jf = jf->next)
     {
     if (sameString(table, jf->table) && slNameInList(jf->dbList, database))
         return jf;
     }
 return NULL;
 }
 
+static char *skipPrefix(char *prefix, char *string)
+/* If string starts with prefix, return a pointer into string just past the prefix;
+ * otherwise just return string.  Do not free result. */
+{
+if (startsWith(prefix, string))
+    return string + strlen(prefix);
+else
+    return string;
+}
+
+static boolean wildExists(char *database, char *table)
+/* Do a SQL wildcard search for table in database. */
+{
+boolean exists = FALSE;
+struct sqlConnection *conn = hAllocConnMaybe(database);
+if (conn != NULL)
+    exists = sqlTableWildExists(conn, table);
+hFreeConn(&conn);
+return exists;
+}
+
 static boolean tableExists(char *database, char *table, char *splitPrefix)
 /* Return TRUE if database and table exist.  If splitPrefix is given,
  * check for existence with and without it. */
 {
-struct sqlConnection *conn = hAllocConnMaybe(database);
-if (conn == NULL)
-    return FALSE;
 char t2[1024];
 if (isNotEmpty(splitPrefix))
     safef(t2, sizeof(t2), "%s%s", splitPrefix, table);
 else
     safef(t2, sizeof(t2), "%s", table);
-boolean hasSqlWildcard = (strchr(t2, '%') || strchr(t2, '_'));
-boolean exists = hasSqlWildcard ? sqlTableWildExists(conn, t2) : hTableExists(database, t2);
+boolean hasSqlWildcard = (strchr(t2, '%') || strchr(skipPrefix("all_", t2), '_'));
+boolean exists = hasSqlWildcard ? wildExists(database, t2) : hTableExists(database, t2);
 if (!exists && isNotEmpty(splitPrefix))
     {
     hasSqlWildcard = (strchr(table, '%') || strchr(table, '_'));
-    exists = hasSqlWildcard ? sqlTableWildExists(conn, table) : hTableExists(database, table);
+    exists = hasSqlWildcard ? wildExists(database, table) : hTableExists(database, table);
     }
-hFreeConn(&conn);
 return exists;
 }
 
 static void addChildren(struct joinerSet *js,  struct slRef **pList)
 /* Recursively add children to list. */
 {
 struct slRef *childRef;
 for (childRef = js->children; childRef != NULL; childRef = childRef->next)
     {
     struct joinerSet *child = childRef->val;
     refAdd(pList, child);
     addChildren(child, pList);
     }
 }