diff --git a/src/applications/almanac/query/AlmanacInterfaceQuery.php b/src/applications/almanac/query/AlmanacInterfaceQuery.php
--- a/src/applications/almanac/query/AlmanacInterfaceQuery.php
+++ b/src/applications/almanac/query/AlmanacInterfaceQuery.php
@@ -121,7 +121,7 @@
           $address->getAddress(),
           $address->getPort());
       }
-      $where[] = implode(' OR ', $parts);
+      $where[] = qsprintf($conn, '%LO', $parts);
     }
 
     return $where;
diff --git a/src/applications/calendar/query/PhabricatorCalendarEventQuery.php b/src/applications/calendar/query/PhabricatorCalendarEventQuery.php
--- a/src/applications/calendar/query/PhabricatorCalendarEventQuery.php
+++ b/src/applications/calendar/query/PhabricatorCalendarEventQuery.php
@@ -444,8 +444,8 @@
 
       $where[] = qsprintf(
         $conn,
-        '%Q',
-        implode(' OR ', $sql));
+        '%LO',
+        $sql);
     }
 
     if ($this->isStub !== null) {
diff --git a/src/applications/daemon/management/PhabricatorLockLogManagementWorkflow.php b/src/applications/daemon/management/PhabricatorLockLogManagementWorkflow.php
--- a/src/applications/daemon/management/PhabricatorLockLogManagementWorkflow.php
+++ b/src/applications/daemon/management/PhabricatorLockLogManagementWorkflow.php
@@ -107,9 +107,9 @@
     }
 
     if (!$parts) {
-      $constraint = '1 = 1';
+      $constraint = qsprintf($conn, '1 = 1');
     } else {
-      $constraint = '('.implode(') AND (', $parts).')';
+      $constraint = qsprintf($conn, '%LA', $parts);
     }
 
     $logs = $table->loadAllWhere(
diff --git a/src/applications/diffusion/DiffusionLintSaveRunner.php b/src/applications/diffusion/DiffusionLintSaveRunner.php
--- a/src/applications/diffusion/DiffusionLintSaveRunner.php
+++ b/src/applications/diffusion/DiffusionLintSaveRunner.php
@@ -229,9 +229,9 @@
         $this->conn,
         'INSERT INTO %T
           (branchID, path, line, code, severity, name, description)
-          VALUES %Q',
+          VALUES %LQ',
         PhabricatorRepository::TABLE_LINTMESSAGE,
-        implode(', ', $values));
+        $values);
     }
 
     $this->conn->saveTransaction();
@@ -295,10 +295,10 @@
         }
         queryfx(
           $this->conn,
-          'UPDATE %T SET authorPHID = %s WHERE %Q',
+          'UPDATE %T SET authorPHID = %s WHERE %LO',
           PhabricatorRepository::TABLE_LINTMESSAGE,
           $author,
-          implode(' OR ', $where));
+          $where);
       }
 
       $this->conn->saveTransaction();
diff --git a/src/applications/diffusion/conduit/DiffusionBrowseQueryConduitAPIMethod.php b/src/applications/diffusion/conduit/DiffusionBrowseQueryConduitAPIMethod.php
--- a/src/applications/diffusion/conduit/DiffusionBrowseQueryConduitAPIMethod.php
+++ b/src/applications/diffusion/conduit/DiffusionBrowseQueryConduitAPIMethod.php
@@ -451,13 +451,13 @@
         WHERE repositoryID = %d
           AND parentID = %d
           AND existed = 1
-        AND (%Q)
+        AND (%LO)
         ORDER BY pathName',
       PhabricatorRepository::TABLE_FILESYSTEM,
       PhabricatorRepository::TABLE_PATH,
       $repository->getID(),
       $path_id,
-      implode(' OR ', $sql));
+      $sql);
 
     $loadable_commits = array();
     foreach ($browse as $key => $file) {
diff --git a/src/applications/diffusion/controller/DiffusionLintController.php b/src/applications/diffusion/controller/DiffusionLintController.php
--- a/src/applications/diffusion/controller/DiffusionLintController.php
+++ b/src/applications/diffusion/controller/DiffusionLintController.php
@@ -276,13 +276,13 @@
             array_keys($branch),
             $path->getPath());
           if ($path->getExcluded()) {
-            $where[] = 'NOT '.$condition;
+            $where[] = qsprintf($conn, 'NOT %Q', $condition);
           } else {
             $or[] = $condition;
           }
         }
       }
-      $where[] = '('.implode(' OR ', $or).')';
+      $where[] = qsprintf($conn, '%LO', $or);
     }
 
     return queryfx_all(
@@ -296,11 +296,11 @@
           COUNT(DISTINCT path) AS files,
           COUNT(*) AS n
         FROM %T
-        WHERE %Q
+        WHERE %LA
         GROUP BY branchID, code
         ORDER BY n DESC',
       PhabricatorRepository::TABLE_LINTMESSAGE,
-      implode(' AND ', $where));
+      $where);
   }
 
   protected function buildActionView(DiffusionRequest $drequest) {
@@ -526,10 +526,10 @@
       $conn,
       'SELECT *
         FROM %T
-        WHERE %Q
+        WHERE %LA
         ORDER BY path, code, line LIMIT %d OFFSET %d',
       PhabricatorRepository::TABLE_LINTMESSAGE,
-      implode(' AND ', $where),
+      $where,
       $limit,
       $offset);
   }
diff --git a/src/applications/diffusion/query/DiffusionCachedResolveRefsQuery.php b/src/applications/diffusion/query/DiffusionCachedResolveRefsQuery.php
--- a/src/applications/diffusion/query/DiffusionCachedResolveRefsQuery.php
+++ b/src/applications/diffusion/query/DiffusionCachedResolveRefsQuery.php
@@ -81,10 +81,10 @@
       $commits = queryfx_all(
         $conn_r,
         'SELECT commitIdentifier FROM %T
-          WHERE repositoryID = %s AND %Q',
+          WHERE repositoryID = %s AND %LO',
         id(new PhabricatorRepositoryCommit())->getTableName(),
         $repository->getID(),
-        implode(' OR ', $prefixes));
+        $prefixes);
 
       foreach ($commits as $commit) {
         $hash = $commit['commitIdentifier'];
diff --git a/src/applications/fact/query/PhabricatorFactDatapointQuery.php b/src/applications/fact/query/PhabricatorFactDatapointQuery.php
--- a/src/applications/fact/query/PhabricatorFactDatapointQuery.php
+++ b/src/applications/fact/query/PhabricatorFactDatapointQuery.php
@@ -158,7 +158,7 @@
         $this->dimensionMap);
     }
 
-    $where = '('.implode(') AND (', $where).')';
+    $where = qsprintf($conn, '%LA', $where);
 
     if ($this->limit) {
       $limit = qsprintf(
@@ -166,7 +166,7 @@
         'LIMIT %d',
         $this->limit);
     } else {
-      $limit = '';
+      $limit = qsprintf($conn, '');
     }
 
     return queryfx_all(
diff --git a/src/applications/multimeter/controller/MultimeterSampleController.php b/src/applications/multimeter/controller/MultimeterSampleController.php
--- a/src/applications/multimeter/controller/MultimeterSampleController.php
+++ b/src/applications/multimeter/controller/MultimeterSampleController.php
@@ -52,8 +52,6 @@
       }
     }
 
-    $where = '('.implode(') AND (', $where).')';
-
     $data = queryfx_all(
       $conn,
       'SELECT *,
@@ -61,13 +59,13 @@
           SUM(sampleRate * resourceCost) AS totalCost,
           SUM(sampleRate * resourceCost) / SUM(sampleRate) AS averageCost
         FROM %T
-        WHERE %Q
-        GROUP BY %Q
+        WHERE %LA
+        GROUP BY %LC
         ORDER BY totalCost DESC, MAX(id) DESC
         LIMIT 100',
       $table->getTableName(),
       $where,
-      implode(', ', array_select_keys($group_map, $group)));
+      array_select_keys($group_map, $group));
 
     $this->loadDimensions($data);
     $phids = array();
diff --git a/src/applications/packages/query/PhabricatorPackagesQuery.php b/src/applications/packages/query/PhabricatorPackagesQuery.php
--- a/src/applications/packages/query/PhabricatorPackagesQuery.php
+++ b/src/applications/packages/query/PhabricatorPackagesQuery.php
@@ -32,7 +32,7 @@
       throw new PhabricatorEmptyQueryException();
     }
 
-    return implode(' OR ', $parts);
+    return qsprintf($conn, '%LO', $parts);
   }
 
 }
diff --git a/src/applications/people/query/PhabricatorPeopleQuery.php b/src/applications/people/query/PhabricatorPeopleQuery.php
--- a/src/applications/people/query/PhabricatorPeopleQuery.php
+++ b/src/applications/people/query/PhabricatorPeopleQuery.php
@@ -268,7 +268,7 @@
           'user.username LIKE %>',
           $name_prefix);
       }
-      $where[] = '('.implode(' OR ', $parts).')';
+      $where[] = qsprintf($conn, '%LO', $parts);
     }
 
     if ($this->emails !== null) {
diff --git a/src/applications/phrequent/query/PhrequentUserTimeQuery.php b/src/applications/phrequent/query/PhrequentUserTimeQuery.php
--- a/src/applications/phrequent/query/PhrequentUserTimeQuery.php
+++ b/src/applications/phrequent/query/PhrequentUserTimeQuery.php
@@ -177,9 +177,9 @@
 
       $preempting_events = queryfx_all(
         $conn_r,
-        'SELECT * FROM %T WHERE %Q ORDER BY dateStarted ASC, id ASC',
+        'SELECT * FROM %T WHERE %LO ORDER BY dateStarted ASC, id ASC',
         $usertime->getTableName(),
-        implode(' OR ', $preempt));
+        $preempt);
       $preempting_events = $usertime->loadAllFromArray($preempting_events);
 
       $preempting_events = mgroup($preempting_events, 'getUserPHID');
diff --git a/src/applications/phriction/query/PhrictionDocumentQuery.php b/src/applications/phriction/query/PhrictionDocumentQuery.php
--- a/src/applications/phriction/query/PhrictionDocumentQuery.php
+++ b/src/applications/phriction/query/PhrictionDocumentQuery.php
@@ -309,10 +309,14 @@
             $max);
         }
 
-        $path_clauses[] = '('.implode(') AND (', $parts).')';
+        if ($parts) {
+          $path_clauses[] = qsprintf($conn, '%LA', $parts);
+        }
       }
 
-      $where[] = '('.implode(') OR (', $path_clauses).')';
+      if ($path_clauses) {
+        $where[] = qsprintf($conn, '%LO', $path_clauses);
+      }
     }
 
     return $where;
diff --git a/src/applications/project/query/PhabricatorProjectQuery.php b/src/applications/project/query/PhabricatorProjectQuery.php
--- a/src/applications/project/query/PhabricatorProjectQuery.php
+++ b/src/applications/project/query/PhabricatorProjectQuery.php
@@ -514,7 +514,7 @@
           'name LIKE %>',
           $name_prefix);
       }
-      $where[] = '('.implode(' OR ', $parts).')';
+      $where[] = qsprintf($conn, '%LO', $parts);
     }
 
     if ($this->icons !== null) {
diff --git a/src/infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php b/src/infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php
--- a/src/infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php
+++ b/src/infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php
@@ -1440,7 +1440,11 @@
         $key);
     }
 
-    return implode(' ', $joins);
+    if ($joins) {
+      return qsprintf($conn, '%LJ', $joins);
+    } else {
+      return qsprintf($conn, '');
+    }
   }
 
   /**
@@ -1516,7 +1520,7 @@
           }
 
           if ($constraint_parts) {
-            $where[] = '('.implode(') OR (', $constraint_parts).')';
+            $where[] = qsprintf($conn, '%LO', $constraint_parts);
           }
           break;
       }
@@ -1670,7 +1674,7 @@
     }
 
     if (!$this->ferretEngine) {
-      $select[] = '0 _ft_rank';
+      $select[] = qsprintf($conn, '0 _ft_rank');
       return $select;
     }
 
@@ -1736,12 +1740,21 @@
       }
     }
 
-    $parts[] = '0';
+    $parts[] = qsprintf($conn, '%d', 0);
+
+    $sum = array_shift($parts);
+    foreach ($parts as $part) {
+      $sum = qsprintf(
+        $conn,
+        '%Q + %Q',
+        $sum,
+        $part);
+    }
 
     $select[] = qsprintf(
       $conn,
       '%Q _ft_rank',
-      implode(' + ', $parts));
+      $sum);
 
     return $select;
   }
@@ -2031,20 +2044,20 @@
       if ($is_not) {
         $where[] = qsprintf(
           $conn,
-          '(%Q)',
-          implode(' AND ', $term_constraints));
+          '%LA',
+          $term_constraints);
       } else if ($is_quoted) {
         $where[] = qsprintf(
           $conn,
-          '(%T.rawCorpus LIKE %~ AND (%Q))',
+          '(%T.rawCorpus LIKE %~ AND %LO)',
           $table_alias,
           $value,
-          implode(' OR ', $term_constraints));
+          $term_constraints);
       } else {
         $where[] = qsprintf(
           $conn,
-          '(%Q)',
-          implode(' OR ', $term_constraints));
+          '%LO',
+          $term_constraints);
       }
     }